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 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)