Java From the Command Line

Overview

In several of our courses we encourage students to develop programs with an IDE (Eclipse or Visual Studio) because IDEs have many built-in features that make it easier to work with large programs. Eclipse in particular provides good support for Java, so it is reasonable enough to use it to develop Java programs. But it is perfectly possible to write, compile, and run Java programs with any text editor and the command-line tools that come with typical Java implementations, and sometimes it can make sense to do so, or to use a combination of an IDE and command-line tools. This document describes how to do this under Linux.

Situations in which you might find this useful include writing short throwaway programs, maklng very small changes to programs of any size, and running programs with command-line arguments or environment variables that should vary from execution to execution. You might even adopt a hybrid development strategy, developing the programs with an IDE and then running them from the command line (by opening a separate terminal window and navigating to the appropriate part of the IDE's workspace directory).

Writing Java programs

You can write Java programs with any text editor (program that lets you edit standard ASCII text files). Among UNIX enthusiasts, whose editor is better is a subject of perennial debate. Two popular and powerful (if not exactly novice-friendly) groups of editors are

Compiling Java programs

A Java compiler is installed on the department Linux machines as /usr/java/latest/bin/javac. (If everything is set up correctly in your account, this should be the same as just javac. Confirm this with which javac.) So you could compile, for example, program Hello.java by typing the following command.

javac Hello.java
This assumes that all the classes needed by Hello.java are either part of the standard Java libraries or in the current directory. If some of the classes referenced in Hello.java are in a JAR file, say MyLib.jar, you would need to type
javac -classpath .:MyLib.jar Hello.java
By default the .class files are created in the current directory, but you can use the -d option to specify a different location, e.g.
javac -d obj -classpath Hello.java
to put them in obj.

If you want to compile many files, especially if they are contained in multiple directories, as is typically the case when using packages, you could type something such as the following, which finds all .java files in pkg and compiles them, placing the results in obj.

javac -d obj `find pkg -name "*.java"`
(Notice that ` is the backquote character near the top left corner of most keyboards and not the single-quote character.)

Running Java programs

Unlike programs written in C++ and many other languages, Java programs do not compile to code that can be executed directly. Instead they compile to ``byte code'' (.class files) meant to be executed by a Java virtual machine. A JVM is installed on the department Linux machines as /usr/java/latest/bin/java (which again should be the same as just java if everything is set up right). So if you want to run the main method in class Hello, you would type the following command.

java Hello
To pass command-line arguments to main in Hello, type them at the end of the above command separated by spaces. Notice that arguments that contain spaces or other characters that mean something to your shell (e.g., the wildcard character ``*'') need to be enclosed in double or single codeDisplay. Again, the above command assumes that all the classes needed by Hello.java are either part of the standard Java libraries or in the current directory. If some of the classes referenced in Hello.java are in a JAR file, say MyLib.jar, you would need to type
java -classpath .:MyLib.jar Hello
You can abbreviate -classpath as -cp. If the classes are not in the current directory, you would need to type something such as the following (if the classes are in directory obj)
java -classpath obj Hello
If the class whose main method you want to run is part of a package, you would need to type something such as the following, which runs Hello in package pkg
java -classpath obj pkg.Hello
Notice that this assumes that Hello.class is in obj/pkg, as described briefly in the next section.

Java packages versus directories/folders

Java normally expects source code for package x.y.z to be stored in a directory (folder) x/y/z. Eclipse automatically creates an appropriate directory structure when you create a package; if you're working from the command line, you should do it yourself (e.g., with mkdir).



Berna Massingill
2014-08-11