• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Running a program from a Java Application

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at java.lang.Runtime.exec()
 
Chanpreet Julka
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic