Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quotes in command line arguments passed to Java main()

I run a Java program with the following command line (Edit: in NetBeans 6.8 project properties)

toto has:"tutu titi"

args is an array of 2 Strings

toto
has:tutu titi

I want (two arguments indeed, the second) args[1] to be

has:"tutu titi"

How should I do that?

Edit: I have already tried escaping the quotes with backslash from "Arguments" line in Netbeans propject properties, but I get args[1]

has:\tutu titi\
like image 888
rds Avatar asked Oct 18 '10 14:10

rds


People also ask

Can we pass arguments in main () in Java?

Command-line arguments in Java are used to pass arguments to the main program. If you look at the Java main method syntax, it accepts String array as an argument. When we pass command-line arguments, they are treated as strings and passed to the main function in the string array argument.

How many arguments can be passed to main () using command line arguments?

Explanation: Infinite number of arguments can be passed to main(). 4.

How can we pass arguments through main () method?

You can write the public static void main() method with arguments other than String the program gets compiled. Since the main method is the entry point of the Java program, whenever you execute one the JVM searches for the main method, which is public, static, with return type void, and a String array as an argument.

How are command line arguments passed in Java?

In the command line, the arguments passed from the console can be received in the java program and they can be used as input. The users can pass the arguments during the execution bypassing the command-line arguments inside the main() method. We need to pass the arguments as space-separated values.


5 Answers

This really depends on your shell. You haven't said what operating system you're using. For example, on Windows this will work:

java Test toto "has:\"tutu titi\""

I believe the same thing will work in bash, too.

But if you're asking what you can do within Java to resolve this: nothing. The shell will have parsed the command line before the process was invoked, and you can't undo that parsing.

like image 127
Jon Skeet Avatar answered Sep 30 '22 00:09

Jon Skeet


I had a similar problem in NetBeans and found the solution:

Edit/Add the property "application.args" in your private.properties to this:

application.args='has:""tutu titi""'

Single quotes to mark your "argument" and two double quotes to define one "double quotes".

like image 44
Nigjo Avatar answered Sep 30 '22 00:09

Nigjo


Use

toto "has:\"tutu titi\""
like image 45
Riduidel Avatar answered Oct 01 '22 00:10

Riduidel


If adding from NetBeans (7.1.2) Configuration/Arguments dialog field, a single-quote outer and escaped double quote inner worked for me e.g.:

my argument
like image 20
parmres Avatar answered Oct 03 '22 00:10

parmres


This has been recognised by netbeans as a bug that won't be fixed!

like image 26
Syed Ali Avatar answered Sep 30 '22 00:09

Syed Ali