如何在Linux中设置CLASSPATH让java找到jar文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16978480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:09:33  来源:igfitidea点击:

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

javalinuxclasspath

提问by Alex

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

在 Linux 下,我试图运行一个 jar 文件,如下所示:

java -jar plantuml.jar -testdot

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

同时CLASSPATH设置为以下任何一项(文件位于/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 javacommand gives an error Unable to access jarfile plantuml.jar. What am I doing wrong here?

在任何一种情况下,无论我如何定义CLASSPATHjava命令都会给出错误Unable to access jarfile plantuml.jar。我在这里做错了什么?

采纳答案by Uwe Plonus

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

您必须在参数后提供完整路径-jar。所以对于你的例子,你必须打电话

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

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

$CLASSPATH只计算发现其他文件(类/资源),而不是在命令行中定义的jar文件。

回答by Rajesh J Advani

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().

当您按名称引用类时,类路径用于查找类。它本质上是一个路径列表(目录和 jar/zip 文件),JVM 在使用ClassLoader.getResourceAsStream().

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

传递给命令行上 -jar 选项的值是 JAR 文件的文件路径。

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.

因此,如果您仅按名称引用 jar 文件,它将找不到 jar 文件。CLASSPATH 中的 JAR 文件路径应该是“包含”其他资源的路径元素。

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.
  1. 尝试执行 jar 时提供 jar 文件的完整路径
  2. 将类路径设置为 jar 文件的路径,然后运行 ​​java 命令,给出要执行的主类的名称。

回答by Beata Gellner

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

也许您缺少主类的名称或 jar 的路径。你有没有试过执行它:

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.

你的程序是否依赖于其他类?如果是,您可能需要添加 -cp 参数。

回答by Soham Krishna Paul

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