现在在某机器部署的java程序要在另外一台windows服务器上执行一条cmd命令,请问该如何实现啊?~~~呼唤大侠~

解决方案 »

  1.   

    socket编程啊,不太熟,呵呵,帮楼主顶了。
      

  2.   

    利用socket通信,把写好的批处理传到服务器上,然后执行,但是好像有难度,只是个想法
      

  3.   

    我这里有个JAVA代码用DOS命令获取MAC地址的,给你看看!import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;public class DOSCommand {
        public DOSCommand() {
        }    public static String getMACAddress() {        String address = "";
            String os = System.getProperty("os.name");
            if (os != null && os.startsWith("Windows")) {
                try {
                    String command = "cmd.exe /c ipconfig /all";
                    Process p = Runtime.getRuntime().exec(command);
                    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                    String line;
                    while ((line = br.readLine()) != null) {
                        if (line.indexOf("Physical Address") > 0) {
                            int index = line.indexOf(":");
                            index += 2;
                            address = line.substring(index);
                            break;
                        }
                    }
                    br.close();
                    return address.trim();
                } catch (IOException e) {
                 e.printStackTrace();
                }
            }
            return address;
        } public static void main(String[] args) {
        System.out.println(""+DOSCommand.getMACAddress());
    }
    }
      

  4.   

    我的JDK版本:
    java version "1.6.0_04"
    Java(TM) SE Runtime Environment (build 1.6.0_04-b12)
    Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)
      

  5.   

    楼主看看 Telnet 协议,http://www.cnpaf.net/class/telnet/ 然后使用 Java 自己实现或使用第三方类库。
      

  6.   

    期待
    SOCKAT编程
    不会啊 !
      

  7.   

    socket serversocket,两台电脑都要装java环境
      

  8.   

                       File FileName=new File("D:\\sql2000\\SETUP.BAT");
       String str="net stop ServerMgr\n net start ServerMgr>E:\\1.txt";
       WriteFile(str);
       try {
             String command = "cmd.exe /c"+"start D:\\sql2000\\SETUP.BAT";
             Process child = Runtime.getRuntime().exec(command); 
      } catch (IOException e)
      {
         System.out.println("文件错误");
      }
      

  9.   

    肯定需要在要执行程序的机器上搞个服务器程序,等待用户远程输入命令,然后再执行,
    不过这不就是unix操作系统的原理么?
      

  10.   

    String command = "cmd.exe /c ipconfig /all";//这里的/c 是什么意思                Process p = Runtime.getRuntime().exec(command);
      

  11.   

    可以用socket把执行的Dos命令传到另一台计算机,然后接受到之后执行命令
      

  12.   

    thinking in java 第四版551页//: net/mindview/util/OSExecute.java
    // Run an operating system command
    // and send the output to the console.
    package net.mindview.util;
    import java.io.*;public class OSExecute {
      public static void command(String command) {
        boolean err = false;
        try {
          Process process =
            new ProcessBuilder(command.split(" ")).start();
          BufferedReader results = new BufferedReader(
            new InputStreamReader(process.getInputStream()));
          String s;
          while((s = results.readLine())!= null)
            System.out.println(s);
          BufferedReader errors = new BufferedReader(
            new InputStreamReader(process.getErrorStream()));
          // Report errors and return nonzero value
          // to calling process if there are problems:
          while((s = errors.readLine())!= null) {
            System.err.println(s);
            err = true;
          }
        } catch(Exception e) {
          // Compensate for Windows 2000, which throws an
          // exception for the default command line:
          if(!command.startsWith("CMD /C"))
            command("CMD /C " + command);
          else
            throw new RuntimeException(e);
        }
        if(err)
          throw new OSExecuteException("Errors executing " +
            command);
      }
    } ///:~
    //: net/mindview/util/OSExecuteException.java
    package net.mindview.util;public class OSExecuteException extends RuntimeException {
      public OSExecuteException(String why) { super(why); }
    } ///:~