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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 77
netstat -ano | find "8000" and taskkill /f /pid 2984 3 56
use lov values 2 50
Running JavaFX on JDeveloper 12C 1 32
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

773 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