ProcessBuilder 不工作

ProcessBuilder not working

我正在实施调用另一个 java 程序的 ProcessBuilder 程序。但是,我发现 class 未找到。

该程序仅产生以下输出:

Error: Could not find or load main class HelloWorld

Program complete

public class ProcessBuilderSample {
    public static void main(String args[]) {
        try {
            ProcessBuilder broker = new ProcessBuilder("java.exe", "-cp",
                "F:\LunaWorkspace\ProcessBuilderTest\bin" ,"com\hello\HelloWorld");  

            Process runBroker = broker.start();                     

            BufferedReader reader = new BufferedReader(new InputStreamReader(runBroker.getInputStream()));

            BufferedReader reader1 = new BufferedReader(new InputStreamReader(runBroker.getErrorStream()));

        String str=null;

        while((str=reader.readLine())!=null){
            System.out.println(str);
        }

        while((str=reader1.readLine())!=null){
            System.out.println(str);
        }

            runBroker.waitFor();

            System.out.println("Program complete");

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

这是我要执行的 java 程序。该程序生成 Hello World 作为输出。

package com.hello;

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

现在我正在使用:

ProcessBuilder 代理 = new ProcessBuilder("java.exe", "-cp", "F:\LunaWorkspace\ProcessBuilderTest\bin" ,"com\hello\HelloWorld");

此命令适用于命令提示符,但不适用于 processbuilder。

编辑:

完整class路径:

ProcessBuilderSample.class:

F:\LunaWorkspace\ProcessBuilderExample\bin\com\sample

HelloWorld.class:

F:\LunaWorkspace\ProcessBuilderTest\bin\com\hello Thanks!!

需要修复你的 HelloWorld class 构建进程构建器的名称:

"com\hello\HelloWorld" -> "com.hello.HelloWorld"