java如何实现向linux控制台输入命令啊 应该先实现SSH或TELNET的客户端功能吧 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 就像是 windows下的 Runtime.getRuntime().exec在linux下Runtime.getRuntime().exec()可以吗 ---“在linux下Runtime.getRuntime().exec()可以吗”是的。至少我在linux上这么干可以。但是如果你执行的命令需要从控制台读取数据的话,就不能光靠这个方法了。还要加点别的。 难岛没人会吗先实现最简单的如dir命令都行啊 如果你的linux命令比较复杂的话,建议把命令都写到一个shell里去,然后用java调用,例如:Process prog=null;prog=Runtime.getRuntime().exec("放所执行的shell文件及参数");BufferedReader stderr = new BufferedReader(new InputStreamReader(prog.getErrorStream()));String LineErr = stderr.readLine();while (LineErr !=null){ System.out.println("LineErrError>>>"+LineErr); LineErr = stderr.readLine();}int progEndWait = prog.waitFor();stderr.close();如果只是简单的linux 命令的话,Runtime.getRuntime().exec("ls -l")啥的应该就可以直接用了 是的,有些命令如cd 命令还是应该写入到一个shell文件里才能正常执行。但假如你要运行echo <password> | <another command> 这种命令时,连shell这种方式也不能起作用,你可以试验一下,应该是机器停在那里没动静了。这就要麻烦些。可以这么写:.... Runtime rt = Runtime.getRuntime(); password = passwordField.getText(); try { Process passwordpr = rt.exec("echo" + password); //在这里passwordpr和 in = passwordpr.getInputStream(); //secondpr 各自对应了管道 //符左右的命令 Process secondpr = rt.exec("<another command>"); OutputStream out = secondpr.getOutputStream(); int a; while((a = in.read()) != -1) { out.write(a); } passwordpr.waitFor(); in.close(); out.close(); in = secondpr.getInputStream(); String consoleOut = null; BufferedReader msgBr = new BufferedReader(new InputStreamReader(in)); while((consoleOut = msgBr.readLine()) != null) { System.out.println("Message is: " + consoleOut); } secondpr.waitFor(); in.close(); msgBr.close(); } catch (Exception e1) .... 大概是这样,你可以试试。 新手,请帮帮忙 将中文时间转换为日文中的和历时间 JComboBox怎么用啊,怎么选择了一个Item后没有反应啊。 取出arraylist第一条记录和最后一条记录 如何获取ftp服务器上要某个下载文件的大小,类库用的common.net.ftp 讨论一下关于java的传值还是引用 String string = null, String string = "";有什么区别吗? 请问在linux下运行 Java程序,要安装哪些软件,怎样设置环境变量 如何得到UTC时间? 子类能不能继承父类里protected的内部类 jdk1.5.0自带mm.mysql的驱动吗? Unicode
在linux下Runtime.getRuntime().exec()可以吗
---“在linux下Runtime.getRuntime().exec()可以吗”
是的。至少我在linux上这么干可以。
但是如果你执行的命令需要从控制台读取数据的话,就不能光靠这个方法了。还要加点别的。
先实现最简单的如dir命令都行啊
Process prog=null;
prog=Runtime.getRuntime().exec("放所执行的shell文件及参数");
BufferedReader stderr = new BufferedReader(new InputStreamReader(prog.getErrorStream()));
String LineErr = stderr.readLine();
while (LineErr !=null)
{
System.out.println("LineErrError>>>"+LineErr);
LineErr = stderr.readLine();
}
int progEndWait = prog.waitFor();
stderr.close();
如果只是简单的linux 命令的话,Runtime.getRuntime().exec("ls -l")啥的应该就可以直接用了
....
Runtime rt = Runtime.getRuntime();
password = passwordField.getText();
try {
Process passwordpr = rt.exec("echo" + password); //在这里passwordpr和
in = passwordpr.getInputStream(); //secondpr 各自对应了管道
//符左右的命令
Process secondpr = rt.exec("<another command>");
OutputStream out = secondpr.getOutputStream(); int a;
while((a = in.read()) != -1) {
out.write(a);
}
passwordpr.waitFor();
in.close();
out.close(); in = secondpr.getInputStream();
String consoleOut = null;
BufferedReader msgBr = new BufferedReader(new InputStreamReader(in)); while((consoleOut = msgBr.readLine()) != null) {
System.out.println("Message is: " + consoleOut);
}
secondpr.waitFor();
in.close();
msgBr.close();
} catch (Exception e1) .... 大概是这样,你可以试试。