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

时间:2018-06-29 16:31:21

标签: 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
    }
}

我想在启动时在Raspberry Pi上运行该程序,因此我将其放在了/etc/rc.local文件末尾调用的shell脚本中。这是我的shell脚本:

cd /home/pi/Desktop
java Program

每当我的Raspberry Pi启动时,我的程序都会在试图实例化Robot类时抛出一个异常Can't connect to X11 window server using :'0.0' as the value of the DISPLAY variable。后来我发现这是因为执行程序时GUI尚未加载,所以我在rc.local中加了一个延迟。这些是rc.local中的最后几行:

sleep 60s
sudo sh /home/pi/Desktop/launcher.sh &
exit 0

尽管程序在GUI加载后开始运行,但仍会抛出此异常。我尝试过:

  1. 使用.bashrc代替rc.local
  2. 在调用实际程序之前在我的launcher.sh脚本中放置一个延迟时间
  3. 调用另一个在开始时有延迟的shell脚本,然后调用launcher.sh

我没有成功,已经耗尽了我所有的想法,不知道在其他地方寻求解决方案。

1 个答案:

答案 0 :(得分:0)

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

因此,在~/.config/autostart目录中,创建my_script.desktop文件(用您想要的任何内容替换 my_script )。

touch my_script.desktop

编辑(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)