Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

shell - Stop expanding wildcard symbols in command line arguments to Java

My program has to watch for files which match a mask. The folder name and mask are passed through command line arguments. But the mask is replaced by the first match before I can use it!

Double quotes have no effect and other symbols too. I tried using or ' to protect the input. However then this symbol will be added to the args, which I do not want. Any idea of how to fix it?

public static void main(String[] args) throws IOException {
    File dir = new File(args[0]);
    String mask = args[1];
    System.out.println(dir.getAbsolutePath());
    System.out.println(mask);
    String regex = args[2];
    System.out.println(regex);
}

Regular expression from args[2] also replaced with some file from folder.

Input: "E:ProgrammingJavaTask7" *.??? ...
Ouput: E:ProgrammingJavaTask7 .git Task7.iml

Input: "E:ProgrammingJavaTask7" *.????* [a-zA-Z]
Output: E:ProgrammingJavaTask7 .idea [a-zA-Z]

Input: "E:ProgrammingJavaTask7" '.???' ...
Output: E:ProgrammingJavaTask7 '.???' ...

Input: "E:ProgrammingJavaTask7" \'.???'\ ...
Output: E:ProgrammingJavaTask7 '.???' ...

I understand that using quote or backslash isn't that bad idea, but I think there exists better way.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Background: On Linux, it is not Java that is expanding the wildcards in the command arguments. The shell does it before the java command is launched.

The way to stop the shell from expanding wildcards is to quote the arguments. How you do this depends on the shell you are using.


Now for the Windows case ... which is what you are really asking about.

From what I have read, the standard "cmd.exe" shell (in its various versions / flavours) does NOT do wildcard expansion. It is left to the application to do expansion (or not) on an ad-hoc basis.

Obviously this is problematic for the Java "write once, run everywhere" philosophy, so the Java designers have tried to make wild-cards in command line arguments work on Windows like they do on Unix and Linux. But unfortunately, they can't do a perfect job of this ... hence this anomaly.

However, according to this page, putting double quotes around an argument tells Java to not do wild-card expansion.

But if this doesn't help, you are probably out of luck.


Here are some links to Oracle documentation on this topic, taken from Oracle Java bug report #5036373:

Java Wildcard expansion on Windows platform has been documented. See the following links:

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html

Wildcard expansion does not work in a Windows command shell for a single element classpath due to the Microsoft bug described in: http://connect.microsoft.com/VisualStudio/feedback/details/98756/vs2005-setargv-obj-wildcard-handling-broken.

The limitations are also mentioned in 7u10 release notes: http://www.oracle.com/technetwork/java/javase/7u10-relnotes-1880995.html

However, I think that the Oracle employee who wrote that was being deliberately obtuse, because the wildcard expansion in general is patently NOT documented in those "manual" pages. They only talk about wildcard expansion in the -cp argument.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...