• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How to execute jar file from another directory

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic