how to avoid glob expansion when running Java app

2019-02-21 12:44发布

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?

2条回答
一纸荒年 Trace。
2楼-- · 2019-02-21 13:29

The pattern (.*) will not be expanded by eclipse, and still works as a regex.

查看更多
干净又极端
3楼-- · 2019-02-21 13:38

The problem looks quite wired:

*.txt
foo.*

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

查看更多
登录 后发表回答