File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to execute jar file from another directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to execute jar file from another directory" Watch "How to execute jar file from another directory" New topic
Author

How to execute jar file from another directory

rivka zam
Greenhorn

Joined: Feb 22, 2005
Posts: 24
hello all,

I have a jar file that is located in C:\JavaFiles\MyJar.jar
When I execute the jar from the directory where it is located
(i.e. java -jar MyJar.jar) it works.

When I execute the jar from any other directory - i get:"Unable to access jarfile MyJar.jar".

The jar file source directory was added to my classpath (on windows xp).

What am i doing wrong?
Is it possible to execute a jar file from a directory other than where it is located?

Thanks
Rivka
Jim Toy
Greenhorn

Joined: Oct 17, 2002
Posts: 14
Explicity add the jar to the classpath, not just the directory.

Or you can add the fully qualified jar to the classpath of the command line.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
Yes, you must add each jar file itself to the classpath, not just the directory contains the jar files.

Then type
C:\>java -jar C:\JavaFiles\MyJar.jar


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
paramu iyer
Greenhorn

Joined: May 13, 2006
Posts: 24
hi Marilyn

I tried this but I was not able to run the jar file from a different folder even after setting the classpath.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
Is MyJar.jar a jar you created or a third party jar? If it is someone else's jar that you're using, I think that there's nothing you can do. If it is your own jar, you might be able to change the manifest to include the directories/files that you need.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
This has nothing to do with the classpath. When you execute "java -jar <file>" you need to give it a valid file, whether it's a relative path or an absolute path. If you want to run it from a different directory that has a relative path that will be consistent then use that, otherwise you'll have to use an absolute path. For example, if you have your program setup such that there's a script in the directory "MyProgram" and your JAR files were all in "MyProgram\data\" then you would run it with "java -jar data\MyFile.jar" telling it to look in the data directory for MyFile.jar. If you wanted to use an absolute path it might look like "java -jar C:\Program Files\MyProgram\data\MyFile.jar" if you had your MyProgram directory under Program Files on the C drive.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to execute jar file from another directory