• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Running a program from a Java Application  RSS feed

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Look at java.lang.Runtime.exec()
 
Chanpreet Julka
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!