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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

911 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now