Skip to content


Compiling Java Code

Java is a very popular language. Here is an introduction on how to compile and run Java programs. The first step is to download and install the latest Java Development Kit (JDK), which allows you to compile and run java code. Note that there is a Java Runtime Environment (JRE) which only allows you to run applications made in Java, you have to download the JDK which contains the development and runtime environments. You can download it from here.

The next step is to write a simple Java program. Using any plain text editor, write the following code:

class mainClass
{
    public static void main(String args[])
    {
    System.out.println("Hello there...");
    }
}

The filename has to be the same as the name of the class defined in the code. In this case, the class that I created is called mainClass. Also, the filename needs to have a .java extension. Therefore, the file has to be saved under the name of mainClass.java. Now you can compile the code using javac which is the Java compiler.

javac *.java

If you need to include some other folders with pre-compiled .class files or other libraries in .jar format, you need to use the -classpath argument. Note that for each .jar you need to specify the complete path. Each inclusion is separated with a semicolon (;). Remember to include the current directory by adding a single dot (.).

javac -classpath .;"c:\path\to\classFiles";"c:\path\to\lib1.jar" *.java

Now the code is ready to be run. Just call java and the name of the class that contains the Main function, which in this case is mainClass.

java mainClass

You can also pack your application into a Jar file, which basically is a compressed file containing the machine code you already compiled and a manifest file. The manifest file tells extra information about the application, in particular which one is the main class to execute. Let’s create this file. Open a text editor and write the following:


Main-Class: mainClass
Manifest-Version: 1.0

As you can see, you have to define which is the Main-Class that is going to be executed when the Jar file is run. Save this file as Manifest.txt. Although the second line itself is optional, it is important to add it (or even just a blank line) below the Main-Class definition because the Main-Class line needs to contain an end of line character in order to work properly. If you do not add an end of line character at the end of the Main-Class line you will receive the following error when you try to run your Jar file:

Failed to load Main-Class manifest attribute from main.jar

Jar is the application that allows you to pack your application. The c argument means that you are creating a Jar file. The f argument allows you to give a filename instead of sending the output to the console. The m argument means that you are telling the program where the manifest file is. Then, you need to provide the jar filename and the manifest filename. In this case, I used the main.jar filename but you can use any name as long as it has a .jar extension. The manifest filename can be anything you want, in this case I used Manifest.txt which was created previously. The last argument tells the program which files to include in the Jar package, in this case I simply used all the files that end in .class which is general enough for this introduction.

jar cfm main.jar Manifest.txt *.class

Now you have your application packed in a Jar file. If you want to run it, you only need to write this:

java -jar main.jar

Now you are able to compile and run Java programs.

class myfirstjavaprog
{
        public static void main(String args[])
        {
           System.out.println("Hello World!");
        }
}

Posted in Programming.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.