在 Java 类路径中使用通配符

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

Using wildcards in java classpath

javaclasspathwildcard

提问by Blanca Hdez

I am running a shell script that is supposed to execute a mainmethod:

我正在运行一个应该执行一个main方法的 shell 脚本:

java -classpath /path/to/app/conf/lib/nameJar* com.example.ClassTest

In this point I get this exception:

在这一点上,我得到了这个例外:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
        at java.net.URLClassLoader.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:303)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)

This is because the spring jars are located in another folder. SO I have changed the script:

这是因为弹簧罐位于另一个文件夹中。所以我改变了脚本:

java -classpath /path/to/app/conf/lib/nameJar*:/path/to/app/lib/spring* com.example.ClassTest

But with this script, the com.example.ClassTest can not be found. Any ideas about this issue?

但是使用此脚本,无法找到 com.example.ClassTest。关于这个问题的任何想法?

Thanks in advance

提前致谢

回答by Hari Menon

The java classpath wildcard expansion is unusual.

java 类路径通配符扩展是不寻常的。

From the docs:

文档

Understanding class path wildcards

Class path entries can contain the basename wildcard character *, which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For example, the class path entry foo/* specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory.

理解类路径通配符

类路径条目可以包含基本名称通配符 *,这被认为等同于指定目录中所有文件的列表,扩展名为 .jar 或 .JAR。例如,类路径条目 foo/* 指定名为 foo 的目录中的所有 JAR 文件。仅由 * 组成的类路径条目扩展为当前目录中所有 jar 文件的列表。

So, what you need to specify is:

因此,您需要指定的是:

java -classpath /path/to/app/conf/lib/*:/path/to/app/lib/*

If you need only specific jars, you will need to add them individually. The classpath string does notaccept generic wildcards like nameJar*, *.jar, spring*etc.

如果您只需要特定的 jar,则需要单独添加它们。类路径字符串接受通用通配符像nameJar**.jarspring*等等。

Read Setting multiple jars in java classpathfor more information.

阅读在 java 类路径中设置多个 jar 以获取更多信息。

回答by Ashika Umanga Umagiliya

seems JVM cant find the Spring related JARS. Put all required JARS in one location (say c:\libs) and use "java.ext.dirs" when runing "java" command

似乎 JVM 找不到与 Spring 相关的 JARS。将所有需要的 JARS 放在一个位置(比如 c:\libs)并在运行“java”命令时使用“java.ext.dirs”

like :

喜欢 :

java -Djava.ext.dirs=c:/libs com.example.ClassTest