JAVA 调用shell脚本
2010-11-23 16:09
537 查看
1:Reading Output from a Command.
try {
// Execute a command without arguments
String command = "ls";
Process child = Runtime.getRuntime().exec(command);
// Execute a command with an argument
command = "ls /tmp";
child = Runtime.getRuntime().exec(command);
} catch (IOException e) {
}
If an argument contain spaces, it is necessary to use the overload that requires the command and its arguments to be supplied in an array:
try {
// Execute a command without arguments
String command = "ls";
Process child = Runtime.getRuntime().exec(command);
// Execute a command with an argument
command = "ls /tmp";
child = Runtime.getRuntime().exec(command);
} catch (IOException e) {
}
If an argument contain spaces, it is necessary to use the overload that requires the command and its arguments to be supplied in an array:
try { // Execute a command with an argument that contains a space String[] commands = new String[]{"grep", "hello world", "/tmp/f.txt"}; commands = new String[]{"grep", "hello world", "c://Documents and Settings//f.txt"}; Process child = Runtime.getRuntime().exec(commands); } catch (IOException e) { } 2:Reading Output from a Command
try { // Execute command String command = "ls"; Process child = Runtime.getRuntime().exec(command); // Get the input stream and read from it InputStream in = child.getInputStream(); int c; while ((c = in.read()) != -1) { process((char)c); } in.close(); } catch (IOException e) { } 3:Sending Input to a Command
try { // Execute command String command = "cat"; Process child = Runtime.getRuntime().exec(command); // Get output stream to write from it OutputStream out = child.getOutputStream(); out.write("some text".getBytes()); out.close(); } catch (IOException e) { }
相关文章推荐
- Window下Java远程调用Shell脚本的实现(学习整理)
- java调用linux中的shell脚本
- 使用Java调用shell脚本时遇到的问题
- java调用shell脚本,并获得结果集的例子
- 使用bat命令或shell脚本调用java程序
- linux下JAVA调用shell脚本
- java jsp调用shell(带参数)脚本并返回值
- java调用shell脚本并传参
- 如何在java程序中调用linux命令或者shell脚本
- Java之怎么通过java去调用并执行shell脚本以及问题总结(转)
- java调用shell脚本,并获得结果集的例子
- Shell脚本调用java程序 Example
- Java调用Shell脚本
- java调用shell脚本,解决传参和权限问题
- Java调用shell脚本阻塞
- Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件
- Java 调用 shell 脚本详解
- java远程调用shell脚本
- Hadoop概念学习系列之Java调用Shell命令和脚本,致力于hadoop/spark集群(三十六)
- java调用Shell脚本