简体   繁体   English

如何从Java应用程序执行可运行的jar

[英]How to execute an runnable jar from java application

I have a runnable Jar which I want to invoke from my another java application. 我有一个可运行的Jar,我想从另一个Java应用程序中调用它。 I have tried this solution 我已经尝试过这种解决方案

[link] how to run a java executable jar in another java program [link] 如何在另一个Java程序中运行Java可执行jar

I am able to execute the jar but seems that threads are in deadlock and it never generates the output. 我能够执行jar,但似乎线程处于死锁状态,并且永远不会生成输出。 Can anyone please help me over this? 有人可以帮我吗?

You can use ProcessBuilder from plain Java or use library like zt-exec from ZeroTurnaround. 您可以使用纯Java中的ProcessBuilder或使用ZeroTurnaround中的zt-exec之类的库。 Second tools allows you to easily add for example execution timeout, set exit values, etc 第二个工具可让您轻松添加例如执行超时,设置退出值等

In first case it'll looks like: 在第一种情况下,它看起来像:

Process p = new ProcessBuilder("java", "-jar myjar.jar").start();
p.start();

In second: 第二:

new ProcessExecutor().command("java -jar myjar.jar").execute();

Another way, if you know the class which have main method, your program invoke the main method form jar. 换句话说,如果您知道具有main方法的类,则程序将调用jar形式的main方法。

Example.java Example.java

public void test() {
    String[] starr = <- if you have some parameter
    ProgramInJar.main(starr);
}

mytest.jar mytest.jar

public class ProgramInJar {
    public static void main(String[] args) {
        // your operation
    }
}

I have found the solution. 我找到了解决方案。 I have updated my JDK and then invoke the jar and it worked. 我已经更新了我的JDK,然后调用了jar并成功了。 Thank you all for the help and time. 谢谢大家的帮助和时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM