How to create and run a Java program

To create a Java program, just create a file with a class that contains a public static method, called main. Compile and run this class in a Java virtual machine.

Below is the explanation in detail of how to do this, using the MS-DOS console. If you do not know how to work with the MS-Dos console, read the tutorials "Command Line" and "MS-DOS commands."

How to create a Java class in a .java file

The class HelloWorld, is executable since it has the necessary main method. Use the showMessageDialog class method of the JOptionPane class to display an alert window that says "Hello World".

    import javax.swing.JOptionPane;
    public class HelloWorld {
        public static void main(String[] args) {
            JOptionPane.showMessageDialog(null, "Hello World");

The class code, must be in a file, with the same name and Java class extension: "". It would also be advisable to create a directory in which to work.

C:\Users\edup>cd \
C:\>md javatest
C:\>cd javatest

To create, use notepad or any other plain text editor. Copy the text of the class within the editor and save in the javatest directory with the name "".

Java Compiler

For our class to be understood by the java virtual machine, it has to be compiled. For this, we use the javac.exe compiler, which is included in the JDK.

 El volumen de la unidad C es OS
 El número de serie del volumen es: 008E-DBB0

 Directorio de C:\javatest

06/05/2014  21:24    <DIR>          .
06/05/2014  21:24    <DIR>          ..
06/05/2014  21:24               395 HelloWorld.class
06/05/2014  21:23               163
               2 archivos            558 bytes
               2 dirs  35.856.474.112 bytes libres

The compiler takes as an argument the file, and generates a file HelloWorld.class containing the bytecode, which is understood by the virtual machine.

How to run a program using the Java virtual machine

The virtual machine runs using the java.exe file. As an argument, you must give the name of the class you want to run

C:\javatest>java HelloWorld  

Keep in mind that Java, will look for the class in the current directory, so it must run in the same directory where the class file is located.

If all goes well, we will see a window that says "Hello World":

Mensaje de alerta

JAR files

The jar files are used to package applications or Java libraries. A jar file usually contains many compiled classes ".class", directories and a file called "MANIFEST.MF" in a directory called META-INF.

The jar files are compressed in zip format, but are stored with ".jar".

A jar file can be converted to an executable, if you include in your "MANIFEST.MF" Main-Class: followed by the name of the class you want to run. To run it, you use "java-jar", followed by the class name.

In Windows, when we install the JDK, the jar files become associated with java.exe, so we can make them run with a double click.

How to create HelloWorld.jar

Create a file manifest.txt in the javatest directory and include the following text:

Main-Class: HelloWorld

IMPORTANT: to make it work, insert enter after HelloWorld.

Once we have the manifiest.txt, we can run the jar.exe tool.

The "cvfm" switch, in the call to jar.exe means; "c" create a jar, "v" show verbose output, "f" specify the jar file name, "m" specify the manifest file name.

C:\javatest>jar cvfm HelloWorld.jar manifest.txt *.class
manifiesto agregado
agregando: HelloWorld.class(entrada = 395) (salida = 288)(desinflado 27%)

 El volumen de la unidad C es OS
 El número de serie del volumen es: 008E-DBB0

 Directorio de C:\javatest

06/05/2014  22:00    <DIR>          .
06/05/2014  22:00    <DIR>          ..
06/05/2014  21:24               395 HelloWorld.class
06/05/2014  23:17               777 HelloWorld.jar
06/05/2014  21:23               163
06/05/2014  22:00                24 manifest.txt
               4 archivos          1.359 bytes
               2 dirs  35.861.430.272 bytes libres

C:\javatest>java -jar HelloWorld.jar

If all goes well, we will see a window that says Hello World.

Mensaje de alerta

If you use Windows Explorer, you can run with double click:

Explorador de archivos