如何在其他Java程序中使用ProcessBuilder运行Java程序。 (使用-cp和-Xbootclasspath命令)

时间:2015-11-16 06:43:52

标签: java process java-5

这是我的代码。

public class Test {
    public static void main(String[] args) {
        String directory = System.getProperty("user.home") + File.separator + "cache";
        System.out.println(directory); // "/Users/byron1st/cache"
        try{
            ProcessBuilder builder = new ProcessBuilder("java",
                    "-cp", directory,
                    "-Xbootclasspath/p:", directory,
                    "framework.PFSystemMain");
            builder.redirectErrorStream(true);
            builder.redirectOutput(new File(System.getProperty("user.home") + "/output.txt"));
            builder.start();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}

该程序使用-cp和-Xbootclasspath命令构建运行java程序的过程。目标程序的类位于/Users/byron1st/cache文件夹中。

enter image description here

我想要做的是运行java -cp /Users/byron1st/cache -Xbootclasspath/p: /Users/byron1st/cache framework.PFSystemMain

(我使用Xbootclasspath/p:的原因是我有一些用于记录的检测类。)

此代码无法运行该过程,只会产生错误消息,意味着"它无法找到或加载名为' .Users.byron1st.cache'"的默认类。 (我很抱歉直接显示错误消息,因为它是用韩文写的。)

我的代码使用ProcessBuilder有什么问题?

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。这是因为'Xbootclasspath/p:'。 我改变了我的代码如下:

ProcessBuilder builder = new ProcessBuilder("java",
                "-cp", directory,
                "-Xbootclasspath/p:" + directory,
                "framework.PFSystemMain");

实际上,我认为-Xbootclasspath / p:和它的目录路径是processbuilder的单独参数,但事实并非如此。