如何通过单击 GUI 中的 JButton 来启动 .exe 文件?
我创建了一个带有 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
提前致谢
I've created a JFrame with 3 Jbuttons. I'd like the button to launch different .exe file located in the same folder. Is this possible? If yes, what should i write for the actionListener? On the other hand, instead of launching an exe file, is it possible to launch a main class using a JButton? If yes, what should i write for the actionListener?
Note: The .exe file is created from a java main program.
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
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
或使用 ProcessBuilder 类。
您应该能够在网上找到使用这些类的示例。
编辑:例如,要在 Windows 中启动记事本 exe,您可以执行以下操作:
如果您想执行一个类,那么您需要以与从命令行调用它相同的方式调用 JVM。
or use the ProcessBuilder class.
You should be able to find example on the web that use these classes.
Edit: For example to start the Notepad exe in Windows you could do:
If you want to execute a class then you need to invoke the JVM the same way you would invoke it from the command line.
您可能有兴趣了解执行外部进程。
这需要 Runtime.getRuntime().exec 或较新的 进程构建器
You may be interested looking into executing an external process.
This would require either Runtime.getRuntime().exec or the newer ProcessBuilder
我认为您会使用 Runtime.exec() 或 ProcessBuilder,类似于您不从 GUI 调用 exe 程序的方式。需要注意的一些事情是,您可能希望在 Swing 主线程(EDT)之外的后台线程上调用 Runtime.exec(),例如由 SwingWorker 对象提供的线程。否则,当 exe 程序接管 Swing 线程时,您的 GUI 可能会冻结。此外,您还需要了解这篇精彩文章中显示的有关调用 Runtime.exec() 的所有警告,
当 Runtime.exec() 不会运行时 ,可能是关于该主题的最好的文章之一——强烈推荐!
问题:我不清楚这句话是什么意思?:
I think that you would use Runtime.exec() or a ProcessBuilder similar to how you'd call an exe program if you weren't calling it from a GUI. Some things to take care about are that you'd likely want to call the Runtime.exec() on a background thread off of the Swing main thread, the EDT, such as provided by a SwingWorker object. Otherwise your GUI could freeze while the exe program takes over the Swing thread. Also you'll also want to head all of the warnings about calling Runtime.exec() shown in this great article,
When Runtime.exec() won't, possibly one of the best articles written on the subject --highly recommended!
Question: What do you mean by this statement as it isn't clear to me?:
这是一个替代答案,无需通过 Runtime.getRuntime().exec(...) 创建新进程 - 并且您也可以维护 System.in/out 通道。然而,
如果您是 Java 编程世界的新手并试图学习诀窍,我建议您遵循 camickr 的建议,不要像下面所述那样弄乱 ClassLoader。
我假设您需要运行的类是自包含的(不使用内部类)并且不在您的类路径或 jarfile 中,因此您只需创建一个实例并调用它的 main() 即可。
如果涉及多个类文件,只需重复加载它们的方法即可。
因此,在您的 JButton addActionListener()-ed 的 ActionListener 中...
您将需要一个新类 MyClassLoader 已经在您的类路径中。这是伪代码:
注意:
当您尝试加载的类驻留在本地计算机上并且您从命令行运行 java 时,这种方法效果很好。我从未成功尝试让小程序从某个 servlet 下载类文件并加载它 - 安全性不允许这样做。在这种情况下,解决方法就是在另一个窗口中运行另一个小程序,但那是另一个线程。上面的类加载解决了必须打包您可能需要的每个类文件的问题 - 只是为了启动 GUI。祝你好运,-MS
Here is an alternative answer without creating a new process through Runtime.getRuntime().exec(...) - and you can maintain your System.in/out channels, too. However,
if you are new to the world of java programming and trying to learn the ropes, I would suggest following camickr's advice and not messing with the ClassLoader as described below.
I am assuming the class you need to run is self contained (uses no inner classes) and not already in your classpath or jarfile so you could just create an instance and call its main().
If there are multiple class files involved, just repeat the method for loading them.
So, in the ActionListener that your JButton addActionListener()-ed to ...
You will need a new class MyClassLoader already in your classpath. Here is a pseudocode:
Notes:
This works well when the class you are trying to load is residing on your local machine and you are running java from the command line. I was never successful trying to get an applet to download a classfile from some servlet and load it - security will not allow that. In that case, the workaround is just to run another applet in another window, but that's another thread. The above classloading solves the problem of having to jar up every single classfile that you might be needing - just to start the GUI. Good luck, - M.S.