在linux中添加类路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/580860/
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-03 17:02:19  来源:igfitidea点击:

adding classpath in linux

javalinuxclasspath

提问by

export CLASSPATH=.;../somejar.jar;../mysql-connector-java-5.1.6-bin.jar
java -Xmx500m folder.subfolder../dit1/some.xml
cd ..

is the above statement for setting the classpath to already existing classpath in linux is correct or not

上面将类路径设置为 linux 中已经存在的类路径的说法是否正确

回答by Yann Ramin

It's always advised to never destructively destroy an existing classpath unless you have a good reason.

除非有充分的理由,否则始终建议不要破坏性地破坏现有的类路径。

The following line preserves the existing classpath and adds onto it.

以下行保留现有的类路径并添加到其中。

export CLASSPATH="$CLASSPATH:foo.jar:../bar.jar"

回答by David Hanak

Paths under linux are separated by colons (:), not semi-colons (;), as theatrus correctly used it in his example. I believe Java respects this convention.

linux 下的路径由冒号 ( :)分隔,而不是分号 ( ;),因为 theatrus 在他的示例中正确使用了它。我相信 Java 尊重这个约定。

Edit

编辑

Alternatively to what andy suggested, you may use the following form (which sets CLASSPATH for the duration of the command):

作为andy 建议的替代方案,您可以使用以下形式(在命令期间设置 CLASSPATH):

CLASSPATH=".:../somejar.jar:../mysql-connector-java-5.1.6-bin.jar" java -Xmx500m ...

whichever is more convenient to you.

哪个对您来说更方便。

回答by flybywire

I don't like setting CLASSPATH. CLASSPATH is a global variable and as such it is evil:

我不喜欢设置 CLASSPATH。CLASSPATH 是一个全局变量,因此它是邪恶的:

  • If you modify it in one script, suddenly some java programs will stop working.
  • If you put there the libraries for all the things which you run, and it gets cluttered.
  • You get conflicts if two different applications use different versions of the same library.
  • There is no performance gain as libraries in the CLASSPATH are not shared - just their name is shared.
  • If you put the dot (.) or any other relative path in the CLASSPATH that means a different thing in each place - that will cause confusion, for sure.
  • 如果你在一个脚本中修改它,突然一些java程序会停止工作。
  • 如果你把你运行的所有东西的库放在那里,它就会变得混乱。
  • 如果两个不同的应用程序使用同一库的不同版本,则会发生冲突。
  • 没有性能提升,因为 CLASSPATH 中的库不是共享的 - 只是它们的名称是共享的。
  • 如果您将点 (.) 或任何其他相对路径放在 CLASSPATH 中,这在每个地方都意味着不同的东西 - 这肯定会引起混淆。

Therefore the preferred way is to set the classpath per each run of the jvm, for example:

因此,首选方法是在每次运行 jvm 时设置类路径,例如:

java -Xmx500m -cp ".:../somejar.jar:../mysql-connector-java-5.1.6-bin.jar"    "folder.subfolder../dit1/some.xml

If it gets long the standard procedure is to wrap it in a bash or batch script to save typing.

如果它变长,标准程序是将它包装在 bash 或批处理脚本中以节省输入。

回答by Chui

Important difference between setting Classpath in Windows and Linux is path separator which is ";" (semi-colon) in Windows and ":" (colon) in Linux. Also %PATH%is used to represent value of existing path variable in Windows while ${PATH}is used for same purpose in Linux (in the bash shell). Here is the way to setup classpath in Linux:

在 Windows 和 Linux 中设置 Classpath 的重要区别在于路径分隔符是“;” (分号)在 Windows 中,“:”(冒号)在 Linux 中。也%PATH%用于表示 Windows 中现有路径变量的值,而${PATH}在 Linux 中(在 bash shell 中)用于相同目的。以下是在 Linux 中设置类路径的方法:

export CLASSPATH=${CLASSPATH}:/new/path

but as such Classpath is very tricky and you may wonder why your program is not working even after setting correct Classpath. Things to note:

但因此类路径非常棘手,您可能想知道为什么即使设置了正确的类路径,您的程序也无法运行。注意事项:

  1. -cpoptions overrides CLASSPATHenvironment variable.
  2. Classpath defined in Manifest file overrides both -cpand CLASSPATHenvorinment variable.
  1. -cp选项覆盖CLASSPATH环境变量。
  2. 清单文件中定义的类路径覆盖-cpCLASSPATH环境变量。

Reference: How Classpath works in Java.

参考:Classpath 如何在 Java 中工作

回答by Adam Winter

For linux users, and to sum up and add to what others have said here, you should know the following:

对于linux用户,总结并补充其他人在这里所说的,你应该知道以下几点:

  1. Global variables are not evil. $CLASSPATH is specifically what Java uses to look through multiple directories to find all the different classes it needs for your script (unless you explicitly tell it otherwise with the -cp override).

  2. The colon (":") character separates the different directories. There is only one $CLASSPATH and it has all the directories in it. So, when you run "export CLASSPATH=...." you want to include the current value "$CLASSPATH" in order to append to it. For example:

    export CLASSPATH=.
    export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.12.jar
    

    In the first line above, you start CLASSPATH out with just a simple 'dot' which is the path to your current working directory. With that, whenever you run java it will look in the current working directory (the one you're in) for classes. In the second line above, $CLASSPATH grabs the value that you previously entered (.) and appends the path to a mysql dirver. Now, java will look for the driver AND for your classes.

  3. echo $CLASSPATH
    

    is super handy, and what it returns should read like a colon-separated list of all the directories you want java looking in for what it needs to run your script.

  4. Tomcat does not use CLASSPATH. Read what to do about that here: https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html

  1. 全局变量不是邪恶的。$CLASSPATH 特别是 Java 用来查看多个目录以查找脚本所需的所有不同类的工具(除非您使用 -cp 覆盖明确告诉它)。

  2. 冒号 (":") 字符分隔不同的目录。只有一个 $CLASSPATH 并且其中包含所有目录。因此,当您运行“export CLASSPATH=....”时,您希望包含当前值“$CLASSPATH”以便附加到它。例如:

    export CLASSPATH=.
    export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.12.jar
    

    在上面的第一行中,您只用一个简单的“点”开始 CLASSPATH,它是您当前工作目录的路径。这样,无论何时运行 java,它都会在当前工作目录(您所在的目录)中查找类。在上面的第二行中,$CLASSPATH 获取您之前输入的值 (.) 并将路径附加到 mysql 驱动程序。现在,java 将查找驱动程序和您的类。

  3. echo $CLASSPATH
    

    非常方便,它返回的内容应该像冒号分隔的所有目录列表,其中包含您希望 java 查找运行脚本所需的所有目录。

  4. Tomcat 不使用 CLASSPATH。在此处阅读如何处理:https: //tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html