简体   繁体   English

如何通过java程序运行brew命令?

[英]How to run brew command through java program?

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: 所以我刚刚使用自制软件安装了'亮度'实用程序包(它基本上是通过终端调整屏幕亮度的一种方式),但是我很难在java中运行这个代码:

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

When I run "brightness -l" through terminal, it gives the current screen brightness level. 当我通过终端运行“brightness -l”时,它会给出当前的屏幕亮度级别。 But when I try the line through java it throws this error: 但是,当我尝试通过java的行时,它会抛出此错误:

Exception in thread "main" java.io.IOException: Cannot run program "brew": error=2, No such file or directory 线程“main”中的异常java.io.IOException:无法运行程序“brew”:error = 2,没有这样的文件或目录

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 线程“main”中的异常java.io.IOException:无法运行程序“/ usr / local / bin /”:error = 13,权限被拒绝

So I guess it'll work if I grant permission to regular users to access bin. 所以,如果我授予普通用户访问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. 您的方法中的问题是您没有专门通过bash运行命令。 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 此外,建议使用ProcessBuilder,因为现在不鼓励使用Runtime.exec() 查看文档

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 有关ProcessBuilder的更多示例, 请参阅此主题

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'. 'command'变量保存包含可执行文件'brightness'的目录,'-l'是函数'brightness'的标志。 The command simply pours the output of 'brightness -l' into the text file 'FileName'. 该命令只是将'brightness -l'的输出倒入文本文件'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) 在'process po'中,我们在第一个参数中指定文件的类型,在本例中为shell脚本为“sh”,而-c指定'/us以指定'/usr/local/Cellar/brightness/1.2/bin/brightness -l '将作为字符串处理(因为路径表达式是任意的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM