使用Process从Java代码执行Java程序

时间:2018-05-30 08:17:11

标签: java

我正在尝试使用Process类来执行java代码。 这是我的代码。

尝试执行的类文件是。

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

class Pro {
    public static void main(String[] args) {
        try {
            ProcessBuilder builder = new ProcessBuilder("java Demo");
            builder.redirectErrorStream(true);
            Process process = builder.start();
            InputStream is = process.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));

            String line = null;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这两个类都在不同的文件中并位于同一个文件夹中。

O / P

java.io.IOException: Cannot run program "java Demo": CreateProcess error=2, The system cannot find the file specified
        at java.base/java.lang.ProcessBuilder.start(Unknown Source)
        at java.base/java.lang.ProcessBuilder.start(Unknown Source)
        at GFG.main(GFG.java:13)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
        at java.base/java.lang.ProcessImpl.create(Native Method)
        at java.base/java.lang.ProcessImpl.<init>(Unknown Source)
        at java.base/java.lang.ProcessImpl.start(Unknown Source)
        ... 3 more

1 个答案:

答案 0 :(得分:0)

试试这样:

    private static final String LOCATION = "D:\\test.java";

    public static void main(String args[]) throws InterruptedException,IOException{

        ProcessBuilder processBuilder = new ProcessBuilder(command);

        List<String> command = new ArrayList<String>();
        command.add("javac");  //or command.add("javac -jar")
        command.add(LOCATION);

        ProcessBuilder builder = new ProcessBuilder(command);
        Map<String, String> environ = builder.environment();

        final Process process = builder.start();

        InputStream is = process.getInputStream();

        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);

        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        System.out.println("Program terminated!");
      }
    }

希望这有帮助,

附录(如何等待流程):

private String executeCommand(String command) {

    StringBuffer output = new StringBuffer();

    Process p;
    try {
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        BufferedReader reader = 
                        new BufferedReader(new InputStreamReader(p.getInputStream()));

                    String line = "";           
        while ((line = reader.readLine())!= null) {
            output.append(line + "\n");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return output.toString();
}

主叫:

public static void main(String[] args) {

    ExecuteShellComand obj = new ExecuteShellComand();

    String domainName = "https://wwww.google.com";

    //in mac oxs
    String command = "ping -c 3 " + domainName;

    //in windows
    //String command = "ping -n 3 " + domainName;

    String output = obj.executeCommand(command);

    System.out.println(output);

}

这是ping一些页面的示例,3次并等待响应。