简体   繁体   English

Runtime.getRuntime().exec(command) 不做任何事情

[英]Runtime.getRuntime().exec(command) doesn't do anything

In my XPages application I have to call curl commands在我的 XPage 应用程序中,我必须调用 curl 命令

The code is simple:代码很简单:

public static InputStream executeCurlCommand(String finalCurlCommand) throws IOException
{
    return Runtime.getRuntime().exec(finalCurlCommand).getInputStream();
}

However, it seems that the command isn't executed at all.但是,似乎根本没有执行该命令。

When I execute the following (in Linux terminal):当我执行以下操作时(在 Linux 终端中):

curl -k -i -X GET https://someHost/fws/api/esia/login?redirectUrl=https://redirectHost/sed/gecho/2019/gecho_apps_2019.nsf/Nav2.xsp

I get the output.我得到输出。

But whenever I execute it with Java nothing works.但是每当我用 Java 执行它时,什么都不起作用。 getInputStream() is empty. getInputStream()为空。

At the same time, if I execute this code on my local Windows machine everything is fine.同时,如果我在本地 Windows 机器上执行此代码,则一切正常。 Same goes for the command executed from cmd.从 cmd 执行的命令也是如此。

But in XPages there are no exceptions, no errors, just nothing.但是在 XPage 中没有例外,没有错误,什么都没有。

Is there a way to debug it?有没有办法调试它?

UPD UPD

If I change the command to something as simple as ls -la everything works fine :x如果我将命令更改为像ls -la这样简单的命令,则一切正常:x

OK, guys, I figured it out.好的,伙计们,我想通了。 The thing was that I passed the full command to the shell.问题是我将完整的命令传递给了 shell。 It doesn't work in UNUX.它在 UNUX 中不起作用。

So所以

You should NEVER use你永远不应该使用

public static InputStream executeCurlCommand(String finalCurlCommand) throws IOException
{
    return Runtime.getRuntime().exec(finalCurlCommand).getInputStream();
}

In UNIX systems.在 UNIX 系统中。 Use ProcessBuilder instead改用ProcessBuilder

For example:例如:

ProcessBuilder pb = new ProcessBuilder(
"curl",
"-k",
"-i",
"-X",
"GET",
"http://host.com");
pb.redirectErrorStream(true);
Process p = pb.start();
InputStream is = p.getInputStream();

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

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