how to excute a linux shell command in java
quoted from: http://bbs.chinaunix.net/viewthread.php?tid=841011&extra=page%3D4 /* test.java */ public class test
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
{
public static void main(String[] args)
{
String lsString = null;
Process process = null;
try
{
process = Runtime.getRuntime().exec(“ls”);
BufferedReader bufferedReader = new
BufferedReader(new InputStreamReader(process.getInputStream()));
while ((lsString = bufferedReader.readLine()) != null)
{
System.out.println(lsString);
}
try
{
process.waitFor();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
} // end test.java
August 3, 2008 at 11:16 am
thanks a lot, it is what I was looking for…
December 14, 2008 at 12:35 pm
Thanks, chears!
December 14, 2008 at 12:36 pm
cheers*
February 13, 2009 at 1:24 pm
Exactly what i needed !
Thanks a lot
May 25, 2009 at 4:19 pm
What if you are on Window and want to connect using Java to Linux server and then run some command on it?
March 11, 2010 at 1:36 am
http://www.informit.com/guides/content.aspx?g=java&seqNum=489
June 19, 2009 at 10:42 pm
if i want to adduser how to it?
since it needs root authentification
November 26, 2009 at 11:26 pm
hello i was trying to compile and execute files like .cpp and .java when i compiling was fine
but when i was trying to execute it i do nothing can someone help me with this
String command_compile=”javac print.java”;
String command_run=”java Main out.out”;
try {
final Process pro= Runtime.getRuntime().exec(command_compile);
final Process p=Runtime.getRuntime().exec(command_run);
} catch (Exception e) {
System.out.println(e.getClass());
}
November 26, 2009 at 11:29 pm
sorry the variable command_run is like this
String command_run=”java Main out.out”;
February 24, 2012 at 12:29 pm
atrakcje eventy łódź…
[...]how to excute a linux shell command in java « Home[...]…