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

后端 未结 4 909
误落风尘
误落风尘 2021-01-07 14:40

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

java -jar plantuml.jar -testdot

while having CLASSPATH set to any of th

相关标签:
4条回答
  • 2021-01-07 15:12

    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.
    0 讨论(0)
  • 2021-01-07 15:23
    export CLASSPATH="/path/to/class_or_jar1":"/path/to/class_or_jar2":"${CLASSPATH}" 
    
    0 讨论(0)
  • 2021-01-07 15:35

    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.

    0 讨论(0)
  • 2021-01-07 15:35

    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.

    0 讨论(0)
提交回复
热议问题