aspose file tools*
The moose likes Beginning Java and the fly likes Running a program from a Java Application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Running a program from a Java Application" Watch "Running a program from a Java Application" New topic
Author

Running a program from a Java Application

Chanpreet Julka
Ranch Hand

Joined: Nov 09, 2001
Posts: 40
I just want to find out how to run a program from a Java Application. (like running the unix date command from a Java Application). Is there some sort of system command that does this, or how would it be done? Thank you.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Look at java.lang.Runtime.exec()


Rob
SCJP 1.4
Chanpreet Julka
Ranch Hand

Joined: Nov 09, 2001
Posts: 40
I wrote this small program to run the date command within the java application.
import java.lang.*;
import java.io.*;
class rundate {
public static void main(String[] args) throws IOException {
String dateCommand = "date";
rundate champ = new rundate();
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(dateCommand);
}
}
This compiles and runs. Now is this suposed to actually run the date command so i see it
on the screen? Because there is no output on the screen? If so please help me out, or tell me how I could. Thanks
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
For executables you can use something like:
Process p = Runtime.getRuntime().exec("notepad.exe OpenMe.txt");
To run DOS commands in a Windows OS you can use
Runtime.getRuntime().exec("cmd /K del File1.txt");
(the older version of windows use the "command" executable)
You can also do:
Runtime.getRuntime().exec("cmd /K start run.bat");
I am not a UNIX person - so I do not know what the equivalent for cmd.exe is.
 
jQuery in Action, 2nd edition
 
subject: Running a program from a Java Application