我想在watch服务类中运行shell脚本,在新文件添加到文件夹后运行shell。 观看服务工作正常,但是当我想要添加Runtime.getRuntime().exec("home/user/test.sh")
;我收到错误。 我只是在此之后添加运行时间:在java中运行shell观看服务
// Dequeueing events
Kind<?> kind = null;
for(WatchEvent<?> watchEvent : key.pollEvents()) {
// Get the type of the event
kind = watchEvent.kind();
if (OVERFLOW == kind) {
continue; //loop
} else if (ENTRY_CREATE == kind) {
// A new Path was created
Path newPath = ((WatchEvent<Path>) watchEvent).context();
// Output
System.out.println("New path created: " + newPath);
Runtime.getRuntime().exec("home/user/test.sh")
我该怎么办?
您使用的是哪个版本的java?这也确实是错误的,为什么不能从代码中调用shell脚本? – jgr208
我正在使用java版本1.7。我知道这是错误的。从字面上我不知道该怎么办。但要求是,我必须观察一个文件夹,当新文件添加,在命令行运行特定的脚本 – Aira
我很确定java有一个文件夹监视器API,你没有使用。还有没有什么理由你不能把这个脚本放在系统上,并在代码中调用? – jgr208