简体   繁体   English

如何从 Linux 中的 Java GUI 启动外部应用程序?

[英]How can I launch an external application from a Java GUI in Linux?

I'm creating a Java application that helps people to learn Chinese.我正在创建一个帮助人们学习中文的 Java 应用程序。 I've already created a Java GUI but I'm struggling to work out how to create a button that launches an external application in a new window.我已经创建了一个 Java GUI,但我正在努力研究如何在新的 window 中创建一个启动外部应用程序的按钮。

I've looked up various tutorials on process, desktop and runtime but they all seem to deal with outputting data on the console, and I can't figure out how to apply them to this case.我查看了有关进程、桌面和运行时的各种教程,但它们似乎都处理在控制台上输出数据,我不知道如何将它们应用于这种情况。

Any help at all would be greatly appreciated!任何帮助将不胜感激! Thanks!谢谢!

EDIT编辑

So I've incorporated the runtime code into my class and I've got it to list the contents of my file but can't get it to launch the application using "/home/kate/Desktop/PTAMM./PTAMM" or "./PTAMM /home/kate/Desktop/PTAMM" or "./ home/kate/Desktop/PTAMM PTAMM" (I tried the last two out of desperation).因此,我已将运行时代码合并到我的 class 中,并且我已经让它列出了我的文件的内容,但无法使用“/home/kate/Desktop/PTAMM./PTAMM”或“启动应用程序” ./PTAMM /home/kate/Desktop/PTAMM" 或 "./home/kate/Desktop/PTAMM PTAMM" (我绝望地尝试了最后两个)。 Any suggestions?有什么建议么? Thanks!谢谢!

Here you go给你 go

Runtime.getRuntime().exec("command to launch executable");

See

I've looked up various tutorials on process, desktop and runtime but they all seem to deal with outputting data on the console,我查看了有关进程、桌面和运行时的各种教程,但它们似乎都处理在控制台上输出数据,

No that is wrong!不,那是错的! Desktop.open(File) .. 桌面.打开(文件) ..

Launches the associated application to open the file.启动关联的应用程序以打开文件。

(Emphasis mine) (强调我的)

So Desktop.open(new File("word.doc")) might open MS Word or the Open Office Writer, while Desktop.open(new File("spreadsheet.xls")) might pop MS Excel of OO Calc.所以Desktop.open(new File("word.doc"))可能会打开 MS Word 或 Open Office Writer,而Desktop.open(new File("spreadsheet.xls"))可能会弹出 OO Calc 的 MS Excel。

To play with the Desktop class, try the code on the File Browser GUI thread.要使用Desktop class,请尝试文件浏览器 GUI线程上的代码。


If you decide to go with using Runtime .如果您决定使用Runtime来 go 。 I suggest:我建议:

  • Read & implement all the advice shown in When Runtime.exec() won't .阅读并实施When Runtime.exec() won't中显示的所有建议。
  • Use ProcessBuilder to construct the Process .使用ProcessBuilder构造Process ProcessBuilder even has a convenience method to merge the output streams, to make them easier to 'consume'. ProcessBuilder甚至有一种方便的方法来合并 output 流,使它们更容易“使用”。

You might conclude after reading that article that using Desktop is the simpler option.阅读那篇文章后,您可能会得出结论,使用Desktop是更简单的选择。 There are many traps & pitfalls involved with using a Process .使用Process涉及许多陷阱和陷阱。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Linux启动Java GUI测试 - how to launch java GUI test from linux 如何在不支持GUI的无头Linux上运行Java GUI应用程序? - How can I run a Java GUI application on a headless Linux that does not support GUI? Java:如何将外部类加载到GUI中 - Java: How can I load an external class into a gui 从Java GUI应用程序启动Windows批处理文件 - Launch a windows batch file from Java GUI Application 当我启动我的java应用程序时,我可以在我的linux控制台上获取路径吗? - When I launch my java application, can I get the path on my linux console? 如何使用launch4j包装还提供命令行界面的GUI应用程序? - How can I use launch4j to wrap a GUI application that also provides a command line interface? 如何从Java应用程序启动git命令序列? - How could I launch the sequence of git commands from java application? Linux(Ubuntu)中的Java系统托盘应用程序从服务(守护程序)启动 - Java System Tray Application in Linux (Ubuntu) launch from service (daemon) 如何从外部Java应用程序将对象放入MB队列? - How can I put an object into a MB queue from an external Java application? 如何在Linux,Mac和Windows上从Java启动.NET应用程序? - How can I start a .NET application from Java on Linux, Mac and Windows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM