How to run a program from GUI?

I want to know how can I run another java program from a GUI. I want to place a button which will run a program if clicked.
[130 byte] By [fde_cpua] at [2007-11-27 3:30:12]
# 1
Check out the Runtime class.
camickra at 2007-7-12 8:33:14 > top of Java-index,Desktop,Core GUI APIs...
# 2

What type of program? A jar? A classfile?

The easy way would be to add the jar to your classpath, then just call its main class.

If it's just a class file, drag it to your working directory, and call it that way.

so lets say your program is called Mario.java, and the program you want to run is "Davey.jar" which is in the same folder...

Compile:

c:\> javac -cp .;Davey.jar Mario.java

Run:

c:\> java -cp .;Davey.jar Mario

Code:

import com.code.Davey; // You need to know what the main class is called

public class Mario() {

// Run Davey.jar

Davey j = new Davey();

j.setVisible(true);

}

In the event you want to do it dynamically, its gets tricky, but its possible, just ask.

-FBL

FBLa at 2007-7-12 8:33:14 > top of Java-index,Desktop,Core GUI APIs...
# 3
But how do I run a word file or a pps?
fde_cpua at 2007-7-12 8:33:14 > top of Java-index,Desktop,Core GUI APIs...
# 4
hi!which version of jdk you are using?regardsAniruddha
Aniruddha-Herea at 2007-7-12 8:33:14 > top of Java-index,Desktop,Core GUI APIs...
# 5
> But how do I run a word file or a pps? http://forum.java.sun.com/thread.jspa?forumID=57&threadID=5123633
camickra at 2007-7-12 8:33:14 > top of Java-index,Desktop,Core GUI APIs...
# 6
But how exactly can I make the connection...{if button clicked then open file} because I don't understand how does it work?
fde_cpua at 2007-7-12 8:33:14 > top of Java-index,Desktop,Core GUI APIs...
# 7

hi!

i asked you which version of jdk you are using. if you are using JDK1.6 then there is a class called java.awt.Desktop

explore that class. if you are not using JDK1.6 then you have write a class like this class. for windows you have run a pps/word file using rundll and as a process camickr described. and you have to check how to run in other OS (like Linux, MAC).

regards

Aniruddha

Aniruddha-Herea at 2007-7-12 8:33:14 > top of Java-index,Desktop,Core GUI APIs...
# 8

I've got JDK1.6 and I searched more about java.awt.Desktop

but I still don't know how to do it....

public void actionPerformed ( ActionEvent e){

String command = e. getActionCommand ();

if ( command.equals("Open"))

//what command shall I write here so that I can open a program?

fde_cpua at 2007-7-12 8:33:14 > top of Java-index,Desktop,Core GUI APIs...
# 9
hi!Desktop.getDesktop().open(File file) here you provide your pps, ppt or doc or what ever file name.i hope this will work.regardsAniruddha
Aniruddha-Herea at 2007-7-12 8:33:14 > top of Java-index,Desktop,Core GUI APIs...
# 10

well it doesn't work.I've wrote

Desktop.getDesktop().open(File Java1.class); and it gives me this errors

Program.java:57: ')' expected

Desktop.getDesktop().open(File Java1.class);

^

Program.java:57: not a statement

Desktop.getDesktop().open(File Java1.class);

^

Program.java:57: ';' expected

Desktop.getDesktop().open(File Java1.class);

^

fde_cpua at 2007-7-12 8:33:14 > top of Java-index,Desktop,Core GUI APIs...
# 11

[nobr]can you please post your code here....

import java.awt.Desktop;

import java.io.File;

/**

* @author: Aniruddha<br>

* @date: May 14, 2007, 9:49:53 AM<br>

* @source: OpenFile.java<br>

* @project: HelpForum<br>

*/

public class OpenFile

{

/**

* @author: Aniruddha<br>

* @date: May 14, 2007, 9:49:53 AM<br>

* @param args

*/

public static void main(String[] args)

{

try

{

Desktop.getDesktop().open(new File("c:\\test.txt"));

}

catch(Throwable a_th)

{

}

}

}

in my machine this is working fine. provided you have test.txt file in your c drive

regards

Aniruddha[/nobr]

Aniruddha-Herea at 2007-7-12 8:33:14 > top of Java-index,Desktop,Core GUI APIs...
# 12

import java.awt.*;

import java.awt.event.*;

import java.awt.Desktop;

import java.io.File;

public class Program extends Frame implements ActionListener{

public Program (String titlu){

super(titlu);

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

setSize(400,400);

setBackground(Color.lightGray);

Label acces=new Label("Introduceti numele si parola!");

acces.setText("");

add(acces);

Panel adaugare=new Panel();

Button adauga=new Button("Adauga un element");

adaugare.add(adauga);//adaugam panel pe suprafata ferestrei

adauga.setForeground(Color.red);//stabilim culoarea pentru panel

add(adaugare,BorderLayout.NORTH);//adaugam panel pe suprafata ferestrei

Button b1=new Button("Java1");

add(b1,BorderLayout.WEST);

Button b2=new Button("Java2");

add(b2,BorderLayout.EAST);

Button b3=new Button("Exit");

add(b3,BorderLayout.SOUTH);

MenuBar mb=new MenuBar();

Menu fisier=new Menu("File");

fisier.add(new MenuItem("Exit"));

mb.add(fisier);

setMenuBar(mb);

fisier.addActionListener(this);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

adauga.addActionListener(this);

}

public void actionPerformed ( ActionEvent e){

String command = e. getActionCommand ();

if ( command.equals("Java1"))//the errorDesktop.getDesktop().open(new File("c:\\test.txt"));

}

// Valabila si pentru meniu si pentru buton

public static void main(String args[]){

//throws IOException{

//Runtime rt = Runtime.getRuntime();

Program f=new Program("Atestat");

f.show();f.setResizable(false);

}

}

fde_cpua at 2007-7-12 8:33:14 > top of Java-index,Desktop,Core GUI APIs...
# 13
hi!do you have c:\\test.txt in your machine? you have to provide a valid file name as a parameter. and what exception you are getting? please provide the exception.regardsAniruddha
Aniruddha-Herea at 2007-7-12 8:33:14 > top of Java-index,Desktop,Core GUI APIs...
# 14
Thank you very much....Now it works....Can you tell me how can I show to the user,in a new window, the values of an array?
fde_cpua at 2007-7-12 8:33:14 > top of Java-index,Desktop,Core GUI APIs...