我想在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