无法使用-cp和jar位置使用Java ProcessBuilder查找或加载类 [英] Can not find or load class with Java ProcessBuilder using -cp and jar location

查看:67
本文介绍了无法使用-cp和jar位置使用Java ProcessBuilder查找或加载类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ProcessBuilder 在我的 Java 应用程序中启动JUnit测试.

I am trying to use ProcessBuilder to start a JUnit test within my Java Application.

我能够从命令行运行相同的命令而不会出现问题.从ProcessBuilder运行时,我需要使用jar的绝对路径吗?还是可以使用相对路径?

I am able to run the same command from the command line without issue. Do I need to use the absolute path for the jar when running from ProcessBuilder or can I use the relative path?

在命令行上运行

java -cp .;lib/junit-4.12.jar org.junit.runner.JUnitCore com.test.Test1

在我的应用程序中运行

junit库位于lib文件夹中

The junit library is in the lib folder

application/lib/junit-4.12.jar

application/lib/junit-4.12.jar

        ProcessBuilder builder = new ProcessBuilder(new String[] {"java", "-cp", ".;lib/junit-4.12.jar", "com.test.Test1"});

        Process process = builder.start();
        process.waitFor();

        debug("process ended");
        debug("process.exitValue() = " + process.exitValue());

输出:

process ended
process.exitValue() = 1
Error: Could not find or load main class org.junit.runner.JUnitCore

推荐答案

使用以下命令运行应用程序时,可以找到类路径是什么 System.getProperty("java.class.path"); 然后,在您的 ProcessBuilder()语句中相应地修改"cp".

You can find what the classpath is when you run your application by using System.getProperty("java.class.path"); Then, modify "cp" in your ProcessBuilder() statement accordingly.

这篇关于无法使用-cp和jar位置使用Java ProcessBuilder查找或加载类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆