我试图使用zenity命令从用户那里获取输入。下面是我传递给zenity的命令:
zenity --question --title "Share File" --text "Do you want to share file?"下面是使用Java执行命令的代码:
private String[] execute_command_shell(String command)
{
System.out.println("Command: "+command);
StringBuffer op = new StringBuffer();
String out[] = new String[2];
Process process;
try
{
process = Runtime.getRuntime().exec(command);
process.waitFor();
int exitStatus = process.exitValue();
out[0] = ""+exitStatus;
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null)
{
op.append(line + "\n");
}
out[1] = op.toString();
}
catch (Exception e)
{
e.printStackTrace();
}
return out;
}虽然我得到了一个输出对话框,但标题只有第一个单词“共享”,问题的文本也只显示了一个单词"Do“
对这种奇怪的行为有什么解释吗?周围的工作是什么?
发布于 2015-11-23 22:15:48
这对我起了作用:
Runtime.getRuntime().exec(new String[]{"zenity","--question","--title","Share File","--text","Do you want to share file?"})我建议将java代码中的参数拆分,这样您就可以检查而不是用引号传递整个命令。
下面是一个示例,包括处理引号的拆分:
String str = "zenity --question --title \"Share File\" --text \"Do you want to share file?\"";
String quote_unquote[] = str.split("\"");
System.out.println("quote_unquote = " + Arrays.toString(quote_unquote));
List<String> l = new ArrayList<>();
for(int i =0; i < quote_unquote.length; i++) {
if(i%2 ==0) {
l.addAll(Arrays.asList(quote_unquote[i].split("[ ]+")));
}else {
l.add(quote_unquote[i]);
}
}
String cmdarray[] = l.toArray(new String[l.size()]);
System.out.println("cmdarray = " + Arrays.toString(cmdarray));
Runtime.getRuntime().exec(cmdarray);发布于 2019-08-22 05:37:32
或者,您可以使用UiBooster for Java来创建这个对话框。这样,您就不必安装zenity,而且您也不局限于windows上的linux或gtk。
String userInput = new UiBooster().showTextInputDialog("Do you want to share file?");https://stackoverflow.com/questions/33878802
复制相似问题