2014-01-20 53 views
0

我想运行命令amixer set Master 50%通过Runtime.getRuntime().exec(); 而且它不起作用。 任何线索如何让它工作?JAVA如何通过exec运行amixer

+0

在这里发布你的努力...你到目前为止尝试过什么? –

回答

1

我在UDOO ubuntu上遇到了同样的问题。我通过用bash脚本包装命令来解决它。

#!/bin/bash amixer sset 'Stereo Master' $1

而且从Java调用脚本。

0

要使用参数运行命令,您需要将其作为String阵列传递。

来自官方的Java文档:

public Process exec(String[] cmdarray) 
      throws IOException 
Executes the specified command and arguments in a separate process. 
This is a convenience method. An invocation of the form exec(cmdarray) behaves in exactly the same way as the invocation exec(cmdarray, null, null). 

换句话说,通过每个单独的那些字符串传递到exec()String[]

String[] cmdArgs = new String[] {"amixer", "set", "Master", "50%"}; 
0

我想这样,但它不工作。我的代码非常简单,我想从java exec运行amixer卷设置。这是我的代码:

String[] cmdArgs = new String[] {"amixer", "set", "Master", "100%"}; 
     Process pro = Runtime.getRuntime().exec(cmdArgs); 
     pro.waitFor(); 

编辑: 我试图在Ubuntu和它的工作,但我需要在树莓派,在安装的Java 1.8中运行它。也许这是一个问题。

+1

尝试在“”之间添加空格 –