Java 添加到 OSX 上的类路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1675765/
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
Adding to the classpath on OSX
提问by Mike2012
Can anyone tell me how to add to the classpath on OSX?
谁能告诉我如何添加到 OSX 上的类路径?
采纳答案by Matt Solnit
If you want to make a certain set of JAR files (or .class files) available to every Java application on the machine, then your best bet is to add those files to /Library/Java/Extensions
.
如果您想让机器上的每个 Java 应用程序都可以使用一组特定的 JAR 文件(或 .class 文件),那么最好的办法是将这些文件添加到/Library/Java/Extensions
.
Or, if you want to do it for every Java application, but only when yourMac OS X account runs them, then use ~/Library/Java/Extensions
instead.
或者,如果您想为每个 Java 应用程序执行此操作,但仅当您的Mac OS X 帐户运行它们时,请~/Library/Java/Extensions
改用。
EDIT:If you want to do this only for a particular application, as Thorbj?rn asked, then you will need to tell us more about how the application is packaged.
编辑:如果您只想针对特定应用程序执行此操作,如 Thorbj?rn 所问,那么您需要告诉我们更多有关该应用程序如何打包的信息。
回答by James Bailey
If your shell is tcsh or csh, you can set it in /etc/profile. Open terminal, "vim /etc/profile" and add the following line:
如果你的 shell 是 tcsh 或 csh,你可以在 /etc/profile 中设置它。打开终端,“vim /etc/profile”并添加以下行:
setenv CLASSPATH (insert your classpath here)
setenv CLASSPATH (insert your classpath here)
回答by wishi
Normally there's no need for that. First of all
通常没有这个必要。首先
echo $CLASSPATH
If there's something in there, you probably want to check Applications -> Utilites -> Java.
如果里面有东西,你可能想检查应用程序 -> 实用程序 -> Java。
回答by Andrew Swan
In OSX, you can set the classpath from scratch like this:
在 OSX 中,您可以像这样从头开始设置类路径:
export CLASSPATH=/path/to/some.jar:/path/to/some/other.jar
Or you can add to the existing classpath like this:
或者您可以像这样添加到现有的类路径中:
export CLASSPATH=$CLASSPATH:/path/to/some.jar:/path/to/some/other.jar
This is answering your exact question, I'm not saying it's the right or wrong thing to do; I'll leave that for others to comment upon.
这是在回答您的确切问题,我并不是说这是对还是错;我会把它留给其他人评论。
回答by Bruce Shen
If you just want to use a class path just for the current run time. You can achieve that by add a class path option when you run java command.
如果您只想为当前运行时使用类路径。您可以通过在运行 java 命令时添加类路径选项来实现这一点。
In you command line. Use
java -cp "path/to/your/jar:." main
rather than just
java main
在你的命令行中。使用
java -cp "path/to/your/jar:." main
而不仅仅是
java main
By doing so, your command tells the process class paths where it can search for libraries.
通过这样做,您的命令会告诉进程类路径它可以在何处搜索库。