File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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

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

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

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.*;
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"

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.
I agree. Here's the link:
subject: Running a program from a Java Application
Similar Threads
Read input from command line
Ensuring a Single Program Instance on Desktop
Running external application with stdin as argument
How to get DNS name and IP Address with Java code
standalone application question