本文介绍了Java简单流程生成器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚为Java Process Builder进行了简单的测试代码.有4个示例,除最后一个示例外,其他所有操作均正常进行.这是我的密码

I just conducted simple test code for Java Process Builder.There are 4 examples and everything is working smoothly excluding last one.Here is my codes

public class bashProcessor {

public static void main(String args[]) {
try {
  ProcessBuilder pb;

  pb = new ProcessBuilder("/bin/bash", "-c", "touch Jin_1.sh");
  pb.start();

  pb = new ProcessBuilder("/bin/bash", "-c", "mkdir Jin_2");
  pb.start();

  pb = new ProcessBuilder("/bin/bash", "-c", "bash /home/Jin/test.sh");
  pb.start();

  //below is not working
  pb = new ProcessBuilder("/bin/bash", "-c",
      "bash /home/solr-tomcat/bin/shutdown.sh warm4 solr-instances");

  pb.start();

  System.out.println("pb job is done now");

  Thread.sleep(3000);

} catch (Exception e) {
  System.out.println(e.getMessage());
}
 }
 }

当我通过手动键入键入最后一个示例(bash/home/solr ...)时.它可以正常工作.我需要您的帮助.如果你有什么想法请让我知道这将是很大的帮助.

When I type last example(bash /home/solr...) by hand-typing. It works without any error. I need your kind help. if you have any ideaplease let me know it would be great help.

推荐答案

@ug_指出,请尝试从命令中删除 bash ,然后执行您的代码.以下代码可与多个命令配合使用.

As pointed out by @ug_ try to remove bash from command and then execute your code. Following code works fine with multiple command.

cmd.java

import java.io.BufferedReader;
import java.io.InputStreamReader;
public class cmd {
    public static void main(String[] args) {
       try {
                String[] command = new String[3];
                command[0] = "/bin/bash";
                command[1] = "-c";
                command[2] = "javac -verbose HelloWorld.java";


                Process p = Runtime.getRuntime().exec(command);
                BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
                BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
                String Error;

                while ((Error = stdError.readLine()) != null) {
                    System.out.println(Error);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
}

HelloWorld.java

public class HelloWorld {
    public static void main(String[] args) {
       System.out.println("Hello World");
    }
}

这篇关于Java简单流程生成器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 05:30
查看更多