gpt4 book ai didi

classpath - Java 类路径通配符行为

转载 作者:行者123 更新时间:2023-12-01 10:56:44 34 4
gpt4 key购买 nike

我以前成功地使用了 java -classpath 通配符扩展功能。我目前遇到一个奇怪的问题。

通配符应该扩展到指定文件夹中的每个 jar。这是来自 Oracle 的引述:

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.

This is a link to Oracle doc on Java 6 on the subject of classpath.

我看到的行为与此相矛盾。这里有 3 次运行。第一个显式命名 jar 并且它可以工作。其他人使用通配符并失败。为什么?这对我来说很重要,因为我依赖通配符(在其他地方),因此了解这种意外行为对我来说很重要。

#!/bin/bash

printf "The EV is...\n"
echo $CLASSPATH
printf "The working directory is...\n"
pwd
printf "Directory listing...\n"
ls
printf "END of directory listing.\n"

printf "Test with named jar.\n"
java -javaagent:../sizeof/sizeof.jar -classpath ./testsizeof.jar info.zqxj.test.Tester

printf "Test with star.\n"
java -javaagent:../sizeof/sizeof.jar -classpath * info.zqxj.test.Tester

printf "Test with dot slash star.\n"
java -javaagent:../sizeof/sizeof.jar -classpath ./* info.zqxj.test.Tester

输出:

The EV is...

The working directory is...
/home/b/Documents/workspace/testsizeof
Directory listing...
bin run.sh src testsizeof.jar
END of directory listing.
Test with named jar.
40
Test with star.
Exception in thread "main" java.lang.NoClassDefFoundError: run/sh
Caused by: java.lang.ClassNotFoundException: run.sh
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: run.sh. Program will exit.
Test with dot slash star.
Exception in thread "main" java.lang.NoClassDefFoundError: //run/sh
Caused by: java.lang.ClassNotFoundException: ..run.sh
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: ./run.sh. Program will exit.

最佳答案

解决方案,双引号类路径参数。示例:-classpath "*" 这在命令行和 bash 脚本中都是必需的。

后续附录:

此外,请注意 -classpath "~/folder/*" 失败但 -classpath ~/folder/"*" 是好的。引用通配符但不引用 ~。好像需要操作系统来解释~但是需要引用*通配符因为要传给java扩展在Java时尚。

另请注意,您不应要求 java 展开 *.jar,因为这会产生意想不到的结果。 Java 规范说正确的通配符只是 *

关于classpath - Java 类路径通配符行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14722657/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com