LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-27-2005, 01:37 PM   #1
Diederick
LQ Newbie
 
Registered: Apr 2005
Posts: 25

Rep: Reputation: 15
Opening programs in Java


I want to know if one kan use java to open another program from within your java program, and how or what to use to accomplish this.

Thanx Diederick
 
Old 06-27-2005, 01:49 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Hi.

You can indeed:
Code:
runEnviron = Runtime.getRuntime();
try {
    Process burnProcess = runEnviron.exec("/usr/bin/myOtherProgram");
} catch (IOException e) {
    System.out.println("Problem!");
}
Dave
 
Old 06-27-2005, 02:21 PM   #3
Diederick
LQ Newbie
 
Registered: Apr 2005
Posts: 25

Original Poster
Rep: Reputation: 15
Thanx but one more problem

What is runEnviron? I have never used this before so i'm still new at this.

Thanx
 
Old 06-27-2005, 03:14 PM   #4
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Oops. My mistake. I had runEnviron declared at the top of the class.
Code:
private Runtime runEnviron;
runEnviron = Runtime.getRuntime();
try {
    Process burnProcess = runEnviron.exec("/usr/bin/myOtherProgram");
} catch (IOException e) {
    System.out.println("Problem!");
}
Dave
 
Old 06-27-2005, 03:20 PM   #5
Diederick
LQ Newbie
 
Registered: Apr 2005
Posts: 25

Original Poster
Rep: Reputation: 15
Almost done.

Thanx Dave it's working now!!! The last thing i want to know is how to send the selected file to that new program sothat it opens the file when the program opens. I used a JFile chooser to get the file.
 
Old 06-27-2005, 03:38 PM   #6
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
OK, something like this should give you the path to the file in 'newSource'

Code:
String newSource = "";
final JFileChooser sourceChooser = new JFileChooser();
int returnValue = sourceChooser.showOpenDialog(caller);
if (returnValue == JFileChooser.APPROVE_OPTION) {
	File sourceFile = sourceChooser.getSelectedFile();
	newSource=sourceFile.getAbsolutePath();
}
'caller' is the JButton which launced the ActionEvent.
Things get a bit interesting after that. If there are any spaces in the file's path, you'll have problems if you just do
Code:
runEnviron.exec("/usr/bin/myOtherProgram" + newSource);
because Java will parse the string according to spaces and pass each chunk of the file path as a separate argument. You should add each argument as a separate string into an array of strings to be sure it'll work with paths with spaces, e.g.
Code:
String[] command = {"/usr/bin/myOtherProgram", newSource};
Process burnProcess = runEnviron.exec(command);
I hope that makes sense. Hit me back if it doesn't.

Also, Sun has some excellent documentation online:
http://java.sun.com/j2se/1.5.0/docs/api/

Dave

Last edited by ilikejam; 06-27-2005 at 03:47 PM.
 
Old 06-27-2005, 03:58 PM   #7
Diederick
LQ Newbie
 
Registered: Apr 2005
Posts: 25

Original Poster
Rep: Reputation: 15
Yes Yes Yes

Dave thanx very much i got the file loaded and every thing works.
Thanx 4 your help and time. Hope i could one day be as good as you in java.

Thanx Diederick (SA)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Opening programs in Mandrake 8.0 DYFLID Linux - Newbie 5 12-15-2004 05:18 PM
opening downloaded programs Bikerboy Linux - Software 1 07-09-2004 12:01 AM
Programs keep opening! antjessopuk Linux - Newbie 1 10-17-2003 06:52 AM
opening files in their programs hamish Linux - Newbie 2 09-07-2003 01:35 PM
Slow opening programs t1mc00per Linux - General 19 04-10-2003 06:59 PM


All times are GMT -5. The time now is 12:43 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration