-1

Using Java and c# for the same example give a different result.
Code in Java :

public static void main(String[] args) 
{
    for(int i = 0; i < args.length; i++)
    {
        System.out.println(args[i]);
    }
}

Here's is the content folder where my java and class files are located :
Folder Content

If i use this code through the CMD, it's fine :

java FileC 2 - 3

Or

java FileC 2 + 3


The result is fine :
2
-
3

or

2
+
3

If i use :

java FileC 2 * 3

The result is :
2
FileB.css
FileC.class
FileC.java
FileD.txt
FileE.eml
folderA
3

But it should be printing :
2
*
3

If i use this :

java FileC 2 "*" 3

It works fine i get :
2
*
3

As explained here, the answer said that the * sign was expanded : The issue of * in Command line argument

But when i use the same code block in c# and run it the same way i did java "myApp 2 * 3" it worked fine, no need for quotes this time, so is it really a shell/cmd interpretation ? I'm confused !

Servy
  • 193,745
  • 23
  • 295
  • 406
  • Could you please show your C# commands as you did for Java? – hmatar Mar 07 '17 at 09:21
  • 3
    In the question you linked [one answer](http://stackoverflow.com/a/31384767/2878796) mentions that this expansion is performed by *java.exe* on windows, not by *cmd.exe* – UnholySheep Mar 07 '17 at 09:23
  • Possible duplicate of [Problem of \* in Command line argument](http://stackoverflow.com/questions/2718873/problem-of-in-command-line-argument) – RobV Mar 07 '17 at 09:59
  • @UnholySheep I was really confused cause answers/comments on many other places said it's a CMD related, and even in one of the first answers in the topic that i linked. –  Mar 07 '17 at 10:11
  • Most of the other answers on that question talk about (UNIX/Linux) shells (such as Bash) which are **not** the same as Windows' *cmd.exe* - they have different commands and behavior. Though I understand the confusion if you weren't aware of that – UnholySheep Mar 07 '17 at 10:13

2 Answers2

1

The * is being globbed by the jvm because of this "feature": https://bugs.openjdk.java.net/browse/JDK-8131329

Palle Due
  • 4,235
  • 3
  • 16
  • 29
  • 1
    The "bug" is marked as "Not An Issue" (and a comment in there states it's a feature) - I'd suggest rephrasing the answer to represent that – UnholySheep Mar 07 '17 at 10:00
0

In C# you launch our application and in java you launch java with your class name as argument.

This class

public class Sample {
    public static void main(String[] args) {
        System.out.println(args[1]);
    }
}

when it used in command line:

java Sample *

Then it responses me (I used it inside Intellij Idea project):

.idea

This behavior is java specific because for python script:

import sys
print sys.argv[1]

When I launch

python main.py *

Then it responses me *.

Have tested on Windows 8.

Dmitry Gorkovets
  • 1,898
  • 8
  • 18