I try to start a new process using Java's ProcessBuilder
, running as root user (during the prototyping phase).
It works as expected when I run the application using a JDK installed with apt
.
My requirement is to allow to use custom JDKs downloaded on-demand. I thought that unpacking the JDK tar.gz file and marking the commands in the bin
folder as executable would be enough to make this "unpacked" JDK usable:
-rwxr-xr-x 1 root 12328 Aug 3 22:33 java
But for some reason when I start the same application with this "unpacked" JDK's java
command:
#!/usr/bin/env sh
export APP_HOME = "/opt/path/to/app"
export JAVA_HOME = "/opt/path/to/simply/unpacked/jdk"
$JAVA_HOME/bin/java -jar $APP_HOME/app.jar
I get the following error when trying to start a new process using ProcessBuilder
, no matter what I try to execute (even very simple commands fail, like whoami
):
Caused by: java.io.IOException: Cannot run program "/usr/bin/whoami" (in directory "/opt/..."): error=13, Permission denied
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1140)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1074)
at org.zeroturnaround.exec.ProcessExecutor.invokeStart(ProcessExecutor.java:997)
... 11 common frames omitted
Caused by: java.io.IOException: error=13, Permission denied
at java.base/java.lang.ProcessImpl.forkAndExec(Native Method)
at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:319)
at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:249)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1111)
... 13 common frames omitted
I have already tried two different JDKs (corretto and oracle) with the same result...
The Kotlin code is trivial, eg.:
try {
ProcessBuilder().command("/usr/bin/whoami").inheritIO().start().waitFor()
} catch (e: Exception) {
println("whoami failed")
}
I use Ubuntu Server 22.04.2 LTS.
The system is a simple default installation of Ubuntu Server, one partition, no network drives, etc.
/opt
is on the local disk as well:
$df -Th /opt/myapp/runtime
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-ubuntu--lv ext4 98G 23G 71G 25% /
The output of ls -l /usr/bin/whoami
:
$ ls -l /usr/bin/whoami
-rwxr-xr-x 1 root root 31232 Feb 7 2022 /usr/bin/whoami
Ps.: I'm a Kotlin/Java developer and a simple Linux user, Ubuntu is my preferred development OS - but I'm not a Linux power-user :)