Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 5th, 2004, 8:05 AM   #1
lokesh_air
Newbie
 
Join Date: Oct 2004
Posts: 1
Rep Power: 0 lokesh_air is on a distinguished road
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
lokesh_air is offline   Reply With Quote
Old Oct 5th, 2004, 8:43 AM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
...
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."
Infinite Recursion is offline   Reply With Quote
Old Oct 5th, 2004, 4:10 PM   #3
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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
Mjordan2nd is offline   Reply With Quote
Old Nov 23rd, 2004, 5:18 PM   #4
eccles
Newbie
 
Join Date: Nov 2004
Posts: 16
Rep Power: 0 eccles is on a distinguished road
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!
eccles is offline   Reply With Quote
Old Nov 23rd, 2004, 5:50 PM   #5
eccles
Newbie
 
Join Date: Nov 2004
Posts: 16
Rep Power: 0 eccles is on a distinguished road
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?
eccles is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:51 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC