How to run superuser commands on Linux via Java code?

There are several users and 1 superuser in my application. I am trying to write and save a file on Linux using Java code, but I am getting permission denied. I used the following code:

Process process = Runtime.getRuntime().exec("/usr/bin/tiff2pdf -o /tmp/tiff_dir/temp.pdf /tmp/tiff_dir/image.tiff");
int returnCode = process.waitFor();

I get the following error:

java.io.FileNotFoundException: image.tiff (Permission denied)

From my analysis, it seems that since the user does not have root privileges, I get this error. What is the solution to this?

+3
source share
3 answers

You should not run such a command as a superuser, as it poses a security risk (i.e. if someone has gained control of your java program, then they have keys to the kingdom). Instead, you should work with lower resolutions.

, image.tiff tiff2pdf. image.tiff.

+1

-, java.io.FileNotFoundException: image.tiff (Permission denied):

Process process = Runtime.getRuntime().exec("/usr/bin/tiff2pdf -o temp.pdf image.tiff");
int returnCode = process.waitFor();

- , , ( ). process.getErrorStream() (, ). , , FileNotFoundException, , Java .

EDIT, :

, returnCode 1. , root.

. , : returnCode.

-, exec exec String[] , , , , .

, , , . (* EDIT: * , , , rwx /tmp/tiff_dir.)

, sudo Runtime.exec(new String[] {"/usr/bin/sudo", ... the rest of your command ... }, . sudoers, , , (, !) , -.)

+1

:

File file = new File("/opt/image.tiff");
Runtime runtime = Runtime.getRuntime();
runtime.exec(new String[] { "/bin/chmod", "777",file.getPath()});

.

0

All Articles