简体   繁体   中英

Single quotation marks as arguments to a Java program

When I run my jar from the cmd in this way:

java -jar myapp.jar -fn='Something'

I get args = [-fn=Something] . So, the problem is that it misses the quotation marks. The same happens with the double quotation marks.

What can I do to solve this? I couldn't find any solution on the Internet.

PS another question would be if I can make my program recognize automatically the -fn variable or I just should parse it like a String. I am using the parsing method, but I just want to know if maybe there are other (more ethical) ways.

There are two different ways to solve the quotes problem:

with escaping the quote:

java -jar myapp.jar -fn="\'Something\'"
java -jar myapp.jar -fn="\"Something\""

with single and double quotes:

java -jar myapp.jar -fn='"Something"'

If you want to create a console programme, I can recommend Picocli or Spring Shell . Otherwise, you could use eg the Commons CLI to parse the arguments, or you can do it yourself manually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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