1

I'm trying to run a jar that is located at /Library/Java/jack.jar, this works:

java -jar /Library/Java/jack.jar

However, I want to run it without the path, relying on the class path. The default class path includes /Library/Java, but this fails with "Unable to access jarfile":

java -jar jack.jar

Why? What's more, this also fails (same error):

java -cp /Library/Java -jar jack.jar

What's the problem/what am I doing wrong?
Thanks.

Jack Sleight
  • 211
  • 2
  • 8

2 Answers2

3

From http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/java.html:

-jar
When you use this option, the JAR file is the source 
of all user classes, and other user class path settings
are ignored.
mihi
  • 890
  • 1
  • 7
  • 14
2

The jar file can not be executed this way. You need to provide the full path of the jar file. the class path -cp classpath specifies a list of directories, JAR archives, and ZIP archives to search for class files needed to run your java program. However, the jar file to be executed should be accessed using full path. After the jar file is run, the classpath can be used to locate more classes and libraries.

By the way, this is not the right site to ask such a question.

Khaled
  • 36,533
  • 8
  • 72
  • 99
  • Ahhhh OK. So there's no way Java can be configured to search a default environment defined directory for JARs to execute using the "java -jar ..." command? The jarfile argument always has to be explicit? – Jack Sleight Dec 21 '11 at 15:23
  • RE the right site: Sorry, I wasn't sure, thought this counted as server configuration. – Jack Sleight Dec 21 '11 at 15:30
  • @JackSleight: AFAIK, yes the jar file path should be set explicitly. – Khaled Dec 21 '11 at 15:32
  • Yes, this problem got me once also, trying to run java from a powershell script: http://thegreenoak.blogspot.com/2009/07/run-java-process-from-windows.html – djangofan Jan 03 '12 at 19:41