[英]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!谢谢!
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:我建议:
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.