![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2004
Posts: 1
Rep Power: 0
![]() |
hi guys,
i want to execute linux commands like(ls -l , cat /etc/passwd etc) from within my java application . can somebody pls tell me what piece of code should i write to make it.. thanks in advance |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
...
Process p = Runtime.getRuntime().exec("ps -ef");
...
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#3 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
I had been wondering over the last couple of days whether Java had a command such as the system function. Pretty cool.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Nov 2004
Posts: 16
Rep Power: 0
![]() |
I have encountered this before, but ended up using a different solution to the problem I was working on...
but out of interest - is there a way to read in the output of the command into a variable? I think it's spawning a new thread or process, and so opportunity for communication might be limited, but does anyone know of a way? in the meantime I might check out my java book and google ![]() -edit oh actually the most sensible thing to do would be look up Process in the API docs ![]() -edit2 ahhh Process.getOutputStream() might be the baby I'm looking for! |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Nov 2004
Posts: 16
Rep Power: 0
![]() |
wow this is cool!!!!
I just wrote this to test it: import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ProcTest
{
public static void main( String[]a )
{
try
{
Process p = Runtime.getRuntime().exec("ps guax");
InputStreamReader converter = new InputStreamReader( p.getInputStream() );
BufferedReader input = new BufferedReader( converter );
for( String in = input.readLine(); in != null; in = input.readLine() )
{
System.out.println( in );
}
}
catch ( Exception e )
{
System.err.println( e );
}
}
}my question is - is there a better / more standard way of doing the continual read? I couldn't think of a way to do it (a java way - I could think of the perl way using a while loop), but the for loop I came up with seems to work quite well... but is there a more standard way than my for( String in = input.readLine(); in != null; in = input.readLine() ) and are there any other little bits I need to do? like flushing/clearing the buffer or anything? or is it fine the way it is? |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|