Monday, 6 October 2014

Compile and run a java program

This article details the commands to use to compile and run a java programs.

Compile with 'javac' 

  javac [options] [source file(s)]

   javac MyClass.java OnOtherClass.java

Note that 'javac' put the compiled file(s) in the same directory than their source file(s). By using the option '-d' you can specify a specific directory in which to put the compiled file(s). Note that if the specified directory does not exist then you will get a compilation error.

    javac -d myClassesDir MyClass.java

This command will compile the source file 'MyClass.java' and put the compiled file 'MyClass.class' under the directory 'myClassesDir'.

Run with 'java'


    java [options] class [args]

System property: You can create a system property during the invocation if a class with "-D[propertyName=propertyValue]". Note that there is not a space between '-D' and the property's declaration.

    java -DmyProperty=aValue MyClass

If a property's value contains a white space then you have to use double quotes:
    java -DmyProperty="a value" MyClass

It is then possible to access to this property and any system's properties from the source code with using: 'System.getProperty(String key)' : get a property's value ('key' is the property's name), 'System.getProperty(String key, String defaultValue)' : get a property's value, if the property is not found then this method returns the default value defined in the second parameter.

    import java.util.Properties
    ...
    // We can access to a property with the method "System.getProperty()":
    System.out.println(''myProperty = "+System.getProperty(''myProperty"));

    // Or we can access to a property with the class "java.util.Properties":
    Properies p = System.getProperties();
    System.out.println(''myProperty = "+p.getProperty(''myProperty"));

We run this program:
     java -DmyProperty="a value" MyClass

Output:
    myProperty = a value
    myProperty = a value

The option "classpath" 


The option -classpath or -cp is used in same way by the commands 'javac' and 'java'.
    (javac|java) -classpath [directory1:directory2:...:directoryN]
    javac -classpath /com/foo:/com/bar MyClass.java

Each directory is separated by a ':' on Unix or a ';' on Windows. You can specify an absolute or/and a relative directory(ies). The directory(ies) is/are searched in order, from the left to the right. If you do not specify the option 'classpath', by default the compiler search in the current directory. However once you use the option 'classpath' then the current directory is not searched. Therefore you have to include it by specifying a dot '.' if you want the compiler to search in it.

    javac -classpath /com/foo:/com/bar/:. MyClass.java

Note that if you specified 'classpath' during the compilation then you have to specify it when you run your class(es).

// If you do not specify dot JVM will still find 
// and compile the source file you specified (MyClass.java)
javac -classpath /com/foo:. MyClass.java

// Important: with the command line "java" you have to specify
// the dot otherwise JVM will not find the file you want to run ('MyClass')!
java /com/foo:. MyClass                                  

JAR files

Note that with the option 'classpath' you have to specify any jar file(s). Even if the jar file(s) is(are) located in a directory specified by the classpath. In the following example, in the classpath we specified the folder '/com/bar' as we need the classes contained in this folder. This folder contains also a 'jar' file 'anApp.jar' that we need. However the compiler is unable to find it unless we specify it explicitly: '/com/bar/anApp.jar':

    javac -classpath /com/bar:/com/bar/anApp.jar MyClass.java

    java -classpath /com/bar:/com/bar/anApp.jar MyClass