0

I have written a Java program that should open another program (which is a .jar file). If I use this code in the main class of my program, all works correctly:

File logFile = new File("./ePaymentUpdater.jar");
Desktop.getDesktop().open(logFile.getCanonicalFile());
//or
Runtime.getRuntime().exec("java -jar ePaymentUpdater.jar");

But if I paste the same code in the event in response to the user clicking a button, it doesn't work as it should: The program seems to run, because it creates a folder like it should be (this code is in the main class of the called program), but it doesn't show the jFrame it should

It seems that i cannot open a frame from inside the frame of another program...

This is my main class:

package prove_idiote;
import java.awt.Desktop;
import java.io.File;
import javax.swing.JOptionPane;

public class Main {

    public static void main(String[] args) {  

//        try {
//            Runtime.getRuntime().exec("java -jar ePaymentUpdater.jar");
//        } catch (Exception e) {            
//            System.out.println(e);
//        }

        Tester tester = new Tester();
        tester.setVisible(true);       
    }   
}

And this is my button event:

private void ExecuteActionPerformed(java.awt.event.ActionEvent evt) {                                         
    try {

        JOptionPane.showMessageDialog(null, "before" ,"ATTENZIONE!",JOptionPane.WARNING_MESSAGE);

        Runtime.getRuntime().exec("java -jar ePaymentUpdater.jar");

        JOptionPane.showMessageDialog(null, "after" ,"ATTENZIONE!",JOptionPane.WARNING_MESSAGE);

    } catch (Exception e) {            
        System.out.println(e);
    }
} 
6
  • Define "doesn't work"! What happens when you execute that code in the event handler? Any error/messages? Commented May 24, 2012 at 10:16
  • Desktop.open() uses windows file associations for opening and executing programs. If they are not correctly set it will not work. Does the ePaymentUpdater.jar have a MANIFEST.MF file with main class set? Commented May 24, 2012 at 10:19
  • No error message, it simply seems that the .jar file called doesn't run PS: i see now that the .jar DOES run, because it creates a folder as it should be, but it doesn't show what it should (a jForm) Commented May 24, 2012 at 10:23
  • Can you provide some code on how you process the event? It will help to test the code locally. Commented May 24, 2012 at 11:05
  • Is there some reason for this to run in a separate process than the one that launched your code? Commented May 24, 2012 at 11:24

3 Answers 3

4

You can use the Runtime class of java and process your jar from there as follows:

Runtime.getRuntime().exec("java -jar ./ePaymentUpdater.jar")

Where Runtime handles all the processes at runtime and executes them one by one.

Sign up to request clarification or add additional context in comments.

Comments

1
Runtime.getRuntime().exec("java -jar yourjarfile")

1 Comment

I've also tried this road, but the result is the same: works if launched in the main, doesn't work if launched in response to the button click
1

I finally found the problem (and the solution) There were some missing libraries in the called .jar (i've put the two jars in the same folder, so they shared the same libs, but one of them was using a lib that was missing)

Thnaks for the hints

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.