gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:04:08 24 4
gpt4 key购买 nike

好的,我有这个简单的 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 启动时,我的程序会在尝试实例化时抛出异常: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

我没有成功,并且已经耗尽了我所有的想法,并且不知道还能在哪里寻找该问题的解决方案。

最佳答案

要在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)

关于java - GUI 加载后,如何在 Raspberry Pi 上启动时运行程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51106040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com