怎么用java的方式,执行局域网内某台机器上的.sh 或.exe呢?

tmily 2015-02-26 03:02:09
怎么用java的方式,执行局域网内某台机器上的.sh 或.exe呢?并且获得返回值?
除了:
Runtime.getRuntime().exec
这种方式,还有什么其他的访问方式吗?
...全文
185 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tmily 2015-02-28
  • 打赏
  • 举报
回复
这个可以用。就是想了解下,看有没有其他的。
Honwhy 2015-02-28
  • 打赏
  • 举报
回复
可以试试Apache Commons exec library
package testShellScript;

import java.io.IOException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;

public class TestScript {
    int iExitValue;
    String sCommandString;

    public void runScript(String command){
        sCommandString = command;
        CommandLine oCmdLine = CommandLine.parse(sCommandString);
        DefaultExecutor oDefaultExecutor = new DefaultExecutor();
        oDefaultExecutor.setExitValue(0);
        try {
            iExitValue = oDefaultExecutor.execute(oCmdLine);
        } catch (ExecuteException e) {
            // TODO Auto-generated catch block
            System.err.println("Execution failed.");
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.err.println("permission denied.");
            e.printStackTrace();
        }
    }

    public static void main(String args[]){
        TestScript testScript = new TestScript();
        testScript.runScript("sh /root/Desktop/testScript.sh");
    }
}
http://stackoverflow.com/questions/525212/how-to-run-unix-shell-script-from-java-code
wula0010 2015-02-27
  • 打赏
  • 举报
回复
Runtime.getRuntime().exec这个方法不好么?
tmily 2015-02-26
  • 打赏
  • 举报
回复
能不能给我说的再详细些,你说的这些,我都没有接触过。谢谢。
普凡 2015-02-26
  • 打赏
  • 举报
回复
1.开源的ssh jar。 2.或者自己写agent利用socket通信

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧