简体   繁体   English

在java中运行外部程序会产生与terminal命令不同的结果

[英]Running external program within java produces different results from terminal command

Hey I'm trying to run a command within java : 嘿我正在尝试在java运行命令:

String curlCommand=String.format("curl --max-time %d --socks5 %s -A \"%s\"  -i %s",maxWait,proxy ,getAgent(), myurl);
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(curlCommand);

For some reason executing this line would result in cookies to be denied. 由于某种原因,执行此行将导致cookies被拒绝。

however, when I run the curl program from terminal I have no problem and everything works as it should be 但是,当我从终端运行卷曲程序时,我没有任何问题,一切正常

here is the result if I run the program Java 这是我运行程序Java

Date: Tue, 03 Mar 2015 19:20:38 GMT
X-Li-Pop: prod-lva1
X-LI-UUID: 8C548z0TyBMwY002tCoAAA==
Set-Cookie: denial-reason-code=3,2,8; Max-Age=5
Set-Cookie: denial-client-ip=xxxxxxx; Max-Age=5
Content-Length: 1852
Content-Type: text/html

here is the result if I run exact command using curl in terminal : 如果我在terminal使用curl运行完全命令,结果如下:

HTTP/1.1 200 OK
Server: Play
X-SSR-Engine-Init: <xxxxxxx>
X-FS-UUID: 0e69d33f4913c813b05f1aefb42a0000
X-Page-Speed: 1
Date: Tue, 03 Mar 2015 19:21:27 GMT
Content-Length: 31113
Content-Type: text/html; charset=utf-8
X-Frame-Options: sameorigin
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Li-Fabric: prod-lva1
Strict-Transport-Security: max-age=0
Set-Cookie: JSESSIONID="ajax:4773794645302404176"; Path=/; Domain=<xxxxxx>; HTTPOnly
Set-Cookie: bcookie="v=2&da2801ec-e7e2-4b04-8244-401d6ac0e7c3"; domain=xxxxx; Path=/; Expires=Fri, 03-Mar-2017 06:58:59 GMT
Set-Cookie: bscookie="v=1&2015030319212713ed4940-eb5d-4860-8c30-2eec4819157aAQHMGWOb0Fiv_feTewpb6Rw3TGGAGQ_Q"; domain=<xxxxxx>; Path=/; Secure; Expires=Fri, 03-Mar-2017 06:58:59 GMT; HttpOnly
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: no-cache, no-store
Connection: keep-alive
X-Li-Pop: prod-lva1
X-LI-UUID: DmnTP0kTyBOwXxrvtCoAAA==
Set-Cookie: lidc="b=VB97:g=149:u=1:i=1425410487:t=1425496887:s=AQHAOhdkFxryw00Wo64LkfjsYb8Q1hqZ"; Expires=Wed, 04 Mar 2015 19:21:27 GMT; domain=<xxxxx>; Path=/

So My question is why should it be different at all, am I missing something out here? 所以我的问题是为什么它应该是完全不同的,我在这里遗漏了什么?

Alright I finally solved it. 好吧,我终于解决了它。

the reason is Runtime runtime = Runtime.getRuntime(); 原因是Runtime runtime = Runtime.getRuntime();

it should not be used. 不应该使用它。 as it just messes up with the parameters (eventhough the print out of the command looks fine, it just didn't send the right command to the OS) instead I should have used ProcessBuilder class . 因为它只是弄乱了参数(虽然命令打印出来看起来很好,但它只是没有向OS发送正确的命令)而是我应该使用ProcessBuilder class it worked like a charm. 它就像一个魅力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM