Windows 上 Java7 命令行的通配符扩展被破坏(7?)

发布于 2025-01-04 08:57:07 字数 1219 浏览 2 评论 0原文

我观察到 Windows 上 Java7 的通配符扩展行为有一个奇怪的行为。

几个世纪以来,“*”与*之间存在着明显的区别。
对于 Java7 来说似乎不再如此(至少在 Windows7 上)。

我在使用 通配符类路径
尽管引用了通配符类路径,它还是被扩展了。
因此,似乎不再可能将通配符传递给 java 应用程序。

因此,使用 java -cp "somewhere/*" 将失败("somewhere\*" 也是如此)。

解决方法似乎是: java -cp "somewhere/*;" ,它会抑制扩展。

为了验证行为,我编写了一个小的 Echo.java 类。

我发现使用 java 1.6.0 引用的“*”和普通 * 的效果与预期一致, 而在 Java7 上我总是得到扩展的通配符。 到目前为止,这是在Windows7上观察到的,不知道XP上会发生什么。

问题出现了,因为 Windows 上的通配符永远不会被黑暗时代 CMD.EXE 扩展(就像 UNIX 上的任何 shell 一样)。相反,每个可执行文件必须使用 setargv.obj 显式执行此操作。

我发现两个相关问题似乎描述了类似的问题:

这是其他人观察到的吗?
或者是否有一些晦涩的 Windows 或批处理文件设置来控制它?

迪特尔.

I observe a strange behavior of wildcard expansion behavior for Java7 on Windows.

For centuries there was a clean difference between "*" versus *.

Seems this it not longer true for Java7 (at least on Windows7).

I noticed the problem when using a wildcard classpath.

In despite of quoting the wildcard-classpath it gets expanded.

Thus it seems not possible any more to pass a wildcard to the java application.

So using java -cp "somewhere/*" will fail (as does "somewhere\*").

A workaround seems to be: java -cp "somewhere/*;" which inhibits the expansion.

To verify the behavior I wrote a small Echo.java class.

I found that using java 1.6.0 quoted "*" and plain * works like expected,
whereas on Java7 I always got the expanded wildcard.
Until now this was observed on Windows7, don't know what happens on XP.

The problem arises, since wildcards on Windows are never ever expanded by dark age CMD.EXE (like any shell does on UNIX). Instead each executable has to perform this explicitly using setargv.obj.

I found two related issues which seem to describe a similar problem:

Is this observed by someone else?

Or are there some obscure Windows or batch-file settings to control this?

Dieter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ま柒月 2025-01-11 08:57:07

是的,我注意到同样的问题。

Yes, I noticed the same issue.

  • It's explained as a 'known issue' in the release notes for Java7 update 4.

  • Here is the bug report. The fix will be delivered in Java7 update 8 (current release is update 6).

  • Note that there isn't a shell-options workaround, because in Windows, the shell doesn't handle wildcard expansion. (Whereas in Unixes, the shell performs the expansion).

禾厶谷欠 2025-01-11 08:57:07

不是解决 /* 问题的直接解决方案,但我希望您可以使用以下脚本来缓解您的情况。

 libDir2Scan4jars="../test";cp=""; for j in `ls ${libDir2Scan4jars}/*.jar`; do if [ "$j" != "" ]; then cp=$cp:$j; fi; done; echo $cp| cut -c2-${#cp} > .tmpCP.tmp; export tmpCLASSPATH=`cat .tmpCP.tmp`; if [ "$tmpCLASSPATH" != "" ]; then echo .; echo "classpath set, you can now use  ~>         java -cp \$tmpCLASSPATH"; echo .; else echo .; echo "Error please check libDir2Scan4jars path"; echo .; fi; 

为 Linux 编写的脚本,也可以为 Windows 提供类似的脚本。如果提供了正确的目录作为“libDir2Scan4jars”的输入;该脚本将扫描所有 jar 并创建一个类路径字符串并将其导出到环境变量“tmpCLASSPATH”。

Not a direct solution to the broken /* issue but I hope you could use the following script to ease your situation.

 libDir2Scan4jars="../test";cp=""; for j in `ls ${libDir2Scan4jars}/*.jar`; do if [ "$j" != "" ]; then cp=$cp:$j; fi; done; echo $cp| cut -c2-${#cp} > .tmpCP.tmp; export tmpCLASSPATH=`cat .tmpCP.tmp`; if [ "$tmpCLASSPATH" != "" ]; then echo .; echo "classpath set, you can now use  ~>         java -cp \$tmpCLASSPATH"; echo .; else echo .; echo "Error please check libDir2Scan4jars path"; echo .; fi; 

Scripted for Linux, could have a similar one for windows too. If proper directory is provided as input to the "libDir2Scan4jars"; the script will scan all the jars and create a classpath string and export it to a env variable "tmpCLASSPATH".

本宫微胖 2025-01-11 08:57:07

我找到了解决 Windows-Java-launcher-argument-wildcard-expansion 问题的通用解决方法:

@argfile 中的参数不会进行通配符扩展。例如

java foo.Echo '*.txt'     => 1.txt 2.txt

echo "foo.Echo *.txt" >argfile
java @argfile             => *.txt

,如果您有一个包装器 BASH 脚本,您可以执行以下操作:

argfile=c:/tmp/argfile$;
echo foo.Echo "$@" >$argfile;
java @$argfile;

,并且您的命令行参数将不会被启动器进行通配符扩展。

测试用:

adopt_openjdk_15+36_swm_cert_v3
adopt_openjdk-11.0.11.9-hotspot
adopt_openjdk-14.0.2_12-hotspot
adopt_openjdk-16.0.1.9-hotspot
jdk-13.0.2+8
jdk-17.0.1+12

I found a generic workaround for the Windows-Java-launcher-argument-wildcard-expansion problem:

Arguments from an @argfile do not undergo wildcard expansion. E.g.

java foo.Echo '*.txt'     => 1.txt 2.txt

echo "foo.Echo *.txt" >argfile
java @argfile             => *.txt

So if you have a wrapper BASH script, you can do:

argfile=c:/tmp/argfile$;
echo foo.Echo "$@" >$argfile;
java @$argfile;

, and your command line args will not be wildcard-expanded by the launcher.

Tested with:

adopt_openjdk_15+36_swm_cert_v3
adopt_openjdk-11.0.11.9-hotspot
adopt_openjdk-14.0.2_12-hotspot
adopt_openjdk-16.0.1.9-hotspot
jdk-13.0.2+8
jdk-17.0.1+12
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文