How to set CLASSPATH in Linux to let java find jar file?

大兔子大兔子 提交于 2019-12-19 04:57:15

问题


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?


回答1:


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.




回答2:


export CLASSPATH="/path/to/class_or_jar1":"/path/to/class_or_jar2":"${CLASSPATH}" 



回答3:


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.




回答4:


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.


来源:https://stackoverflow.com/questions/16978480/how-to-set-classpath-in-linux-to-let-java-find-jar-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!