System Commands From JAVA

Posted on 1998-05-19
Last Modified: 2010-04-16
I would like to know how to perform system commands and use the results with JAVA.
For example in Perl, I can use

system("showrev -p >/tmp/patches.dat");

Then open (via perl) /tmp/patches.dat and use the results in my code.
Can the same thing be done with Java?


Question by:afsac
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

fontaine earned 100 total points
ID: 1221194
You can use the "exec()" methods of the Runtime class. They allow to spawn an external process. Example:

Runtime runtime = Runtime.getRuntime();

try {
      runtime.exec("showrev -p >/tmp/patches.dat");
} catch (IOException e) {
// error message here...

You can then, of cource, open the /temp/patches.dat file from your Java app., as usual.

Expert Comment

ID: 1221195
Yes you can do it using the runtime.exec(String ) method . This executes the string as a commandline, in the environment. I shall try to provide a concrete example soon.

Author Comment

ID: 1221196
Thank you for your quick response!

Expert Comment

ID: 1221197
Thanks, afsac! Just to be complete: the exec() methods all return an instance of a java.lang.Process object that give you more control on the sub-process. Example:

Runtime runtime = Runtime.getRuntime();

try {
       Process process =  runtime.exec("showrev -p >/tmp/patches.dat");
       process.waitFor(); // waits for the subprocess to complete.
 } catch (IOException e) {
   // error message here...
} catch (InterruptedException ie) {
// error message here...

Take a look at the doc. for more info.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 64
swing controls 2 21
Tagging and Merging on Branch 1 39
glassfish admin console 1 19
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question