Quotes in command line arguments passed to Java main()

落花浮王杯 提交于 2021-02-07 18:15:52

问题


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\

回答1:


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".




回答2:


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.




回答3:


Use

toto "has:\"tutu titi\""



回答4:


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



回答5:


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



来源:https://stackoverflow.com/questions/3960172/quotes-in-command-line-arguments-passed-to-java-main

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!