2

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 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). Any suggestions? Thanks!

Kate
  • 21
  • 3

2 Answers2

6

Here you go

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

See

jmj
  • 237,923
  • 42
  • 401
  • 438
  • @Kate, the above is what you need, you just have to replace the String with what you normally used to launch that external app from the command line/console – dm76 Jul 13 '11 at 09:43
  • Thanks so much for your help! I run the application by using ./PTAMM. So would this command work?: home/kate/Desktop/PTAMM ./PTAMM? I would try it now but I'm not at the stage where I'm able to do so yet. – Kate Jul 13 '11 at 09:54
  • 2
    For better understanding of Runtime.exec() read http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1 – Adriaan Koster Jul 13 '11 at 10:23
0

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

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks for your help, I looked up Desktop.open but all I can see that it does is open a file, which I don't want to do, I want to launch an application. Believe me I'd LOVE to use the easier option but I think Runtime is better suited to this. – Kate Jul 14 '11 at 16:11
  • How exactly does opening a program (with no source document), help people to learn Chinese? What specific program(s) are of interest, and why? – Andrew Thompson Jul 14 '11 at 16:18
  • It's an augmented reality program called PTAMM that I'm using to superimpose Chinese words on top of real life objects. – Kate Jul 14 '11 at 17:44