Solved

System Commands From JAVA

Posted on 1998-05-19
4
150 Views
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?

Thanx

Don
0
Comment
Question by:afsac
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
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.
0
 
LVL 2

Expert Comment

by:aziz061097
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.
0
 

Author Comment

by:afsac
ID: 1221196
Thank you for your quick response!
0
 
LVL 5

Expert Comment

by:fontaine
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

743 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

10 Experts available now in Live!

Get 1:1 Help Now