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

4

这是我的代码。

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个回答

3
我解决了这个问题。原因是 'Xbootclasspath/p:'。 我将我的代码更改为以下内容:
ProcessBuilder builder = new ProcessBuilder("java",
                "-cp", directory,
                "-Xbootclasspath/p:" + directory,
                "framework.PFSystemMain");

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接