大虾请进,如何用java调用linux操作系统下的命令行?

freelarry 2005-10-08 04:53:19
比如说我想在java中实现 打一个tar包

请大虾指教
...全文
461 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
OnlyFor_love 2005-10-09
  • 打赏
  • 举报
回复
import java.io.BufferedInputStream;
import java.io.IOException;

public class ExecLs {

static public void main(String[] args) {
String cmd = "ls"

try {
Process ps = Runtime.getRuntime().exec(cmds);
System.out.print(loadStream(ps.getInputStream()));
System.err.print(loadStream(ps.getErrorStream()));
} catch(IOException ioe) {
ioe.printStackTrace();
}
}

// read an input-stream into a String
static String loadStream(InputStream in) throws IOException {
int ptr = 0;
in = new BufferedInputStream(in);
StringBuffer buffer = new StringBuffer();
while( (ptr = in.read()) != -1 ) {
buffer.append((char)ptr);
}
return buffer.toString();
}

}
freelarry 2005-10-08
  • 打赏
  • 举报
回复
谢谢楼上的,但在linux下呢?
doway 2005-10-08
  • 打赏
  • 举报
回复
Runtime.getRuntime().exec("dir -l");

23,404

社区成员

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

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