通过java 调用linux 下命令 如何实现 通过java 调用linux 下命令 如何实现,例如 通过java 调用 iptables -nL -v , 同时希望将页面的结果能够显示到web 页面当中。谢了各位,急了一周了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 类似这样:Runtime.getRuntime().exec( "cmd /c start C:\\bea\\user_projects\\domains\\HBEP\\autoStart.cmd");你也可以把要处理的命令写成个批处理文件,用上面的语句调用 我需要在web 页面上显示 执行命令的内容 一个完整的例程,把system.out转向就可以输出到网页上import java.util.*;import java.io.*;class StreamGobbler extends Thread{ InputStream is; String type; StreamGobbler(InputStream is, String type) { this.is = is; this.type = type; } public void run() { try { InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line=null; while ( (line = br.readLine()) != null) System.out.println(type + ">" + line); } catch (IOException ioe) { ioe.printStackTrace(); } }}public class GoodWindowsExec{ public static void main(String args[]) { if (args.length < 1) { System.out.println("USAGE: java GoodWindowsExec <cmd>"); System.exit(1); } try { String osName = System.getProperty("os.name" ); String[] cmd = new String[3];System.out.println( osName); if( osName.equals( "Windows 2000" ) ) { cmd[0] = "cmd.exe" ; cmd[1] = "/C" ; cmd[2] = args[0]; } else if( osName.equals( "Windows 95" ) ) { cmd[0] = "command.com" ; cmd[1] = "/C" ; cmd[2] = args[0]; } Runtime rt = Runtime.getRuntime(); System.out.println("Execing " + cmd[0] + " " + cmd[1] + " " + cmd[2]); Process proc = rt.exec(cmd); // any error message? StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR"); // any output? StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT"); // kick them off errorGobbler.start(); outputGobbler.start(); // any error??? int exitVal = proc.waitFor(); System.out.println("ExitValue: " + exitVal); } catch (Throwable t) { t.printStackTrace(); } }} 类似这样:Runtime.getRuntime().exec( "cmd /c start C:\\bea\\user_projects\\domains\\HBEP\\autoStart.cmd");注意这样的方法将要使你放弃跨平台的特性。 java 类中直接写方法 在服务器上如何将客户端上传过来的io流封装成文件? 如何使“主线程”等待“子线程”执行结束后再继续执行? Classpath问题 java模拟电信月末计费的小程序(那位高手帮忙看看错在那里啊?) 我这段代码应该怎么优化呢?如下所示用if判断的话,要遍历所有if判断 一个有关toString()的问题! 如何判断当数据库无记录时,ResultSet的状态值 做了小站,还没做完,发布之前,希望大家给点意见。 请问以下程序为啥执行不了,报terminated 帮我看看二分法查找的问题 JVM 环境变量加载问题
Runtime.getRuntime().exec( "cmd /c start C:\\bea\\user_projects\\domains\\HBEP\\autoStart.cmd");
你也可以把要处理的命令写成个批处理文件,用上面的语句调用
import java.util.*;
import java.io.*;class StreamGobbler extends Thread
{
InputStream is;
String type;
StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}public class GoodWindowsExec
{
public static void main(String args[])
{
if (args.length < 1)
{
System.out.println("USAGE: java GoodWindowsExec <cmd>");
System.exit(1);
}
try
{
String osName = System.getProperty("os.name" );
String[] cmd = new String[3];
System.out.println( osName);
if( osName.equals( "Windows 2000" ) )
{
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = args[0];
}
else if( osName.equals( "Windows 95" ) )
{
cmd[0] = "command.com" ;
cmd[1] = "/C" ;
cmd[2] = args[0];
}
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0] + " " + cmd[1]
+ " " + cmd[2]);
Process proc = rt.exec(cmd);
// any error message?
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error???
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}
Runtime.getRuntime().exec( "cmd /c start C:\\bea\\user_projects\\domains\\HBEP\\autoStart.cmd");注意这样的方法将要使你放弃跨平台的特性。