views:

746

answers:

5

I have a .jar file that I would like to be able to call without having to use a full file path to its location.

For example, if the .jar file is located at: /some/path/to/thearchive.jar

I'd like to be able to run it with:

java -jar thearchive.jar

instead of:

java -jar /some/path/to/thearchive.jar

when I'm elsewhere in the directory tree. In my specific case, I'm running a Mac with OS X 10.5.7 installed. Java version "1.5.0_16". I tried adding "/some/path/to" to PATH, JAVA_HOME and CLASSPATH, but that didn't work.

So, how do I setup to run a .jar from the command line without having to use its full path?

UPDATE: Another item to deal with would be arguments. For example:

java -jar /some/path/to/thearchive.jar arg1 arg2

This can have an effect on the way the question is dealt with as mentioned in the answers below.

+1  A: 

You can add a variable to hold the directory:

export JARDIR=/some/path/to
java -jar $JARDIR/thearchive.jar

I'm not sure you can do it from environment variables implicitly.

Brabster
@Brabster - good idea - you could also set the environment variable to "/some/path/to/thearchive.jar" and "java -jar $JAR"
jabbie
This works a little better than the alias when you need to pass additional arguments to it. I'm using a full path to the .jar as followed up by @jabbie and calling with "java -jar $JAR" then passing the arguments. This is working as expected.
anotherAlan
+1  A: 

I don't believe so. If you have the jar specified in your CLASSPATH you could just call java with the main class specified. (i.e java com.test.Main) Alternatively you could create an alias in you shell to execute the command

alias execJar="java -jar /some/path/to/thearchive.jar"

Or another alternative is to create a wrapper script to execute it.

jabbie
This answer is helpful, but it runs into issues with passing additional arguments. As long as you don't need to pass additional arguments (which I didn't list in my initial question), this should work fine.
anotherAlan
@stimulatingpixels You can pass additional arguments to an aliased command, or if you need them to be inserted at an arbitrary location create a function instead. A wrapper script can also be written to pass through the additional arguments.
jabbie
+3  A: 

No you can't.

Running a jar with -jar does not involve any kind of classpath mechanism since the jar file is the classpath.

Alternatively use a shell alias to launch the jar or a small script file.

Robert Munteanu
+3  A: 

According to Sun:

java -jar app.jar

To run the application from jar file that is in other directory, we need to specify the path of that directory as below: java -jar path/app.jar

where path is the directory path at which this app.jar resides.

So either out the path in a "standard" environment variable or define a wrapper which would be in your PATH

VonC
as described in @Brabster's answer and @jabbie's follow up, I'm exporting the full path to the .jar file like: export THEJAR=/some/path/to/thearchive.jarand running it via: java -jar $THEJAR This allows me to also send arguments as necessary, like: java -jar $THEJAR arg1 arg2And everything is working as expected.
anotherAlan
A: 

The Java system itself does not give you a way to specify something like JAR_PATH (a list of places to look for jar files). The other answers given use the MAC/Unix shell capabilities:

  1. Setting an environment variable
  2. Setting an alias
  3. Possibly using a symbolic link (to the file or to the directory).

What might be helpful is to find out why specifying the entire path is a problem. That may guide us as to which answer is best or possibly find a completely different solution to your problem.

Kathy Van Stone
It is really just a matter of convenience. One of the specific example is Sun's Multi-Schema XML Validator (MSV) which I use to validate XML files against schemas via the command line. When I'm in a terminal at the directory where the .xml and .xsd files are located it's nicer to be able to do something like "java -jar $MSV schema.xsd doc.xml" than "java -jar /path/to/the/real/msv.jar schema.xsd doc.xml".
anotherAlan
In that case using an environment variable or alias will probably solve your problem.
Kathy Van Stone