Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to execute jar file from another directory

 
rivka zam
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9068
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
paramu iyer
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9068
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic