无法通过JAVA在Linux中运行Shell脚本

我有以下尝试在Linux OS上运行的代码。我手动运行curl命令,它按预期返回了响应,但是当我尝试通过Java运行时,它失败了,Process.waitFor()返回了非零值。

码:

 public static String runShellCmd(String url)throws Exception
    {
        String key="i_testonia";
        String value="i_dba@088922";
        String result="";
        try
        {
           // String[] command = {"curl","-k", "-i" ,"-X", "GET" ,"-H", "Accept:application/json", "-u", key.trim()+":"+value.trim() , url};
           //ProcessBuilder process = new ProcessBuilder(command); 
            ProcessBuilder process = new ProcessBuilder();
            process.command("bash","-c","curl -k -i -u i_testonia:i_dba@088922 -XGET "+url);
            Process p;
            try
            {
                p = process.start();
                BufferedReader reader =  new BufferedReader(new InputStreamReader(p.getInputStream()));
                StringBuilder builder = new StringBuilder();
                String line = null;
                while ( (line = reader.readLine()) != null) {
                    builder.append(line);
                    builder.append(System.getProperty("line.separator"));

                }
                result+=builder.toString();

                System.out.print(result);

                int exitVal = p.waitFor();

                if (exitVal == 0) {
                    System.out.println("Success!");
                    System.out.println(result);
                    System.exit(0);
                } else {
                    //abnormal...  
                     System.out.println("Failed and"+exitVal ); // return 1 as output

                }
            }
            catch (IOException e)
            {   System.out.print("error");
                e.printStackTrace();

            }

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

        }
        return result;
    }

当我在linux框中运行上述代码并查看result.length()返回0时,p.waitFor()返回1作为值。

有人可以让我知道上面代码中缺少的内容吗?我只是在终端中运行curl命令,可以看到响应JSON。但是它不能通过JAVA工作。

我尝试使用1)processBuilder.command(“”)以及2)创建String []并将该值传递给ProcessBuilder构造函数(//在上面的示例中注释那些代码)。这两个选项均无效。

评论