4

我创建了一个带有 3 个 Jbutton 的 JFrame。我希望该按钮启动位于同一文件夹中的不同 .exe 文件。这可能吗?如果是,我应该为 actionListener 写什么?另一方面,是否可以使用 JButton 启动主类,而不是启动 exe 文件?如果是,我应该为 actionListener 写什么?

注意:.exe 文件是从 java 主程序创建的。

JButton button1 = new JButton("Program1"); //The JButton name.
frame.add(button1); //Add the button to the JFrame.
button1.addActionListener().... // how to launch the .exe file

提前致谢

4

4 回答 4

4
Runtime.getRuntime().exec( ... );

或使用 ProcessBuilder 类。

您应该能够在网络上找到使用这些类的示例。

编辑:例如,要在 Windows 中启动记事本 exe,您可以执行以下操作:

Process process = Runtime.getRuntime().exec( "cmd.exe /C start notepad" );

如果你想执行一个类,那么你需要像从命令行调用它一样调用 JVM。

于 2011-01-23T04:29:10.117 回答
1

您可能有兴趣研究执行外部进程

这将需要Runtime.getRuntime().exec或更新的ProcessBuilder

于 2011-01-23T04:29:10.537 回答
1

我认为如果你不是从 GUI 调用它,你会使用 Runtime.exec() 或类似于调用 exe 程序的 ProcessBuilder。需要注意的一些事情是,您可能希望在 Swing 主线程(EDT)之外的后台线程上调用 Runtime.exec(),例如由 SwingWorker 对象提供的。否则,当 exe 程序接管 Swing 线程时,您的 GUI 可能会冻结。此外,您还需要关注这篇很棒的文章中显示的有关调用 Runtime.exec() 的所有警告, 当 Runtime.exec() 不会时,可能是关于该主题的最佳文章之一——强烈推荐!

问题:由于我不清楚,您的这个陈述是什么意思?:

注意:program.exe 文件是从 java 主程序创建的。

于 2011-01-23T04:30:46.540 回答
1

这是一个替代答案,无需通过 Runtime.getRuntime().exec(...) 创建新进程 - 您也可以维护 System.in/out 通道。但是,如果您是 Java 编程世界的新手并试图学习其中的技巧,我建议您遵循 camickr 的建议,不要像下面描述的那样弄乱 ClassLoader。

我假设您需要运行的类是自包含的(不使用内部类)并且不在您的类路径或 jarfile 中,因此您可以创建一个实例并调用它的 main()。如果涉及多个类文件,只需重复加载它们的方法即可。

因此,在您的 JButton addActionListener() 的 ActionListener 中...

public void actionPerformed (ActionEvent e) {
    String classNameToRun = e.getActionCommand(); // Or however you want to get it
    try {
        new MyClassLoader().getInstance(classNameToRun).main (null);
    } catch (ClassNotFoundException ce) {
        JOptionPane.showMessageDialog (null, "Sorry, Cannot load class "+classNameToRun,
                            "Your title", JOptionPane.ERROR_MESSAGE);
}}

您将需要一个新类 MyClassLoader 已经在您的类路径中。这是一个伪代码:

import java.io.*;
import java.security.*;

public class MyClassLoader extends ClassLoader {

        protected       String classDirectory = "dirOfClassFiles" + File.separator,
                                packageName = "packageNameOfClass.";

        /**
         * Given a classname, get contents of the class file and return it as a byte array.
         */

        private byte[] getBytes (String className) throws IOException {
                byte[] classBytes = null;
                File file = new File (classDirectory + className + ".class");

                // Find out length of the file and assign memory
                // Deal with FileNotFoundException if it is not there
                long len = file.length();
                classBytes = new byte[(int) len];

                // Open the file
                FileInputStream fin = new FileInputStream (file);

                // Read it into the array; if we don't get all, there's an error.
                // System.out.println ("Reading " + len + " bytes");
                int bCount = fin.read (classBytes);
                if (bCount != len)
                        throw new IOException ("Found "+bCount+" bytes, expecting "+len );

                // Don't forget to close the file!
                fin.close();
                // And finally return the file contents as an array
                return classBytes;
        }

        public Class loadClass (String className, boolean resolve)
                                                throws IOException, ClassNotFoundException,
                                                IllegalAccessException, InstantiationException {
                Class myClass = findLoadedClass (packageName + className);
                if (myClass != null)
                        return myClass;

                byte[] rawBytes = getBytes (className);
                myClass = defineClass (packageName + className,
                                                rawBytes, 0, rawBytes.length);
                // System.out.println ("Defined class " +packageName + className);
                if (myClass == null)
                        return myClass;
                if (resolve)
                        resolveClass (myClass);

                return myClass;
        }

        public Object getInstance (String className) throws ClassNotFoundException {
                try {
                        return loadClass (className, true).newInstance();
                } catch (InstantiationException inExp) {        inExp.printStackTrace();
                } catch (IllegalAccessException ilExp) {        ilExp.printStackTrace();
                } catch (IOException ioExp) {                   ioExp.printStackTrace();
                }
                return null;
        }
}

注意:当您尝试加载的类驻留在本地计算机上并且您从命令行运行 java 时,这很有效。我从来没有成功地尝试让一个小程序从某个 servlet 下载一个类文件并加载它——安全性不允许这样做。在这种情况下,解决方法就是在另一个窗口中运行另一个小程序,但那是另一个线程。上面的类加载解决了你可能需要的每一个类文件的问题 - 只是为了启动 GUI。祝你好运 - 女士

于 2011-01-23T07:05:20.467 回答