在 Eclipse 中运行 Java 应用程序时如何避免全局扩展
我遇到了 Eclipse 运行配置的特殊行为,这似乎是 Windows 独有的问题。假设我有一个打印出命令行参数的 Java 应用程序,如下所示:
public class WildCard {
public static void main(String[] args) {
for (String arg: args) {
System.out.println(arg);
}
}
}
如果我提供带有可由 shell 扩展的通配符的参数,shell 将扩展它并将其提供给 Java 程序。这并不奇怪。因此,如果我在命令提示符下执行此操作,
java WildCard test/*
程序将打印
test/foo.txt
test/bar.txt
foo.txt 和 bar.txt 是目录“test”中的文件。
如果我将通配符参数括在引号中,则可以防止 Shell 扩展; *nix 上为单引号,Windows 上为双引号。因此,对于 Windows,如果我在命令提示符下执行以下操作:
java WildCard "test/*"
程序现在将打印
test/*
(不扩展)。
然而,我发现Eclipse运行启动器中的引用似乎没有效果,并且参数仍然被扩展。如果我将
"test/*"
程序参数部分放入 Eclipse 运行启动器中,并运行上面的类,我仍然会得到
test/foo.txt
test/bar.txt
换句话说,当程序实际运行时,双引号似乎丢失了。这似乎只发生在 Windows 上。
有没有办法防止 Windows 上的 Eclipse 运行启动器进行全局扩展?
I am running into a peculiar behavior of the Eclipse run configuration, and it appears to be a Windows-only problem. Suppose I have a Java app that prints out the command line arguments, like the following:
public class WildCard {
public static void main(String[] args) {
for (String arg: args) {
System.out.println(arg);
}
}
}
If I provide argument with a wild card that can be expanded by the shell, the shell will expand it and give it to the Java program. That's no surprise. So, if I do on the command prompt
java WildCard test/*
the program will print
test/foo.txt
test/bar.txt
where foo.txt and bar.txt are files in the directory "test".
Shell expansions can be prevented if I surround the wildcard argument in quotes; single quotes on *nix, and double quotes on Windows. So for Windows, if I do the following on the command prompt:
java WildCard "test/*"
the program will now print
test/*
(no expansion).
However, what I find is that the quoting in the Eclipse run launcher seems to have no effect, and the argument is still expanded. If I put
"test/*"
in the program argument section in the Eclipse run launcher, and run the above class, I still get
test/foo.txt
test/bar.txt
In other words, the double quotes seem to be lost when the program actually runs. This seems to happen only with Windows.
Is there a way to prevent the glob expansion with the Eclipse run launcher on Windows?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题看起来很复杂:
不会扩展,但
会扩展。
看起来只有“所有文件”被扩展,但所有其他字符串(包括 *)将保持不变。
我遇到同样的问题,我使用 XP 和 eclipse 3.5.2
The problem looks quite wired:
will NOT be expanded, but
will be expanded.
It looks like only "all files" is expanded, but all other strings (including *) will stay unchanged.
I'm at the same problem and I use XP and eclipse 3.5.2
模式
(.*)
不会被 eclipse 扩展,并且仍然作为正则表达式使用。The pattern
(.*)
will not be expanded by eclipse, and still works as a regex.