I’m changing file permission with this code:
Runtime.getRuntime().exec("chmod 600 '/home/user/Desktop/file'");
But it has no effect!
Before and after code execution ls -l /home/user/Desktop/file reports:
-rw-rw-r-- 1 evir evir 7928 Jul 31 14:54 file
What is the problem?
The question you asked is very specific to your system and environment.
So I’ll answer the broader question – and teach a man to fish – by pointing out that you should look at the output in order to determine what happened (or didn’t).
First up, you should assign the
Processinstance returned fromexecto a variable. Fire-and-forget is nice when it works, but if it doesn’t you’re left with absolutely no way to determine what’s happened.First stop is to call
process.waitFor()– which waits for the process to finish, and returns you its exit code. If this is non-zero it didn’t run correctly; and depending on the process this may even tell you what class of error was encountered.If this is not sufficient to solve your problem, you’ll want to see what was output to
stdoutandstderr. You can get handles to read these streams by callingprocess.getInputStream()andprocess.getErrorStream()respectively. Once you have these streams, just read bytes from them as normal.(Note that you really ought to read from these streams as a matter of course, whether you intend to process the data or not. Processes that write enough output to fill the buffer may otherwise block until the “other end” (you!) has read some of it down. With your
chmodexample though that’s unlikely to be the problem.)Now you have access to the exit status, stdout and stderr streams – in fact everything you’d get if you ran the process within a console. All that remains now is to use that information to solve your specific issue…