0

Under Linux I am trying to run a jar file as follows:

java -jar plantuml.jar -testdot

while having CLASSPATH set to any of the following (the file is located at /home/user/plantuml.jar):

export CLASSPATH=/home/user
export CLASSPATH=/home/user/
export CLASSPATH=/home/user/plantuml.jar

In either case, no matter how I define CLASSPATH, the java command gives an error Unable to access jarfile plantuml.jar. What am I doing wrong here?

2
  • have you tried the -cp argument to java executable? Commented Jun 7, 2013 at 7:31
  • Are you sure that your jar is executeable in sense of Java? Does MANIFEST.MF have Main-Class entry? Classpat seems to be irrelevant in this case. Commented Jun 7, 2013 at 7:32

4 Answers 4

2

You have to supply the complete path after the parameter -jar. So for your example you have to call

java -jar /home/user/plantuml.jar -testdot

The $CLASSPATH is only evaluated to find additional files (classes/resources) but not the jar file defined in the command line.

Sign up to request clarification or add additional context in comments.

4 Comments

Isnt he able to use java -cp /home/user -jar plantuml.jar -testdot ?
@vikingsteve No this is not possible. The parameter after -jar must be a full path name.
I had the impression CLASSPATH can be used to set paths where java is looking for something, just as this is possible for any other programming language I have encountered so far. I will solve this problem by defining something in my .bashrc to avoid typing the full pathname everytime I want to use this command.
" where java is looking for something" - Not exactly. It's used by ClassLoaders to find resources by name. The parameter to the -jar option is not the same thing.
2
export CLASSPATH="/path/to/class_or_jar1":"/path/to/class_or_jar2":"${CLASSPATH}" 

Comments

0

Maybe you are missing name of the main class or path to the jar. Have you tried execute it:

java -jar full_path/plantuml.jar package.YourClass -testdot

Is your program depending on other classes? If yes you might want to add -cp parameter.

1 Comment

This is not the case in the OP problem. If the class name is named in the manifest and the jar is called with the -jar parameter there is no need to name the class name. Also the error message indicates that the jar file is not found.
0

The classpath is used to find classes when you refer to them by name. It's essentially a list of paths (directories AND jar/zip files) where the JVM needs to look for classes, or other resources when using methods like ClassLoader.getResourceAsStream().

The value passed to the -jar option on the command line is the file-path to the JAR file.

So, it won't find a jar file if you are only referring to the jar file by name. The JAR file path in the CLASSPATH is supposed to be a path element that 'contains' other resources.

What you need to do here, is either

  1. Provide the full path to the jar file when trying to execute the jar
  2. Set the classpath to the jar file's path, and run the java command giving the name of the main class you want to execute.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.