我有一些问题。当我运行命令时:
openssl md5 "./build/outputs/apk/myApp.apk"
我得到了我需要的结果,所以openssl工作正常。 然后是我的Java代码:
String md5_cmd = "openssl md5 \"./build/outputs/apk/myApp.apk\"";
String md5Str = obj.executeCommand(md5_cmd);
String whichCmd = obj.executeCommand("which openssl"); //For testing executeCommand
System.out.println(md5Str); //Not prints anything
System.out.println(whichCmd); //Prints the result just Fine
private String executeCommand(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
所以,第一个命令“md5_cmd”没有显示任何输出,虽然如果我直接通过命令行运行它就行了。带有“whichCmd”的第二个命令按预期工作。你能建议吗?
答案 0 :(得分:1)
而不是使用单个字符串运行命令
String md5_cmd = "openssl md5 \"./build/outputs/apk/myApp.apk\"";
带参数的命令应使用String []
运行String[] md5_cmd = {"openssl", "md5", "./build/outputs/apk/myApp.apk"};