English 中文(简体)
Java 如何从代码中运行另一个.jar 程序以响应单击按钮
原标题:Java how to run another .jar program from code in response to button click
  • 时间:2012-05-24 10:12:09
  •  标签:
  • java

我写了一个 Java 程序, 它应该打开另一个程序(. jar 文件 ) 。 如果我在程序的主要类中使用这个代码, 所有的操作都是正确的 :

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

看来我无法从另一个程序的框架里打开一个框架...

这是我的主班:

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);       
    }   
}

这是我的按钮事件:

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);
    }
} 
最佳回答

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)

提示的线索

问题回答

您可以使用“运行时间”类java,并从那里处理您的罐子,具体如下:

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

Runtime 处理运行时所有进程并逐个执行的地方。

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签