不能在我的 Java 程序中 运行 一个 jar,没有结果没有错误

cannot run a jar in my Java Program, no result no error

我有一个 activeMq 服务器和该服务器的一个侦听器。在我的监听器中,我尝试启动一个可运行的 jar 文件。

String[] args = new String[]{
    "java",
    "-jar",
    "HelloWorld.jar"};

try {
    ProcessBuilder pb = new ProcessBuilder(args);
    Process p = pb.start();

    System.out.println("Start encrypt with: " + String.join(" ", args));

} catch (Exception e) {
    System.out.println(e.getMessage());
}

我的HelloWorld.jar

public class HelloWorld {

public static void main(String[] args) {
    File file = new File("/tmp/test/test.txt");
    file.mkdirs();      
}

}

如果我在服务器中写一个队列并且侦听器启动,我可以看到 'System.out.println("Start encrypt with*)'。使用命令 'htop' 我可以看到进程,但未创建 'console.log'。 有人有想法吗?

对不起,蹩脚的英语。

输入/输出重定向是命令 shell(cmd、bash 等)固有的功能。 java.exe 不知道这个。取而代之的是,由您的应用程序启动的进程让您可以访问其 InputStream 和 OutputStream

因此,如果您想要将输出写入 console.log,您需要阅读您的进程'InputStream 并自己写入 console.log

首先以这种方式尝试以确保您的读取不会阻碍进度:

try {
    ProcessBuilder pb = new ProcessBuilder(args);
    Process p = pb.start();

    System.out.println("Start encrypt with: " + String.join(" ", args));

} catch (Exception e) {
    System.out.println(e.getMessage());
}

我解决了这个问题。谢谢您的帮助。没有帮助我找不到答案。

问题出在我的 pom.xml 中的依赖条目。 我将条目从 activemq-all 更改为 activemq-core,它起作用了。

    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-core</artifactId>
        <version>5.7.0</version>
    </dependency>

问题是 activemq-all 库中的 spring-库。出现版本崩溃。