In this Java tools tutorial, I will guide you how to use the java command provided in JDK (Java Development Kit) to execute Java programs.
Table of content:
You know, java is the Java application launcher tool which is used to execute programs written in Java programming language and compiled into bytecode class files. Its executable file can be found under JDK_HOME\bin directory (java.exe on Windows and java on Linux), so make sure you include this path to the PATH environment variable in order to invoke the program anywhere in command line prompt.
java [options] file.class [arguments...]
java [options] -jar file.jar [arguments... ]
The first syntax is for executing a class file, and the second one is for executing a JAR file.
Type java -help to consult the available options or browse Oracle’s Java documentation for detailed description and explanation of the options. The arguments, if specified, will be passed into the running program.
NOTES:
Following are common usage examples of the java tool in daily Java development.
If you have a source file called MyProgram.java and it is compiled into MyProgram.class file, type the following command:
java MyProgram
If the class MyProgram.java is declared in the package net.codejava, change the working directory so that it is parent of the net\codejava directory, then type:
java net.codejava.MyProgram
If we have a JavaMail-based program that depends on mail.jar library. Assuming the jar file is at the same directory as the class file, type:
java -cp mail.jar;. PlainTextEmailSender
NOTES: There must be a dot (.) after the semicolon.
If the jar file is inside a directory called lib:
java -cp lib/mail.jar;. PlainTextEmailSender
If the program depends on more than one jar files:
java -cp mail.jar;anotherlib.jar;. MyProgram
We can use wildcard character to refer to all jar files:
java -cp *;. MyProgramOr:
java -cp lib/*;. MyProgram
The following example passes two arguments “code” and “java” into the MyProgram:
java MyProgram code java
If the argument has spaces, we must enclose it in double quotes, for example:
java MyProgram "code java" 2013
That will pass two arguments “code java” and “2013”.
java -jar MyApp.jar
Here the MyApp.jar file must define the main class in the header Main-Class of its manifest file MANIFEST.MF. The header is usually created by the jar tool.
NOTES: if the jar file depends on other jar files, the reference jar files must be specified in the header Class-Path of the jar’s manifest file. The -cp option will be ignored when using -jar flag.
Pass two arguments “code” and “java” to the program:
java -jar MyApp.jar code java
If the argument contains space, enclose it in double quotes like this:
java -jar MyApp.jar "code java" 2013
For Swing-based application, we can use the -splash:imagePath flag to show a splash screen at program’s startup. For example:
java -splash:SplashScreen.png MyProgram
Here the image SplashScreen.png is loaded as splash screen at startup.
We can use the -Dproperty=value option to specify a system property when running a program:
java -Dupload.dir=D:\Uploads MyProgram
if the property’s value contains spaces, enclose it in double quotes:
java -Dupload.dir="D:\My Uploads" MyProgram
java -Dupload.dir=D:\Uploads -Ddownload.dir=D:\Downloads MyProgram
We can override the predefined system properties. For example, the following command overrides the system property java.io.tmpdir:
java -Djava.io.tmpdir=E:\Temp MyProgram
When launching a Java program, we can specify initial size and maximum size of the heap memory:
The size is measured in bytes. It must be multiple of 1024 and is greater than 1MB for initial size and 2MB for maximum size. Append k or K to indicate kilobytes; m or M to indicate megabytes. For example, the following command launches a program with initial heap size 32MB and maximum heap size 1024MB:
java -Xms32M -Xmx1024M MyProgram
Other Java Tools Tutorials: