1

I'm writing a program for work and a certain part of the code uses Process and ProcessBuilder to run some system commands.

I'm developing it in a Linux Mint machine but the program will run in Ubuntu 20.04 GNOME.

When I build the maven project as javafx:jlink (because I'm going to build it as a .deb package) and run the bin/app script that gets created. I get this error in the console.

java.io.IOException: Cannot run program "uptime": error=13, Permission denied

I've already searched other threads and they all say the same thing that I have to give the user running the program permissions to that executable with chmod u+x /usr/bin/uptime. It doesn't work.

Originally this part of the code was using Process, switched to ProcessBuilder and still didn't do anything. I even went as far as to make the user owner of /usr/bin/uptime and I still get that error.

This is what I'm currently working with:

// Set Uptime
try{
    Process p = new ProcessBuilder().command("uptime","-p").start();
    InputStream in = p.getInputStream();
    uptime = new String(in.readAllBytes());
    p.destroy();
}catch (IOException err){
    System.out.println(err.getMessage());
    err.printStackTrace();
}

The only difference that I can think of is that in my Linux Mint machine, my user is Administrator where as the test machine, the user is a regular user.

I disabled AppArmor thinking it was causing issues, checked journalctl -xe to see if anything odd came up and even checked /var/log/syslog but no errors either.

If I open a regular terminal as this user, I can run these commands perfectly fine. It's getting a bit hard to pinpoint where this is giving me trouble. Any thoughts?

jewelsea
  • 150,031
  • 14
  • 366
  • 406
xR34P3Rx
  • 395
  • 9
  • 28

1 Answers1

2

As shown here, specify redirectErrorStream(true) to see more about the error. For example, on my platform,

Process p = new ProcessBuilder()
    .command("/usr/bin/uptime", "-p")
    .redirectErrorStream(true)
    .start();
InputStream in = p.getInputStream();
System.out.println(new String(in.readAllBytes()));

Tells me this:

uptime: illegal option -- p
usage: uptime

Removing the unsupported option, produces the expected output. As an aside the full path can be determined by running which uptime.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I added this to 2 different `ProcessBuilder`s and none of them have given any other errors aside from the stacktrace. – xR34P3Rx Jul 22 '22 at 14:20
  • Try the full path returned by `which uptime`. – trashgod Jul 22 '22 at 15:39
  • Tried that as well. Still nothing... I did run my project on the actual machine that we will be deploying the software to and running it from the IDE, it loaded everything just fine. I wonder if there's some Java permission i need to setup or something. As mentioned, I'm building the project as JLink, so I wonder if that has something to do with it... – xR34P3Rx Aug 01 '22 at 14:13
  • Sorry, I've not used [tag:jlink], but it's hard to imagine `uptime` requiring more privilege than launching the JVM. Can you launch the application's JAR directly on the target to see more? – trashgod Aug 01 '22 at 15:17
  • On my development machine (which is Linux Mint) I built the project with JLink and the app ran just fine. I then copied the files built on my dev machine and sent it to the production machine in a ZIP and it launched just fine! I think its something funky GitHub is doing when building the project using Workflows... But glad its not an actual Linux Permission issue because I have changed so many permissions and nothing helped, lol. Thanks though! – xR34P3Rx Aug 01 '22 at 16:01
  • 1
    I've encountered such problems based on file permissions, line terminators, etc. Glad it's working; you might update the question to reflect the difficulty reproducing the problem. – trashgod Aug 01 '22 at 16:22