Using Java via Command Line

by Sophie Engle on Aug 2013

For some classes, you will need to test your Java code on the lab computers. You can log in remotely, and then run your Java code via the command-line. You can find some basic information about the java command-line tool in the JDK documentation.

Lab Setup

You need to set your $CLASSPATH variable on the lab computers, or you will be stuck specifying long classpath parameters every time you run java. This becomes especially important when you start using third-party libraries and must include multiple jar files.

To do this, you will need to edit a configuration file in your home account. See the Using Linux via Command Line guide if you are unfamiliar using a text editor via the command line prior to completing this guide.

Change to your home directory (run cd ~ at the prompt) and open the .bash_profile file. If it does not exist yet, go ahead and create it. Add the following line to the file:

export CLASSPATH=".:.*"

Some classes also provide some third-party libraries for you on the lab computers. For example, the CS 212 course places third-party libraries at /home/public/cs212/libraries/ on the lab computers. You can also add that directory (or any other) to your $CLASSPATH variable by adding the additional line:

export CLASSPATH="$CLASSPATH:/home/public/cs212/libraries/*"

Save the file and exit. Then, run the following command:

. .bash_profile

Make sure you include the . dot followed by a space before the .bash_profile above.

To test that everything is setup, try the following command:

cat ~/.bash_profile

You should see somewhere in the output the following lines (there may be other lines as well):

export CLASSPATH=".:./*"
export CLASSPATH="$CLASSPATH:/home/public/cs212/libraries/*"

If that looks correct, run the following command as well:

echo $CLASSPATH

If it is setup correctly, you will see something similar to the following:

.:./*:/home/public/cs212/libraries/*

You should see all of the directories you specified in your $CLASSPATH variable above. (It is okay if there are extra directories in the output.)

Compile

Make sure you are currently in the directory with your source code files. Remember, Java source code is always stored a *.java file. You first need to compile these files using Java. To do this, run the following command:

javac *.java

Now, when you ls your directory, you should notice you also have *.class files. Those are the files necessary to execute your Java code.

A common error if you do not have your classpath setup properly will be similar to:

error: package org.junit does not exist

In this case, javac cannot figure out where the third-party libraries like junit are located. Double-check that your classpath is setup correctly.

Execute

To run your code, you need to know which class has the main() method. To execute your code, run the following command:

java ClassName

Replace ClassName with the classname (no file extension) that contains your main() method. If you have not setup your classpath yet, you need to also provide a -cp "." parameter to tell the java tool to look for your *.class files in the current directory.

A common error if you do not have your classpath setup properly will be similar to:

Exception in thread "main" java.lang.NoClassDefFoundError:

In this case, javac cannot figure out where the class files are located. Double-check that your classpath is setup correctly and that your code is properly compiled.

Arguments

If you want to provide command-line arguments to your program, just include those arguments after the class name:

java ClassName arg1 arg2 arg3 ...

Replace arg1 arg2 arg3 ... with your command-line arguments (separated by spaces). For example, suppose the class Driver takes a filename as an argument. To execute your code with the argument, run the following command:

java Driver hello.txt

If you must specify a file name with a space inside it, use quotes around the filename:

java Driver "Hello World.txt"

Jar Files

If you have already packaged your *.class files in a *.jar file, you can skip compiling your code with javac. To execute your code, run the following command:

java -cp filename.jar ClassName arguments

Replace filename.jar with the name of your jar file (assuming it is in the current directory). For this to work, your *.jar file must contain ALL of the necessary classes for your program to run.

JUnit Tests

You can run JUnit tests via the command-line on the lab computers. You need to make sure your classpath is setup (see above) first. To run your tests, you can use the following command:

java org.junit.runner.JUnitCore ClassName

Replace ClassName with the name of the class with the JUnit tests. For example, suppose the JUnit tests are located in a file named Tester.java. After you have compiled the source code, you can use the following command to run those tests:

java org.junit.runner.JUnitCore Tester

You will get some basic text output telling you how many tests you passed or failed.