小编dub*_*ubi的帖子

使用java Runtime(在windows机器上)将linux命令发送到linux服务器

下面是我正在运行的代码片段.我是否使用数组pf字符串(cmd)或它是单个字符串我得到一个例外(见下文),同时有一个无密码登录到目标Linux系统.

private static int bringHostFile() {
    try {            
        String[] cmd ={"ssh" , "root@im6-64s" , "/root/bring_hosts"};
   Process p = Runtime.getRuntime().exec(cmd);
        BufferedReader stdInput = new BufferedReader(new 
             InputStreamReader(p.getInputStream()));

        BufferedReader stdError = new BufferedReader(new 
             InputStreamReader(p.getErrorStream()));

        String s = null;
        // read the output from the command
        if ((s = stdInput.readLine()) != null) {
            System.out.println(s);     
         }

        // read any errors from the attempted command
        while ((s = stdError.readLine()) != null) {
               System.out.println(s);
        }            

    }
    catch (IOException e) {
        e.printStackTrace();
        System.exit(-1);
    }   

    return 0;  
}
Run Code Online (Sandbox Code Playgroud)

例外: …

java runtime runtime.exec

0
推荐指数
1
解决办法
1768
查看次数

标签 统计

java ×1

runtime ×1

runtime.exec ×1