问题
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:\Programming\Java\Task7" *.??? ...
Ouput: E:\Programming\Java\Task7 .git Task7.iml
Input: "E:\Programming\Java\Task7" *.????* [a-zA-Z]
Output: E:\Programming\Java\Task7 .idea [a-zA-Z]
Input: "E:\Programming\Java\Task7" '.???' ...
Output: E:\Programming\Java\Task7 '.???' ...
Input: "E:\Programming\Java\Task7" \\'.???'\\ ...
Output: E:\Programming\Java\Task7 \'.???'\ ...
I understand that using quote or backslash isn't that bad idea, but I think there exists better way.
回答1:
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.
来源:https://stackoverflow.com/questions/25948706/stop-expanding-wildcard-symbols-in-command-line-arguments-to-java