0

I want to run shell script inside watch service class to run the shell after new file add to folder. Watch service is working perfectly but when I want to add Runtime.getRuntime().exec("home/user/test.sh"); I recived error. I just add Runtime after this:

   // 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")

What I have to do?

Aira
  • 5
  • 4
  • what version of java are you using? also that is really really wrong what you did, any reason why can't call the shell script from within code? – jgr208 Jun 08 '16 at 16:38
  • I am using java version 1.7. I know it is wrong. literally I don't know what should I do. But the requirement is, I have to watch one folder, when new file add, run specific script in command line – Aira Jun 08 '16 at 16:41
  • i am pretty sure java has a folder watcher API you are not using. And again is there any reason you cant put this script on the system and call from within the code? – jgr208 Jun 08 '16 at 16:43
  • I am using this tutorial for watching the folder http://andreinc.net/2013/12/06/java-7-nio-2-tutorial-writing-a-simple-filefolder-monitor-using-the-watch-service-api/ – Aira Jun 08 '16 at 16:43
  • https://docs.oracle.com/javase/tutorial/essential/io/examples/WatchDir.java this might be a better example – jgr208 Jun 08 '16 at 16:45
  • The only reason is I want to do everything automatically. I mean when one folder ( in my case movie) add to path, i want to run ffmpeg script to convert it from mp4 to avi. for this I have to write ffmpeg script in shell and then run it from inside the code – Aira Jun 08 '16 at 16:45
  • huh? well i don't think that is possible by what you are doing. you are basically doing command line commands and not a shell script when java does that stuff unless you call a shell script – jgr208 Jun 08 '16 at 16:47
  • Shouldn't you use `.exec(new String[] {"/path/to/bash", "-someArgs", "path/to/script" })` ? – csharpfolk Jun 08 '16 at 16:50
  • so there is no way to run command line from inside the java code? – Aira Jun 08 '16 at 16:51
  • @csharpfolk oh duh i got confused with what they was saying i thought they programmed the script inside the java. but they should be using processbuilder not runtime.exec – jgr208 Jun 08 '16 at 16:52
  • actually I am girl;) by the way no I didnt write command line inside the code. I want run command line from the code with using bash file – Aira Jun 08 '16 at 16:53
  • @csharpfolk i have to write this after creating new folder? – Aira Jun 08 '16 at 16:54
  • @Aira ok well in that case use processbuilder which has practically replaced runtime.exec – jgr208 Jun 08 '16 at 16:54

1 Answers1

0

I thing problems with running script have nothing to do with WatchService, since you don't post actual exception that is throws (that would help a lot) I can only guess what is wrong, so please check this:

  1. Script doesn't have permission to execute (easily fixable by chmod +x path/to/script.sh) - in that case you would get IOException with message like Permission denied or similar

  2. System cannot find your script, since you are using relative path (no / at the beginning of script name) in that case ether use full script name e.g. /home/user/foo/script.sh or use proper relative path ../foo/script.sh - you should check if script exists before running it via exec (How do I check if a file exists in Java?)

  3. Beware that script may be called with working directory of running Java program - so you should pass newly created file path as parameter to script to make it independent of its location

I followed tutorial that you were using with code:

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(new String[] { "/home/xxx/foo.sh", newPath.toString() });
                    }

and script:

#!/bin/bash
echo "FILE CREATED: $1" >> /home/xxx/watch_dir.log

and it worked without any errors.

Community
  • 1
  • 1
csharpfolk
  • 4,124
  • 25
  • 31