我尝试了一个简单的程序在运行时执行Linux命令。但是下面的程序会被编译并且运行时没有任何错误,但是文本文件并没有像预期的那样被创建。这个程序有什么问题吗?Java - 运行时命令执行
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);
}
}
}
此代码正尝试写入progam正在运行的当前目录中名为'Documents'的目录。该目录是否存在? – andy256
@ andy256是的,它的确如此。 –