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
4001 gold badge5 silver badges19 bronze badges
asked Jul 25, 2018 at 11:54
1
  • 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. Commented Jul 25, 2018 at 12:11

2 Answers 2

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
4001 gold badge5 silver badges19 bronze badges
answered Jul 25, 2018 at 12:05
Sign up to request clarification or add additional context in comments.

4 Comments

'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.........) }
try running some other command as like to open a application...maybe there is some problem with brightness itself
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 <FILE_NAME>"); is returning null for some reason.
I turned <FILE_NAME> 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"
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)

answered Jul 26, 2018 at 11:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.