问题描述
我遇到了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 / *
/ pre>
程序将打印
test / foo.txt
test / bar.txt
其中foo.txt和bar.txt是目录test。
如果我用引号包围通配符参数,则可以防止Shell扩展; * nix上的单引号,Windows上的双引号。所以对于Windows,如果我在命令提示符下执行以下操作:
java WildCardtest / *
该程序现在将打印
test / *
(无扩展)。
然而,我发现在Eclipse运行启动器中的引用似乎没有任何效果,而且参数仍然被扩展。如果我把
test / *
在Eclipse运行启动器的程序参数部分,并运行上面的类,我仍然得到
code> test / foo.txt
test / bar.txt
在其他单词,当程序实际运行时,双引号似乎丢失。这似乎只发生在Windows下。
有没有办法通过Windows上的Eclipse运行启动程序阻止glob扩展?
解决方案模式
(。*)
不会被eclipse扩展,仍然可以作为正则表达式。 p>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?
解决方案The pattern
(.*)
will not be expanded by eclipse, and still works as a regex.这篇关于在eclipse中运行Java应用程序时如何避免glob扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!