Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to execute Linux shell commands from Java2 app?

Status
Not open for further replies.

Joncamp

MIS
Oct 30, 2006
15
US
In other languages like PHP/Perl you can execute any shell command, what package and method do you use in Java2 to do a shell command execution?

Thanks,
Jon
 
I think you mean Runtime().exec() :

Code:
String[] commands = new String[1];
commands[0] = "ls";

try {
	Process p = Runtime.getRuntime().exec();
	
	BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
	String line;
	while ((line = br.readLine()) != null) {

		System.out.println(line);

	}
	br.close();
	
	p.waitFor();


} catch (InterruptedException ie) {
	throw new IOException("Error waiting for command " commands[0] +" :" +ie.getMessage());
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
By the way, this forum is meant for J2EE questions only - the J2SE formum is here : forum269

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top