0

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.

P.S. 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.

flaxel
  • 4,173
  • 4
  • 17
  • 30
Radu Aramă
  • 195
  • 1
  • 1
  • 10

1 Answers1

1

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 e.g. the Commons CLI to parse the arguments, or you can do it yourself manually.

flaxel
  • 4,173
  • 4
  • 17
  • 30
  • Okay. Thank you. I just had a specific task to make the program to take specifically these types of inputs like -fn='Something' But I think it was a mistake – Radu Aramă Apr 10 '21 at 12:00
  • 1
    Picocli (https://picocli.info) can be used to create both interactive shell programs as well as simple CLI commands (with subcommands, colors and autocompletion). Not sure if Commons CLI should be recommended. :-) (But I’m biased...) :-) – Remko Popma Apr 10 '21 at 12:57