传递给 Java main() 的命令行参数中的引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3960172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:09:41  来源:igfitidea点击:

Quotes in command line arguments passed to Java main()

javanetbeansescapingquotes

提问by rds

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

我使用以下命令行运行 Java 程序(编辑:在 NetBeans 6.8 项目属性中)

toto has:"tutu titi"

args is an array of 2 Strings

args 是一个包含 2 个字符串的数组

toto
has:tutu titi

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

我想要(确实是两个论点,第二个)args[1]

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]

编辑:我已经尝试从 Netbeans 项目属性的“参数”行中用反斜杠转义引号,但我得到 args[1]

has:\tutu titi\

采纳答案by Nigjo

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

我在 NetBeans 中遇到了类似的问题并找到了解决方案:

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

在您的 private.properties 中编辑/添加属性“application.args”:

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

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

单引号标记您的“参数”,两个双引号定义一个“双引号”。

回答by Jon Skeet

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

这真的取决于你的外壳。您还没有说明您使用的是什么操作系统。例如,在 Windows 上这将起作用:

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

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

我相信同样的事情也适用于 bash。

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

但是,如果您要问在 Java 中可以做什么来解决这个问题:什么也没有。shell 将在调用进程之前解析命令行,并且您无法撤消该解析。

回答by Riduidel

Use

利用

toto "has:\"tutu titi\""

回答by sttaq

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

这已被 netbeans 识别为无法修复的错误

回答by parmres

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

如果从 NetBeans (7.1.2) 配置/参数对话框字段添加,单引号外层和转义双引号内层对我有用,例如:

my argument