Java Commands - Development Tools in the Java SE Development Kit
Development Tools in the Java SE Development Kit
When the JDK is installed, various development tools are located under {JAVA_HOME}/bin.
- javac: Java compiler
- java: Java interpreter
- javadoc: generates Java API documentation(HTML Document)
- javap: Java decompiler
- appletviewer.exe: Java applet viewer
- jar: Java archiver
- jdb: Java debugger
- jshell command: interactive tool for Java code prototyping
The main commands are explained below.
javac Command
The javac command compiles Java code.
javac (option) [src]
option: command option
src: source file
The javac command compiles Java source code(.java files) and generates class files(.class files).
The available command options are as follows.
Main options of the javac command
| Option | Summary |
|---|---|
-encoding character code |
Character code used in the source code |
-sourcepath path |
Path where source files are stored |
-d path |
Output path for class files |
-classpath path |
Search path for related class files. When specifying multiple paths, separate them with a semicolon(Windows) or colon(Unix) |
--module-path path |
Module search path |
-verbose |
Display detailed messages |
-source version |
Specify the target code version |
For example, the following specifies the class file output location, classpath, and character code.
$ javac -d ~/develop -classpath .:~/develop -encoding UTF-8 HelloWorld.java
The -classpath option indicates the location(path) of class files or JAR files needed to compile the source. If setting it every time is inconvenient, you can specify it beforehand with the CLASSPATH environment variable.
java Command
The java command runs a Java program.
java (option) [clazz] (params)
option: command option
clazz: class to run
params: parameters
The java command loads and runs the specified class.
params are values passed to the Java program. They can be received as the args parameter of the main method. If there are multiple values, separate them with spaces.
The available command options are as follows.
Main options of the java command
| Option | Summary |
|---|---|
-classpath path |
Search target for related class files. When specifying multiple paths, separate them with a semicolon(Windows) or colon(Unix) |
–-module-path path |
Module search path |
-jar |
Run a .jar file |
-version |
Show the Java version |
For example, the following runs Hello.jar. It sets "devkuma" as a parameter.
$ java -jar Hello.jar "devkuma"
jar Command
The jar command creates a JAR file.
jar (option) [jfile] (clazz...)
option: command option
jfile : name of the jar file to create
clazz: class files to include(multiple values allowed)
JAR stands for Java ARchive and is a format that compresses class files in Zip format. When distributing Java applications, it is common to package them in JAR format.
Use the jar command to create a JAR file.
The available command options are as follows.
Main options of the jar command
| Option | Summary |
|---|---|
c |
Create a new JAR file |
u |
Update an existing JAR file |
x |
Extract the contents of a JAR file |
t |
Display the list of contents in a JAR file |
f |
Specify the target JAR file |
–-module-path path |
Module search path |
For example, the following creates Hello.jar based on prepared class files and a manifest file(MANIFEST.MF).
$ jar cvfm Hello.jar ./main/resource/com/devkuma/tutorial/META-INF/MANIFEST.MF ./main/src/com/devkuma/tutorial/*.class
javadoc Command
The javadoc command creates API documentation.
javadoc (option) [src]
option: command option
src : original source file for the documentation
The javadoc command generates API documentation from documentation comments described in source files. The API documentation published on the official site is also originally created with the javadoc command.
For example, the following is an example of documentation comments from the Math class included in the Java standard library.
/**
* The class {@code Math} contains methods for performing basic
* numeric operations such as the elementary exponential, logarithm,
* square root, and trigonometric functions.
... omitted ...
* @author unascribed
* @author Joseph D. Darcy ... author
* @since JDK1.0 ... target version
*/
public final class Math {
...omitted...
/**
* Returns the trigonometric sine of an angle. Special cases:
* <ul><li>If the argument is NaN or an infinity, then the
* result is NaN. ... method summary
... omitted ...
* @param a – an angle, in radians. ... argument
* @return the sine of the argument. ... return value
*/
public static double sin(double a) {
return StrictMath.sin(a); // default impl. delegates to StrictMath
}
...omitted...
}
The javadoc command can be run as follows. The following example creates documentation based on .java files in the /lang folder. The target is the /docs folder.
$ javadoc -d docs ~/develop/*.java
jshell Command
The jshell command starts an interactive shell environment.
jshell (option)
option: command option
The jshell command starts a REPL(Read-Eval-Print-Loop) environment for running Java code interactively from the command line. It was added in Java 9.
Before Java 9, using simple code required compiling and running it, but JShell lets you run it immediately.
The available command options are as follows.
Main options of the jshell command
| Option | Summary |
|---|---|
| –-classpath path | Search target for related class files. When specifying multiple paths, separate them with a semicolon(Windows) or colon(Unix) |
| –-module-path path | Module search target |
| –-enable-preview | Use preview features |
For example, the following starts JShell and runs simple code.
% jshell
| Welcome to JShell -- Version 17.0.4.1
| For an introduction type: /help intro
jshell> import java.util.ArrayList; (1) import
jshell> new ArrayList<String>(); (2) define a list
$2 ==> []
jshell> $2.add("안녕하세요."); (3) manipulate the list
$3 ==> true
jshell> $2.add("반갑습니다.");
$4 ==> true
jshell> $2.get(0);
$5 ==> "안녕하세요."
jshell> /exit
| Goodbye
The JShell prompt is jshell>. When using libraries, import classes with the import command just like in normal code(1).
The subsequent code(1 to 2) is implicitly treated as code under the main method. Variable declarations are also unnecessary because generated objects are assigned in order to automatic variables such as $1, $2, and so on. In (3), the ArrayList object created in (2) can be accessed through the variable $2.
To exit JShell, use the /exit meta command.