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: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
java -cp mail.jar;. PlainTextEmailSenderNOTES: There must be a dot (.) after the semicolon.If the jar file is inside a directory called lib:
java -cp lib/mail.jar;. PlainTextEmailSenderIf the program depends on more than one jar files:
java -cp mail.jar;anotherlib.jar;. MyProgramWe 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
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
java -Xms32M -Xmx1024M MyProgramOther Java Tools Tutorials: