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:

    1. Run a Java program from a .class file
    2. Running a Java program from an executable jar file
    3. Specify splash screen
    4. Set system properties
    5. Specify memory size
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 command syntax:

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.

 

1. Run a Java program from a .class file

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 

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”.

 

2. Run a Java program from an executable JAR file

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 
 

3. Specify splash screen

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.

 

4. Set system properties

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
 

5. Specify memory size

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:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.