开发者

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. 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.

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 applicat开发者_StackOverflowion 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). Any suggestions? Thanks!


Here you go

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

See

  • API doc


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.

To play with the Desktop class, try the code on the File Browser GUI thread.


If you decide to go with using Runtime. I suggest:

  • Read & implement all the advice shown in When Runtime.exec() won't.
  • Use ProcessBuilder to construct the Process. ProcessBuilder even has a convenience method to merge the output streams, to make them easier to 'consume'.

You might conclude after reading that article that usingDesktop is the simpler option. There are many traps & pitfalls involved with using a Process. ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜