简体   繁体   English

如何将命令行参数包含“|”传递给java程序

[英]how to pass commandline argument include “|” to java program

One of the arguments passed to my java program is like this, ab|cd . 传递给我的java程序的一个参数是这样的, ab|cd Initially, when I run the java program, like this, 最初,当我运行java程序时,像这样,

$ java className ab|cd $ java className ab | cd

it fails, since | 它失败了,因为| is interpreted in the linux shell as a pipe symbol. 在linux shell中被解释为管道符号。 So only ab is passed into java program. 所以只有ab被传递到java程序中。 So I made a second attempt: 所以我做了第二次尝试:

$ java className "ab|cd" $ java className“ab | cd”

This time, "ab|cd" is passed in, but the value includes the double quotes. 这次,传入"ab|cd" ,但该值包含双引号。 What the program is really intended to have is ab|cd . 该程序的真正意图是ab|cd How can I pass in the correct value without the quotes? 如何在没有引号的情况下传递正确的值?

在命令shell中,您可以使用'\\'字符转义字符。

 java className ab\|cd

Try (for Linux): 试试(对于Linux):

$ java className ab\|cd

For Windows: 对于Windows:

java className ab^|cd

您可以像以下代码一样使用转义字符:

$ java className ab\|cd

尝试这个,

"\\" is used inorder to nullify the effect of the characters which have special meanings.

java className ab\|cd

Maybe try this: 也许试试这个:

arg="ab|cd"
java className $arg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM