IMPORTANT
- All of the following commands assumes the Java compiler (javac)
and Java Virtual Machine (java) locations are known to the
shell/Operating System.
Compiling Java Source From the Command Line
- How to compile a single source file:
- Open a shell on the machine you wish to compile on.
- Change directory (cd) to the directory that contains the source file(s) (.java) that you want to compile.
- Enter the following command (modifying the source file name as appropriate):
javac YourSourceFile.java
- How to compile multiple source files by name:
- Open a shell on the machine you wish to compile on.
- Change directory (cd) to the directory that contains the source file(s) (.java) that you want to compile.
- Enter the following command (modifying the source names as appropriate)
javac FileOne.java FileTwo.java FileThree.java
- How to compile multiple
source files using wildcards - if you have all of the source files for
a single program in the same directory you can compile them all
with a single command without having to specify all of their names, to
do so:
- Open a shell on the machine you wish to compile on.
- Change directory (cd) to the directory that contains the source file(s) (.java) that you want to compile.
- Enter the following command:
javac *.java
- How to compile using compiler flags:
- Open a shell on the machine you wish to compile on.
- Change directory (cd) to the directory that contains the source file(s) (.java) that you want to compile.
- Enter the following command (note that you can compile single or multiple files by name or wildcards):
javac -Xlint:unchecked SourceFile.java
Running Java Programs From the Command Line
- How to run normally (without assertion statements enabled):
- Open a shell on the machine you wish to run on.
- Change directory (cd) to the directory that contains the program you want to run.
- Make sure the program has been compiled
- Enter the following command (note that the file name specified must be a .class file that contains a main method):
java YourClassName
- How to run with assertion statements enabled:
- Open a shell on the machine you wish to run on.
- Change directory (cd) to the directory that contains the program you want to run.
- Make sure the program has been compiled
- Enter the following command (note that the file name specified must be a .class file that contains a main method):
java -ea YourClassName