3

So I just installed the 'brightness' utility package using homebrew (its basically a way to adjust screen brightness through terminal), but I'm having a hard time running this code in java:

    Process p = Runtime.getRuntime().exec("brightness -l");

When I run "brightness -l" through terminal, it gives the current screen brightness level. But when I try the line through java it throws this error:

Exception in thread "main" java.io.IOException: Cannot run program "brew": error=2, No such file or directory

I've tried the following:

    Process p = Runtime.getRuntime().exec("/usr/local/bin/ brightness -l");

but it gives me a permission denied error:

Exception in thread "main" java.io.IOException: Cannot run program "/usr/local/bin/": error=13, Permission denied

So I guess it'll work if I grant permission to regular users to access bin. But thats too risky, is there any other way to do it?

Lucas Prestes
  • 362
  • 1
  • 4
  • 19
  • 1
    Ummm ... "/usr/local/bin" is a directory. That is why you can't execute it! Regular users should have "r_x" permissions on it anyway. Note that "x" doesn't mean execute for a directory. – Stephen C Jul 25 '18 at 12:11

2 Answers2

1

The problem in your method is that you are not running command through bash exclusively. So my Solution would be something like

Runtime.getRuntime().exec("/bin/bash -c brightness -l");

Moreover from it is advisable to use ProcessBuilder instead because usage of Runtime.exec() is now discouraged look the documentation

So my final solution would be :

String[] args = new String[] {"/bin/bash", "-c", "brightness" ,"-l"};
Process proc = new ProcessBuilder(args).start();

for more examples of ProcessBuilder see this topic

Lucas Prestes
  • 362
  • 1
  • 4
  • 19
Avinash Karhana
  • 659
  • 4
  • 16
  • 'Runtime.getRuntime().exec("/bin/bash -c brightness -l");' isn't working, so I tried creating a bash file to execute the command but when I run the bash file my reader returns null. Do you think its a problem with my reader or the Runtime line itself? 'while ((line = reader.readLine()) != null) { if(line.includes.........) } – Nizar G. Habib Jul 25 '18 at 12:14
  • try running some other command as like to open a application...maybe there is some problem with brightness itself – Avinash Karhana Jul 25 '18 at 12:22
  • I added an echo line to the bash file to see if its even running the bash file, but I guess its not. Runtime.getRuntime().exec("/bin/bash "); is returning null for some reason. – Nizar G. Habib Jul 25 '18 at 12:34
  • I turned into an sh, but for some reason java is still not giving me any output. *#!/bin/sh echo "hi" brightness -l* it wont even return "hi" – Nizar G. Habib Jul 25 '18 at 12:52
0

The syntax that worked for me was:

String command = "echo $(/usr/local/Cellar/brightness/1.2/bin/brightness -l) >    /Users/nizhabib/Desktop/FileName";
    Process po = new ProcessBuilder("/bin/sh", "-c", command).start();

Where the 'command' variable holds the directory that includes the executable 'brightness' and '-l' is a flag for the function 'brightness'. The command simply pours the output of 'brightness -l' into the text file 'FileName'.

in 'process po' we specify the type of the file in the first argument which in this case is 'sh' for shell script and -c to specify that '/usr/local/Cellar/brightness/1.2/bin/brightness -l' is to be handled as a string (because the path expression is arbitrary)