怎么用java的方式,执行局域网内某台机器上的.sh 或.exe呢? 本帖最后由 tmily 于 2015-02-26 15:02:57 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1.开源的ssh jar。2.或者自己写agent利用socket通信 Runtime.getRuntime().exec这个方法不好么? 可以试试Apache Commons exec librarypackage 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 = 与 +=底层实现的区别? 直接点link 创建弹开的新页面 VK_ENTER cannot be resolved 用ArrayList如何拆分字符串并统计出其中每个单词的数目 在线等!! 用Java程序读取条码的问题。 求教输出格式的问题 大家快来加入这个JAVA群~~一起学习一起进步~!(1416551) 筮待解决的问题! 可以用Java读取Notes的数据么?急,定给分!!! 一道题,没搞懂 jdbc 递归无限极tree final static与static 修饰的常量,用类名.常量名调用时候静态代码块加载问题
2.或者自己写agent利用socket通信
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