Strange Java * argument behavior

I wrote this class:

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

javac ListArg.java// compiled class

I compiled the class above and run as: java ListArg *

But ListArg displays the current contents of the directory on the console, not "*".

+6
source share
5 answers

EDIT: Looks like I was wrong, and in the end it could be Java. If you are using a Unix shell, this is probably just a globbing shell. However, it seems to do the exact same thing on Windows, which surprises me (since the Windows command line does not perform globalization by default).

Unfortunately, on Windows, normal quoting gives you the argument given, i.e. if you print args[0] by running

 java ListArg '*' 

it will contain a single quote. I am investigating if there is a way to disable this ... although this will work on a Unix shell.

EDIT: Hmm ... no luck so far managing the string only stars in Windows :(

+4
source

You must wrap * quotation marks: java ListArg '*' or java ListArg "*"

+2
source

This is your shell (if you are not on Windows) expanding *. Try to slip away from it by putting it in quotation marks, for example "*".

For me on Windows XP, using double quotes, "*" as an argument gives the java program * as an argument (without double quotes and without an extension). Perhaps this is different for other versions of Windows.

+1
source

you give the command line arguments in double quotes and check.

+1
source

* - wildcard symbol that is pre-processed by the terminal

Like * , you can use

  • *.*
  • *.txt . This will be a list of files and folders with .txt as the end

etc. You can try this

This link contains information about this.

It works not only on Linux, but also on Windows.

0
source

Source: https://habr.com/ru/post/905703/


All Articles