通过程序启动的 JAR 接收到的参数中,所有的双引号已被移除。

huangapple go评论76阅读模式
英文:

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

问题

我有一个小的控制台SpringBoot应用程序,它通过命令行接收参数。当我在命令行中使用java -jar运行此应用程序时,一切都正常。问题是,我需要从其他Java应用程序中以编程方式运行此应用程序。我是这样做的:

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

其中 payload 是一个 JSON,类似于:

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

由于某种神秘的原因,payload 文本不带任何双引号。我在那个 SpringBoot 应用程序中作为第一个命令记录了它,如下所示:

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

我可以看到它没有任何双引号!记录的值看起来像这样:

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

如果使用单引号,它们将传递给应用程序。不幸的是,我只能使用双引号。这可能是什么原因呢?

英文:

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

得分: 1


这取决于所使用的命令行环境。如果您按照以下方式传递字符串,应该可以正常工作:(在Windows CMD和git bash上测试通过)

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

您可以使用replaceAll方法将每个双引号替换为3个双引号:

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

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("\"", "\"\"\"");

huangapple
  • 本文由 发表于 2020年9月28日 21:22:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64103046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定