Java Shell Commands: javac java jar javadoc

By Xah Lee. Date: . Last updated: .

Purpose

javac
Compile Java source code.
java
Run Java program.
jar
Pack Java class files.
javadoc
Generate Java doc.

Type man javac to read its manual page.

Following are examples of using these commands.

Compile Java File

javac Aa.java
Compile a source code file “Aa.java”. 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