Simple?
Say the jar name is exec.jar and is located in c:\java.
Go to :-
c:\
cd c:\java
java -jar exec.jar
This executes fine.
But why do I need to go into the directory where the jar exists. Say I am in the drive c: I can always do:-
java -classpath c:\java\exec.jar -jar exec.jar
Unable to access jarfile exec.jar
No it does not work.
Lets try set classpath=.;c:\java\exec.jar;
and then execute
java -jar exec.jar
Still the same error
Now lets try
java -jar c:\java\exec.jar
Works fine...
But why is the classpath not working.
The reason is provided in the java tool documentation from the Sun site.
"-jar Execute a program encapsulated in a JAR file.
.....
......
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored. "
The classpath option does not work with java -jar option.
ref: java tool documentation
Thus the options if you want to execute a jar file are:-
- Go to the directory of jar and execute the java -jar exec.jar
- From any other directory execute java -jar c:\java\exec.jar
- If you want to read it from the classpath, use the workaround by retrieving the name of the main class from mainifest and executing it:-
- java -classpath c:\java\exec.jar Main