12

I have an executable file (ffmpeg) that I'm trying to run with a Java program on a Mac. I used the Java program to send the command chmod 777 /path/to/ffmpeg, but when I try to run ffmpeg, I get the following error:

java.io.IOException: Cannot run program "/Users/james/WalkTheHall/ffmpeg": error=13, Permission denied

But when I run chmod 777 /path/to/ffmpeg from Terminal on my own before opening the Java application, the command to ffmpeg will run just fine in the Java program.

Is there a difference between calling chmod from within the Java program and calling it on my own? Why will it not work? Thank you!

Raedwald
  • 46,613
  • 43
  • 151
  • 237
James Skidmore
  • 49,340
  • 32
  • 108
  • 136
  • maybe you are running the java executable with a user who hasn't got the privileges to chmod that file – Silvio Donnini Jun 21 '10 at 15:18
  • Details... I don't know on Mac, but on GNU/Linux as common user you could not do `chmod 777` (some permissions are "masked"), so is it possible when you try from terminal you are indeed the kind of user that can do such a thing, while you run the java as an user that can't? (it is also possible that the java vm drops privilegies when running as "power" users, just to enhance security) – ShinTakezou Jun 21 '10 at 15:24

6 Answers6

7

I just had the same problem in my code. i solved this by add waitFor after exec. The "chmod" process is not finished when next command is executed. the code may look like:

p = Runtime.getRuntime.exec("chmod 777 xxx");
p.waitFor();
Runtime.getRuntime.exec("./xxx");
macrokigol
  • 71
  • 1
  • 2
4

Yes, there is a difference. When you run the command from the terminal, it is you who is performing the action, and thus it is performed using your credentials. The Java application is running the command using the Java application's permissions. This is to prevent an application from running and then making dangerous, unwanted changes to the file system. Perhaps someone else can elaborate and give guidance to a workaround for this.

Ryan Hayes
  • 5,290
  • 4
  • 42
  • 52
  • Thanks Ryan! Surely there is a way to run the program within Java. Any ideas on what to do? – James Skidmore Jun 21 '10 at 15:35
  • Not sure. It seems like this shouldn't be allowed by design, like ShinTakezou's comment on your question says, for security reasons. Have you tried CarlG's answer? If it works on Linux, then it would definitely be worth a try on Mac OS. – Ryan Hayes Jun 21 '10 at 15:41
4

I'd guess that chmod is a shell command, not an executable. Try running chmod through your shell. See more details here: Want to invoke a linux shell command from Java

Community
  • 1
  • 1
CarlG
  • 1,656
  • 1
  • 17
  • 21
  • Thank you so much Carl, it worked perfectly to run it with bash! – James Skidmore Jun 24 '10 at 04:16
  • `which chmod` says `/bin/chmod` for me so under bash-3.2.48 it's an executable and not a shell builtin. Perhaps whatever launches Java does not have it on the executable search path, but invoking the shell runs the users shell config scripts repairing PATH. – Mike Samuel Nov 15 '11 at 16:16
2

Try this:

File commandFile = new File("myFile.txt");
commandFile.setExecutable(true);
Process p = Runtime.getRuntime.exec(commandFile.getAbsoluteFile());
Jefferson Lima
  • 5,186
  • 2
  • 28
  • 28
2

I am currently working on a project that also makes use of FFMpeg on OSX. I store FFMpeg in the JAR and extract it and set executable on use as you seem to be doing. This is what I do, and it seems to work.

public static void setExecutable(File file, boolean executable)
{
    Process p = Runtime.getRuntime().exec(new String[] {
        "chmod",
        "u"+(executable?'+':'-')+"x",
        file.getAbsolutePath(),
    });
    // do stuff to make sure p finishes & capture output
}

The code is GPL, so feel free to check it out. Its not the nicest codebase, and even the FFMpeg stuff is perhaps overly complex, but it works.

Source is viewable at http://korsakow.net

These two files in particular might be interesting for you

FFMpegEncoderOSX.java

FileUtil.java

putgeminmouth
  • 583
  • 4
  • 7
  • Thanks for the answer, I appreciate it! I'm running that command, but it's still giving a permission denied error. Any ideas why? – James Skidmore Jun 23 '10 at 18:51
0

to start an program on OSX you need this:

Runtime.getRuntime().exec("chmod 777 "+path);   //in order to execute it
Runtime.getRuntime().exec(path);                //execute it
Runtime.getRuntime().exec("chmod 744 "+path);   //undo every change

path should be the path to the exc of the program, for example:

AppStore -> Applications/App\ Store.app/Contents/MacOS/App\ Store