Java GUI加载后,如何在Raspberry Pi上启动程序?

Java GUI加载后,如何在Raspberry Pi上启动程序?,java,variables,raspberry-pi,startup,display,Java,Variables,Raspberry Pi,Startup,Display,好的,我有一个简单的java程序: import java.awt.AWTException; import java.awt.Robot; class Program { public static void main(String[] args) throws AWTException { Robot robot = new Robot(); // do some things with the robot } } 我想在启动时在我

好的,我有一个简单的java程序:

import java.awt.AWTException;
import java.awt.Robot;

class Program
{
    public static void main(String[] args) throws AWTException
    {
        Robot robot = new Robot();
        // do some things with the robot
    }
}
我想在启动时在我的Raspberry Pi上运行这个程序,所以我把它放在一个shell脚本中,我在
/etc/rc.local
文件末尾调用了这个脚本。这是我的shell脚本:

cd /home/pi/Desktop
java Program
每当我的Raspberry Pi启动时,我的程序在尝试实例化Robot类时就会抛出一个异常,即
无法连接到X11 window server,并使用“0.0”作为显示变量的值。后来我发现这是因为在执行程序时GUI还没有加载,所以我在
rc.local
中设置了延迟。以下是
rc.local
中的最后几行:

sleep 60s
sudo sh /home/pi/Desktop/launcher.sh &
exit 0
尽管程序在GUI加载后开始运行,但仍会抛出此异常。我试过:

  • 使用
    .bashrc
    而不是
    rc.local
  • 在调用实际程序之前,在我的
    launcher.sh
    脚本中设置延迟
  • 调用另一个在开始时有延迟的shell脚本,然后调用
    launcher.sh

  • 我没有成功,已经耗尽了我的所有想法,也不知道还有什么地方可以搜索到这个问题的解决方案。

    要在GUI启动时启动应用程序,您应该将.desktop文件添加到autostart目录中

    因此,在
    ~/.config/autostart
    目录中,创建
    my_script.desktop
    文件(用所需内容替换my_script)

    编辑它(
    nano my_script.desktop
    ),使其看起来像这样:

    [Desktop Entry] 
    Name=put_name_here
    Exec=type_command_to_run_here
    Type=application
    Terminal=true/false (true if you want it to run in terminal) 
    

    您是否尝试过
    ~/.config/autostart/
    变体(cerate a
    myprogram.desktop
    )?感谢您的建议@Robert,最终我使用路径`~/.config/lxsession/LXDE pi/autostart`来运行我的程序,因为
    autostart
    目录不在
    .config
    中。谢谢@masan。我的
    autostart
    目录不在
    .config
    中,因此我手动添加了它,并尝试了您的建议。不幸的是,它仍然不起作用,但我认为我可能错误地将命令键入
    Exec
    参数。不管怎样,您的回复让我找到了另一个类似的解决方案,我必须在这个文件的末尾添加命令:`~/.config/lxsession/LXDE pi/autostart`,以便在GUI启动后运行它。再次感谢!
    [Desktop Entry] 
    Name=put_name_here
    Exec=type_command_to_run_here
    Type=application
    Terminal=true/false (true if you want it to run in terminal)