无法通过java Runtime.getRuntime()。exec()执行CURL命令

Sammidbest

我正在使用Java执行curl命令。

curl -i --user "OAMADMIN_tenant_358922247351079_svc_358922247369079_APPID:Iuj.2swilg5fhv" -H "Content-Type: application/json" -H "Accept: application/json" -H 'X-USER-IDENTITY-DOMAIN-NAME: tenant_358922247351079' -H "X-RESOURCE-IDENTITY-DOMAIN-NAME: tenant_358922247351079" --request GET "https://slc04yre-1.dev.oraclecorp.com:4443/oam/services/rest/11.1.2.0.0/oauth/admin/Clients?name=myMCS_svc_358922247369079_MCS_Client_OAUTHCLIENT"

我想在我的代码中获取该curl命令的输出,但是我的stdoutput出来为空。

 private static String executeCommand(String command) {
        StringBuffer output = new StringBuffer();

        Process p;
        try {
            p = Runtime.getRuntime().exec(command);

            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));
            //p.waitFor();
            String line = "";
            while ((line = reader.readLine()) != null) {
                System.out.println("line="+line);
                output.append(line + "\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return output.toString();

    }

尝试手动执行curl命令,其工作正常。然后我打印了标准错误,我可以看到:

   [testng] error line=
   [testng] error line=curl: (6) Couldn't resolve host 'application'
   [testng] error line=
   [testng] error line=curl: (6) Couldn't resolve host 'application'
   [testng] error line=
   [testng] error line=curl: (6) Couldn't resolve host 'tenant_359516638431079''
   [testng] error line=
   [testng] error line=curl: (6) Couldn't resolve host 'tenant_359516638431079"'
   [testng] error line=
   [testng] error line=curl: (1) Unsupported protocol: "https

手动执行curl命令时,它可以正常工作,然后为什么不通过Runtime.getRuntime()呢?

请建议!任何帮助将不胜感激。

克里斯·斯诺

好像数据外壳程序/控制台正在解释/更改字符。例如以下行:

-H "Content-Type: application/json"

...似乎被解释为三个不同的参数:

-H Content-Type:application/json由shell /控制台。

尝试使用以下格式将命令字符串分解为组件数组:

exec(String[] cmdarray)

这样一来,shell /控制台就可以清楚地将哪些参数组合在一起。

这是常规测试,证明了这一点:

def Object executeCommand(command) {

   def proc = Runtime.getRuntime().exec(command);
   def sout = new StringBuffer()
   def serr = new StringBuffer()

   proc.consumeProcessOutput(sout, serr)
   proc.waitFor()

   return [ 'sout':sout.toString(), 'serr':serr.toString() ]
}   

response = executeCommand('''curl --silent --show-error -H "Accept: application/json" --request GET "https://education.cloudant.com/"''')
assert response['sout'] == ''
assert response['serr'].startsWith( 'curl: (6) Could not resolve host: application' )

response = executeCommand(['curl', '--silent',  '--show-error',  '-H', 'Accept: application/json',  '--request', 'GET', 'https://education.cloudant.com/'] as String[] )
assert response['sout'].startsWith('{"couchdb":"Welcome","version":"1.0.2","cloudant_build":"2367"}')
assert response['serr'] == ''

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用runtime.getruntime命令通过Java代码运行时,该命令无法成功执行

来自分类Dev

java Runtime.getRuntime()。exec()无法运行命令

来自分类Dev

我可以获取Java Runtime.getRuntime()。exec的真正完整执行的命令吗?

来自分类Dev

Java中的cleartool命令执行-runtime()。exec

来自分类Dev

如何通过具有Java管理员权限的Runtime.getRuntime.exec()运行cmd命令?

来自分类Dev

Java Runtime.exec无法正确执行JSTACK命令

来自分类Dev

无法通过 Runtime.exec 运行命令

来自分类Dev

Runtime.getRuntime.exec()无法正常工作

来自分类Dev

Java Runtime.getRuntime().exec 无法捕获所有输出

来自分类Dev

带有Java Runtime.getRuntime()。exec(command)的mysqldump命令不会生成转储

来自分类Dev

使用Runtime.getRuntime()。exec(String [])在Java中运行Windows命令

来自分类Dev

如何使用Java'Runtime.getRuntime()。exec'命令防止cmd退出

来自分类Dev

无法使用Runtime.exec()在Android Java代码中执行shell命令“ echo”

来自分类Dev

无法从Runtime.getRuntime()。exec(..)启动Steam.exe

来自分类Dev

Runtime.getRuntime()。exec()Java 1.7中的Perl脚本

来自分类Dev

Java Runtime.getRuntime()。exec(“ ipconfig / release”); 返回错误?

来自分类Dev

Java Runtime.getRuntime()。exec.getInputStream释放内容

来自分类Dev

使用Runtime.getRuntime()。exec从Java启动的进程无法访问文件系统位置

来自分类Dev

使用Runtime.getRuntime()。exec从Java启动的进程无法访问文件系统位置

来自分类Dev

发出执行Runtime.getRuntime()。exec AWK的问题

来自分类Dev

JavaScript使用java.lang.Runtime.getRuntime()运行DOS命令

来自分类Dev

使用 Runtime.getRuntime() 在 Java 中运行命令行

来自分类Dev

使用Linux more命令时Runtime.getRuntime()。exec()挂起

来自分类Dev

使用Linux more命令时Runtime.getRuntime()。exec()挂起

来自分类Dev

java Runtime exec进程无法正常运行

来自分类Dev

使用Java的Runtime包执行Windows命令

来自分类Dev

Java:通过Runtime.getRuntime()。exec()为操作系统相关的调用转义字符串的正确方法

来自分类Dev

Java:通过Runtime.getRuntime()。exec()对与操作系统相关的调用转义字符串的正确方法

来自分类Dev

通过Runtime.exec()执行时命令失败,但在Raspberry Pi(Linux)上手动执行时命令起作用

Related 相关文章

  1. 1

    使用runtime.getruntime命令通过Java代码运行时,该命令无法成功执行

  2. 2

    java Runtime.getRuntime()。exec()无法运行命令

  3. 3

    我可以获取Java Runtime.getRuntime()。exec的真正完整执行的命令吗?

  4. 4

    Java中的cleartool命令执行-runtime()。exec

  5. 5

    如何通过具有Java管理员权限的Runtime.getRuntime.exec()运行cmd命令?

  6. 6

    Java Runtime.exec无法正确执行JSTACK命令

  7. 7

    无法通过 Runtime.exec 运行命令

  8. 8

    Runtime.getRuntime.exec()无法正常工作

  9. 9

    Java Runtime.getRuntime().exec 无法捕获所有输出

  10. 10

    带有Java Runtime.getRuntime()。exec(command)的mysqldump命令不会生成转储

  11. 11

    使用Runtime.getRuntime()。exec(String [])在Java中运行Windows命令

  12. 12

    如何使用Java'Runtime.getRuntime()。exec'命令防止cmd退出

  13. 13

    无法使用Runtime.exec()在Android Java代码中执行shell命令“ echo”

  14. 14

    无法从Runtime.getRuntime()。exec(..)启动Steam.exe

  15. 15

    Runtime.getRuntime()。exec()Java 1.7中的Perl脚本

  16. 16

    Java Runtime.getRuntime()。exec(“ ipconfig / release”); 返回错误?

  17. 17

    Java Runtime.getRuntime()。exec.getInputStream释放内容

  18. 18

    使用Runtime.getRuntime()。exec从Java启动的进程无法访问文件系统位置

  19. 19

    使用Runtime.getRuntime()。exec从Java启动的进程无法访问文件系统位置

  20. 20

    发出执行Runtime.getRuntime()。exec AWK的问题

  21. 21

    JavaScript使用java.lang.Runtime.getRuntime()运行DOS命令

  22. 22

    使用 Runtime.getRuntime() 在 Java 中运行命令行

  23. 23

    使用Linux more命令时Runtime.getRuntime()。exec()挂起

  24. 24

    使用Linux more命令时Runtime.getRuntime()。exec()挂起

  25. 25

    java Runtime exec进程无法正常运行

  26. 26

    使用Java的Runtime包执行Windows命令

  27. 27

    Java:通过Runtime.getRuntime()。exec()为操作系统相关的调用转义字符串的正确方法

  28. 28

    Java:通过Runtime.getRuntime()。exec()对与操作系统相关的调用转义字符串的正确方法

  29. 29

    通过Runtime.exec()执行时命令失败,但在Raspberry Pi(Linux)上手动执行时命令起作用

热门标签

归档