无法通过java Runtime.getRuntime()执行CURL命令。exec() [英] Unable to execute CURL command through java Runtime.getRuntime().exec()

查看:1588
本文介绍了无法通过java Runtime.getRuntime()执行CURL命令。exec()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  curl -i --userOAMADMIN_tenant_358922247351079_svc_358922247369079_APPID:Iuj.2swilg5fhv HContent-Type:application / json-HAccept:application / json-H'X-USER-IDENTITY-DOMAIN-NAME:tenant_358922247351079'-HX-RESOURCE-IDENTITY-DOMAIN-NAME:tenant_358922247351079 -request GEThttps://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 +\\\
);
}

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

return output.toString();

}

尝试手动执行curl命令,
然后我打印标准错误,我可以看到:

  [testng] error line = 
[testng]错误行= curl:(6)无法解析主机'application'
[testng]错误行=
[testng]应用程序'
[testng]错误行=
[testng]错误行= curl:(6)无法解析主机'tenant_359516638431079'
[testng]错误行=
[testng] error line = curl:(6)无法解析主机'tenant_359516638431079''
[testng]错误行=
[testng]错误行= curl:当curl命令手动执行时,它的工作正常,那么为什么不通过Runtime.getRuntime()?



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

解决方案

看起来像数据shell /控制台解释/更改字符。例如以下行:



-HContent-Type:application / json



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



-H Content- 应用程序 / json



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



exec(String [] cmdarray)



这样,shell /控制台就会清楚哪些参数被分组在一起。



这里是groovy中的测试,证明了这一点:

  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 -HAccept:application / json--request GEThttps://education.cloudant.com/''')
assert response ['sout'] =='
assert response ['serr']。startsWith('curl:(6)无法解析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}')
['serr'] ==''


I am executing a curl command using java.

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"

I want to get the output of this curl command in my code,but my stdoutput is coming out to be empty.

 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();

    }

Tried executing the curl command manually, its working fine. Then I printed the standard error, and I can see:

   [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

When curl command is executed manually, its working fine then why not through Runtime.getRuntime() ?

Kindly suggest!! Any help will be appreciated.

解决方案

It seems like the data shell/console is interpreting/changing characters. For example the following line:

-H "Content-Type: application/json"

... seems to be getting interpreted as three different arguments:

-H Content-Type: and application and /json by the shell/console.

Try breaking the command string down into an array of components using the format:

exec(String[] cmdarray)

That way it will be clear to the shell/console which arguments are grouped together.

Here is a test in groovy that proves the point:

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'] == ''

这篇关于无法通过java Runtime.getRuntime()执行CURL命令。exec()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆