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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With