通过java 调用linux 下命令 如何实现,例如 通过java 调用 iptables -nL -v , 同时希望将页面的结果能够显示到web 页面当中。谢了各位,急了一周了。

解决方案 »

  1.   

    类似这样:
    Runtime.getRuntime().exec( "cmd /c start C:\\bea\\user_projects\\domains\\HBEP\\autoStart.cmd");
    你也可以把要处理的命令写成个批处理文件,用上面的语句调用
      

  2.   

    我需要在web 页面上显示 执行命令的内容
      

  3.   

    一个完整的例程,把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();
              }
        }
    }
      

  4.   

    类似这样:
    Runtime.getRuntime().exec( "cmd /c start C:\\bea\\user_projects\\domains\\HBEP\\autoStart.cmd");注意这样的方法将要使你放弃跨平台的特性。