java 如何为类路径设置参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5898180/
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
how to set argument for classpath
提问by user739991
to launch tests , I have to set a big list of jar files as arguments for classpath : -classpath a.var;b.jar;.... - Is there an other way to specify libraries ? for example is it possible to set file as arguments and the file contains path to all libraries example : -classpath myFile.txt and myFile.txt contains ../a.jar ../b.jar .. etc
要启动测试,我必须设置一个很大的 jar 文件列表作为类路径的参数: -classpath a.var;b.jar;.... - 还有其他方法可以指定库吗?例如,是否可以将文件设置为参数,并且该文件包含所有库的路径示例:-classpath myFile.txt 和 myFile.txt 包含 ../a.jar ../b.jar .. 等
thanks,
谢谢,
回答by Peter Knego
You can programmatically add a new path to classpath:
您可以以编程方式向类路径添加新路径:
String currentPath = System.getProperty("java.library.path");
System.setProperty( "java.library.path", current + ":/path/to/my/libs" );
// this forces JVM to reload "java.library.path" property
Field fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" );
fieldSysPath.setAccessible( true );
fieldSysPath.set( null, null );
You can add a path, so no need to list all jars. This changes classpath only for your JVM instance, so it will not affect other java applications.
您可以添加路径,因此无需列出所有 jar。这只会为您的 JVM 实例更改类路径,因此不会影响其他 Java 应用程序。
Update:
更新:
:
and /
are UNIX-specific lib-path and folder-path separators. For multi-OS you should use "path.separator" and "file.separator" system properties.
:
和/
是 UNIX 特定的 lib-path 和 folder-path 分隔符。对于多操作系统,您应该使用“path.separator”和“file.separator”系统属性。
回答by Michael Borgwardt
Since Java 6, you can use wildcards in your classpath:
从 Java 6 开始,您可以在类路径中使用通配符:
java -classpath 'lib/*'
Note that you must quote the classpath string to avoid having the shell expand the wildcard.
请注意,您必须引用类路径字符串以避免外壳扩展通配符。
回答by JB Nizet
Since Java 6, you may use wildcards in classpath, to include all jars in a directory. See http://download.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html.
从 Java 6 开始,您可以在类路径中使用通配符,以包含目录中的所有 jar。请参阅http://download.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html。
回答by justkt
You can set the environment variable CLASSPATH (use the proper syntax for your shell to do this, ex. bash, Windows XP, etc).
您可以设置环境变量 CLASSPATH(使用正确的 shell 语法来执行此操作,例如bash、Windows XP等)。
You can also create some sort of profile file that does this for you all the time, ex .bashrc. This will affect every time the java
command is used under that profile, of course.
您还可以创建某种配置文件,始终为您执行此操作,例如.bashrc。当然,这将影响每次java
在该配置文件下使用该命令的时间。
If your main class is in a jar, you can also use the jar's manifest to set the classpath. Sun/Oracle has a tutorial page on doing this. Create a file called Manifest.txt. In that file add the line:
如果您的主类在 jar 中,您还可以使用 jar 的清单来设置类路径。 Sun/Oracle 有一个关于这样做的教程页面。创建一个名为 Manifest.txt 的文件。在该文件中添加以下行:
Class-Path: jar1-name jar2-name directory-name/jar3-name
类路径:jar1-name jar2-name directory-name/jar3-name
where the various jar1-name
parts are actual jars on your classpath.
其中各个jar1-name
部分是类路径上的实际 jar。
Then create your jar using:
然后使用以下方法创建您的 jar:
jar cfm MyJar.jar Manifest.txt MyPackage/*.class
jar cfm MyJar.jar Manifest.txt MyPackage/*.class
Or the Ant Jar Taskwith manifest attribute set, or use the Maven jar pluginor however your build works, get the manifest set for the jar.
或者使用清单属性集的Ant Jar 任务,或者使用Maven jar 插件,或者无论您的构建如何工作,获取 jar 的清单集。
Or continue to use --classpath as you are currently.
或者继续使用 --classpath 就像你现在一样。
回答by Vladimir Dyuzhev
To lunch testsI strongly suggest you to use Ant.
对于午餐测试,我强烈建议您使用 Ant。
And Ant has <classpath>
element, which allows you to specify "all jars within given directory":
Ant 有<classpath>
元素,它允许您指定“给定目录中的所有 jar”:
<classpath>
<pathelement path="${classpath}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<pathelement location="classes"/>
<dirset dir="${build.dir}">
<include name="apps/**/classes"/>
<exclude name="apps/**/*Test*"/>
</dirset>
<filelist refid="third-party_jars"/>
</classpath>