Stop expanding wildcard symbols in command line arguments to Java
Asked Answered
M

2

5

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.

Marlyn answered 20/9, 2014 at 12:21 Comment(1)
This problem is caused by a known bug in the JVM, documented here: bugs.openjdk.java.net/browse/JDK-8131329 See also #37037875Bluefield
G
10

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.

Gunar answered 20/9, 2014 at 12:30 Comment(0)
C
0

Additionally to the possibility, to quote the arguments which should not be expanded, there is another possibility. You can use a command-line argument file. The wildcards in parameters which are specified there are not expanded.

At least, it is so described here: https://docs.oracle.com/en/java/javase/11/tools/java.html#GUID-4856361B-8BFD-4964-AE84-121F5F6CF111. See there the section "java Command-Line Argument Files".

The launcher doesn’t expand wildcards that are present within an argument file.

Conchoid answered 15/1, 2022 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.