通过Java进行OSD通知

时间:2012-01-24 13:19:44

标签: java notifications ubuntu-10.04

我试图通过java

访问Ubuntu中的通知气泡

这是我的代码

class TestOSD{

public static void main(String[] args) throws Exception{
    String[] cmmd = {"/usr/bin/notify-send -t 10000 \"Hello OSD\" \"This is my first programmatic OSD notification\""};
    System.out.println("This is said : Hello OSD");
    Runtime.getRuntime().exec(cmmd);
}
}

我在命令行中获取此堆栈跟踪

Exception in thread "main" java.io.IOException: Cannot run program "/usr/bin/notify-send -t 10000 "Hello OSD" "This is my first programmatic OSD notification"": java.io.IOException: error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
    at java.lang.Runtime.exec(Runtime.java:593)
    at java.lang.Runtime.exec(Runtime.java:466)
    at TestOSD.main(TestOSD.java:6)
Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:148)
    at java.lang.ProcessImpl.start(ProcessImpl.java:65)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
    ... 3 more

基本上我无法理解Runtime类的exec()方法的重载?在这方面有人可以帮助我吗?

1 个答案:

答案 0 :(得分:11)

我认为问题在于它正在寻找整个字符串作为要启动的进程的名称。试试这个:

String[] cmd = { "/usr/bin/notify-send",
                 "-t",
                 "10000",
                 "Hello OSD This is my first programmatic OSD notification"};

这应该从参数中分离出进程的名称,并从其余参数中分离出每个参数。