Argument sent to jar (started programmatically) has all double-quotes removed

Viewed 191

I have a small console SpringBoot app which receives arguments via command line. When I run this app in command line as java -jar everything works fine. The problem is that I need to run this app programmatically from the other java app. I do it like this:

Runtime.getRuntime().exec(new String[]{
                "java",
                "-jar",
                pathToJar,
                "--text",
                payload
        });

Where payload is a json, smth like:

String payload= "{\"app\":{\"name\":\"SOME_NAME\",\"signature\":\"308203a73092a86\"}}

For some mysterious reason the payload text comes without any double-quotes. I logged it in that SprintBoot app as the very first command like this:

public static void main(final @Nonnull String... args) {
    for(String arg: args){
        LOG.info(arg);
    }

and I can see that it comes without any double-quotes! Logged values look like this:

2020-09-28 15:31:56.565 | INF | .t.s.a.AttestationTool | --text
2020-09-28 15:31:56.565 | INF | .t.s.a.AttestationTool | {app:{name:SOME_NAME,signature:308203a73092a86}}
2020-09-28 15:31:56.565 | INF | .t.s.a.AttestationTool | Starting application

If use single quotes they get passed to the application. Unfortunately I have to use double-quotes only. What could be the reason for that?

1 Answers

This depends on the shell. if you pass String as follows this should works: (worked on Windows CMD and git bash)

{"""app""":{"""name""":"""SOME_NAME""","""signature""":"""308203a73092a86"""}}"

you can use replaceAll method to replace each double quote with 3 double quotes:

String payload= "{\"app\":{\"name\":\"SOME_NAME\",\"signature\":\"308203a73092a86\"}}"
payload = payload.replaceAll("\"", "\"\"\"");
Related