Running Java Applications
Understanding the java command will save you from some frustrating errors.
While running Java program using command line/server, I always got confused about using -jar option and -cp option. We bundle the application in jar file using maven/gradle or any building tool. Now there may be some resource files which should be provided from outside of the application. This is very common scenario.
Now if you use
java -cp /path/to/resources -jar app.jarJava won't read classpath resources and will look for classpath mentioned in app.jar manifest file.
So, in such case,
java -cp /path/to/resources:/path/to/jar package.main-classnameNow, Java will search for the jar file and resources in classpath (separated by ; in windows and : in unix).
if you are not providing any -cp option, java checks System's CLASSPATH and uses the directories.
If there is no envrionment variable CLASSPATH set, then it checks the current directory
java -cp . package.main-classnameAny arguments provided after the main class name will be considered as program arguments
java -cp /path/to/all/resources package.main-classname arg1 arg2 arg3You can pass JVM arguments before the main class and after -cp option with -D as prefix
java -cp /path/to/resources -Dkey=value package.main-classname arg1 arg2Have a nice day :)
Last updated
Was this helpful?