【发布时间】:2013-08-19 23:07:53
【问题描述】:
我尝试了一个在运行时执行 Linux 命令的简单程序。但是下面的程序编译并运行没有任何错误,但是文本文件没有按预期创建。这个程序有什么问题吗?
import java.io.*;
class ExecuteJava
{
public static void main(String args[])
{
String historycmd = "cat ~/.bash_history >> Documents/history.txt";
try
{
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(historycmd);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
【问题讨论】:
-
此代码正在尝试写入程序运行所在的当前目录中名为
Documents的目录。该目录是否存在? -
@andy256 是的,确实如此。
标签: java runtime.exec