Java Shell Commands: javac java jar javadoc

By Xah Lee. Date: . Last updated: .


The purposes of these tools are:

Type man javac to read its manual page.

Following are examples of using these commands.

Compile Java File

javac → compile a source code file “”. It will generate a file named “Aa.class”. That's the compiled file.

Note: by convention, Java class file name starts with capital letter.

Run Java File

java Aa → run the class file “Aa.class”, starting with the class's method named “main”. (Note: do not add the “.class” suffix in the command line.)

Note: Class name and its corresponding file name have a sameness relation enfored by Java. Each file has class, and the class name is the same as file name (without the “.class” suffix.)

[see Java: Package]

java ff/Aa → run the class file “Aa.class” in directory “ff”.

Note: directory represents a package. A package name has a directory of the same name, and is enforced by Java. So, a package “hh” will be in directory “hh”, and a package “hh.ii” will be in directory “hh/ii”, etc. In the directory, are class files.

java -cp classpaths class_name → run the program class_name, and search classes in classpaths. The classpaths can be relative to the current dir, and multiple class paths are separated by colon :. By default, java search class files in current directory. (and also by environment variable CLASSPATH)

java -jar jar_file_path → run the jar file.

Classpath can contain both compiled java class file or jar file.

# run a java program
# search class files in current directory and gg.jar
# start program with package hh and class Jj
java -cp .:gg.jar hh.Jj

jar files

Java: working with jar file