Juan Balceda
Juan Balceda

Reputation: 13

Why java arguments behave differently in Windows and Linux?

I'm getting different results testing a simple java class on Windows cmd and wsl (ubuntu).

The java class:

public class PrintArgs {

    public static void main(String[] args) {

        System.out.println("Printing some arguments in this code: ");

        // Loop through arguments passed and print them to standard output
        for (int i = 0; i < args.length; i++) {
            System.out.println("Argument " + (i + 1) + ": " + args[i]);
        }
    }
}

I use this test arguments:

java PrintArgs.java Test "Testing TestThis" 'Some arguments' ´More Arguments´

In cmd, the single quote doesn't group the arguments: cmd results and java version

but in ubuntu, it does: wsl results and java version

Any idea why this is happening?

Upvotes: 1

Views: 370

Answers (1)

Joachim Sauer
Joachim Sauer

Reputation: 308151

The Java process will already get an array of strings as the argument (in anything C-based it's more like a **char, but that's close enough). It doesn't even see the quotes that group a single argument together, because those will already have been interpreted by the shell.

The shell (probably Bash in WSL and cmd.exe in the command window) is responsible for taking the single continuous string of what the user entered and splitting it into arguments (and expanding wildcards, where applicable, but that doesn't happen in this case).

Now Bash and cmd.exe have different rule about how quoting works, so they split the single string differently.

Upvotes: 1

Related Questions