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:

    • A Java class must have the public static void main(String[] args) method in order to be executed by the Java launcher.
    • An executable JAR file must specify the startup class in by the Main-Class header in its manifest file.

Following are common usage examples of the java tool in daily Java development.

 

1. Run a Java program from a .class file

  • Run a simple class:

If you have a source file called MyProgram.java and it is compiled into MyProgram.class file, type the following command:

java MyProgram 
  • Run a class which is declared in a package:

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 
  • Run a class which has dependencies on jar files:

    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 *;. MyProgram
    Or:
    java -cp lib/*;. MyProgram

     

  • Passing arguments to the program:

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

  • Run a standalone 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.

  • Passing arguments:

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:

  • Specify a single property:
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
  • Specify multiple properties:
java -Dupload.dir=D:\Uploads -Ddownload.dir=D:\Downloads MyProgram 
  • Override predefined property:

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:

    • -Xms<size>: specifies initial heap size
    • -Xmx<size>: specifies maximum heap size.

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 began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#6Antonio2017-09-25 09:28
No lixux trocar ; por :
Quote
#5Nam2016-09-15 19:48
Quoting Abhishek:
how to i pass arguments as well as increase heap size while running a jar file?

java -Xms32M -Xmx1024M -jar MyProgram.jar
Quote
#4Abhishek2016-09-15 03:32
how to i pass arguments as well as increase heap size while running a jar file?
Quote
#3Davin2015-11-02 14:04
I thought maybe there was a way to execute more than one class file at the same time using command line just as you can compile more than one java file using the javac command. Thanks, learnt some new things that will be useful to my computer programming website.www.cscprogrammingtutorials.com
Quote
#2Edwin2015-11-02 14:03
Thanks. Was very useful
Quote