Solved

System Commands From JAVA

Posted on 1998-05-19
4
157 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
[X]
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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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:
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…

719 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