1

I have a runnable Jar which I want to invoke from my another java application. I have tried this solution

[link] how to run a java executable jar in another java program

I am able to execute the jar but seems that threads are in deadlock and it never generates the output. Can anyone please help me over this?

3
  • That is proper way as it is shown in link. Does that jar run properly if you run it separately? Commented Jan 15, 2014 at 8:42
  • Yes, if I run the Jar from a bat file, it works fine. Commented Jan 15, 2014 at 8:53
  • If I run the Jar from java program, jar runs fine. But at the end where it needs to generate the output, it stuck. Commented Jan 15, 2014 at 8:55

3 Answers 3

1

You can use ProcessBuilder from plain Java or use library like zt-exec from ZeroTurnaround. 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(); 
Sign up to request clarification or add additional context in comments.

4 Comments

I have tried this but it worked the same and application hangs.
By the application you mean executed jar or your application that is invoking execution?
It's my application which is invoking jar. Actually application doesn't get the control back from the JAR and it keep waiting. Moreover, I saw in debug window that there are two threads. Thread which is in the link I mentioned above and second is the DestroyVM thread. They never get the control.
I have used process executor now. Application is able to call the jar successfully. But seems that jar is throwing an exception of illegal method exception "Exception in thread "main" java.lang.NoSuchMethodError: java.lang.Integer.compare(II)I".
0

Another way, if you know the class which have main method, your program invoke the main method form jar.

Example.java

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

mytest.jar

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

Comments

0

I have found the solution. I have updated my JDK and then invoke the jar and it worked. Thank you all for the help and time.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.