aspose file tools*
The moose likes Java in General and the fly likes How do i find out the relative path for my jar? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How do i find out the relative path for my jar?" Watch "How do i find out the relative path for my jar?" New topic
Author

How do i find out the relative path for my jar?

Purushotham Kudva
Greenhorn

Joined: Mar 22, 2012
Posts: 7
Hi. I am new to java. I have a batch file that calls a .jar if i change the location of the batch file and run i get an error that says unable to read the .jar file. i if i use cd.. or change the directory then the batch file runs. please help me
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
We need more information. What directory is the jar in? What directory is the batch file in? How are you running the program (specific command line). Otherwise we're just shooting in the dark, telling you the same things the instructions for these operations told you in the first place.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Purushotham Kudva wrote:Hi. I am new to java. I have a batch file that calls a .jar if i change the location of the batch file and run i get an error that says unable to read the .jar file. i if i use cd.. or change the directory then the batch file runs. please help me


This really has nothing to do with Java. It's basic OS/FS/command shell stuff. Just like almost any command that accepts a file name for an arg, you either have to specify the full path, or the file has to exist in the given path relative to the "working directory", whatever that happens to mean in a particular context.
Purushotham Kudva
Greenhorn

Joined: Mar 22, 2012
Posts: 7
Hi Ralph,

i have my .jar in the location D:\BIP\RR_Jar\ and the batch file in the location D:\BIP\Batches\

with this folder structure the batch is running successfully. Below is the result.

D:\BIP>java -Xms512m -Xmx512m -jar RR_Jar\NOPARAM_PDF.jar "/FBC Reports/EOM Repo
rts/Byo Corp/Byo Credit gr/Byo Credit gr.xdo" "Reports\EOM\Byo Corp\Byo Credit
gr.pdf"

D:\BIP>PAUSE
Press any key to continue . . .

Now if i change the location of batch from D:\BIP\Batches\ to D:\BIP\Batches\EOD\ it fails saying the below.

D:\BIP\Batches>java -Xms512m -Xmx512m -jar RR_Jar\NOPARAM_PDF.jar "/FBC Reports/
EOM Reports/Byo Corp/Byo Credit gr/Byo Credit gr.xdo" "Reports\EOM\Byo Corp\By
o Credit gr.pdf"
Unable to access jarfile RR_Jar\NOPARAM_PDF.jar

D:\BIP\Batches>PAUSE
Press any key to continue . . .

so i want to know how the system is picking up D:\BIP>java and D:\BIP\Batches>java location when i run it from a different folder.

My batch file contents are

java -Xms512m -Xmx512m -jar RR_Jar\NOPARAM_PDF.jar "/FBC Reports/EOM Reports/Byo Corp/Byo Credit gr/Byo Credit gr.xdo" "Reports\EOM\Byo Corp\Byo Credit gr.pdf"


Purushotham Kudva
Greenhorn

Joined: Mar 22, 2012
Posts: 7
Also i am just double clicking on the batch file
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

As I already told you: In one case, RR_Jar\NOPARAM_PDF.jar exists relative to the path where the batch file lives. In the other case, it does not. In your batch file, before calling java, execute dir and notice how in one case it shows RR_Jar and in the other case it does not.

Again, this has nothing to do with Java.
Purushotham Kudva
Greenhorn

Joined: Mar 22, 2012
Posts: 7
Sorry Jeff. Can you please tell me how to set relative path for any jar in generic
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Purushotham Kudva wrote:Sorry Jeff. Can you please tell me how to set relative path for any jar in generic


I don't know how else to explain it to you. This has nothing to do with Java or jar files in particular.

When you execute a command like

some_command some_path_to_file


there is a notion of a current directory. If you are using a relative path, then that path must exist relative to that current directory.

So whatever your current directory is when you execute the batch file, you have to know where the jar file will be relative to that directory. If you have to go back up to get to a common parent before going down the appropriate branch of the directory tree, you can use "dot-dot". For instance C:\a\b\c\..\..\x indicates location C:\a\x.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18903
    
    8

You can tell what your current directory is, because it's in the prompt where you type your commands. You can also change the current directory using the "cd" command.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Jeff Verdegan wrote:
Again, this has nothing to do with Java.


I disagree. S/He's trying to run a java program, and one has to understand how the classes are found (and not found) to do that. It is a place where the shell and the java environment have to cooperate a little, and the explanations I've seen for it are confusing. Too many examples, not enough explanation of principle.

I am short of time this evening, so I am just going to get down a few principles that you should know and point you to some things you might be able to look up yourself. I do not know if I will get back in a few hours to solve the specific problem or not, but I think this is more important.

The directory path to a java class is determined by its package; you can put class files on a disk in a directory structure and run them from there IF your JVM has the root directory of that structure on its classpath.

So if you're running a class in a package named com.iona.math, and the class is named Matrix, then you can put Matrix.class into a directory named math in a directory named iona in a directory named com (on Windows, com\iona\math\Matrix.class). The next question is, where does com need to go?

Often the 'current directory', a concept common to most environments, is on the classpath; if your current directory is c:\test\, and com is in that directory, and "." (the symbol for the current directory) is in the class path, then you can run your program and it can import and use Matrix.

If you have a jar file, then realize that a jar file is a specific kind of zip file, and zip files store directory structures as well as file names. So if you create your jar to have relative paths so that Matrix is in com/iona/math within the jar file (and you have remembered to NOT store it with the full path in front of com), then the classpath can contain the jar file and your program will have access to Matrix.

Note that, if you specify the jar file as a relative name, then your current directory will have to be in the right relative position; if you are, again, in c:\test\, and you want to say "java -jar MathStuff.jar", then MathStuff.jar has to be in the current directory. If you say java -jar x\MathStuff.jar, then it has to be in x in test, and you can also say c:\x\MathStuff.jar so it won't matter what your current directory is.

Now, once you understand all that, you can look up manifest files. A manifest.mf file is a special file you can put in a jar to help the JVM decide how to run it. One of the things you specify in a manifest file is the main class for the program, i.e., the class containing 'main(String[] args)'. You can package your stuff in a jar, including a manifest in the root of the jar, and (on windows at least) double-click on the jar to run it. If you have other libraries you need, there is a way to specify THEM in the jar, by specifying a classpath in the jar.

Eclipse will create such a jar for you; the options for it are a bit tortuous, even for one familiar with it, but once you get it created it's very easy to create it again.

I will try to get back to this in 3-4 hours to see if we can solve the specific problem, but these are the principles to start with, and you should read up on manifest files if you don't know them already. I can say that, when you changed the location of the batch file, you probably then changed your current directory down to EOD, and the command line phrase "-jar RR_Jar\NOPARAM_PDF.jar" can't find that jar because it is looking for RR_Jar relative to its current directory, which no longer contains RR_Jar.

Will try to get back tonight.

rc
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ralph Cook wrote:
Jeff Verdegan wrote:
Again, this has nothing to do with Java.


I disagree. S/He's trying to run a java program, and one has to understand how the classes are found (and not found) to do that.


No, that would be the case if it were a classpath issue.

This has to do with Java only to the extent that java.exe follows the same rules as nearly every other command we run from a *n*x shell or Windows cmd shell in terms of how it interprets relative paths. He's running java -jar from a batch file, and he's specifying the jar using a relative path, and when he changes directories before launching the batch file, that path no longer exists relative to the batch's notion of "current directory". Nothing to do with classpath, and not at all particular to Java. He'd have the same issues running dir whatever.jar.

The directory path to a java class is determined by its package;


Not relevant here. Nor is anything else relating to classpath.
Purushotham Kudva
Greenhorn

Joined: Mar 22, 2012
Posts: 7
thanks Guys...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do i find out the relative path for my jar?