ashish_r
Posts: 5
Joined: Thu Mar 22, 2018 5:10 pm

auto execution jar file after raspberry start up using cron job

Thu Mar 22, 2018 6:39 pm

i want to auto execution GUI based jar file after raspberry start up using cron job , i got exception such like

Exception in thread "main" java.awt.HeadlessException at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:204) at java.awt.Window.(Window.java:536) at java.awt.Frame.(Frame.java:420) at java.awt.Frame.(Frame.java:385) at javax.swing.JFrame.(JFrame.java:189) at com.mycompany.gui.FirstSwingExample.main(FirstSwingExample.java:43)


my crontab command is @reboot /usr/bin/java -jar /home/pi/Desktop/GUI-1.0-SNAPSHOT.jar > /home/pi/Desktop/log.txt 2>&1

i also passed JVM argument -Djava.awt.headless=true

please help me

knute
Posts: 452
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

Re: auto execution jar file after raspberry start up using cron job

Thu Mar 22, 2018 9:57 pm

You do know that headless and GUI are mutually exclusive?

knute
Posts: 452
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

Re: auto execution jar file after raspberry start up using cron job

Thu Mar 22, 2018 10:28 pm

The @reboot scheme isn't going to work either because the X11 desktop isn't running when cron tries to start the program, hence the headless errors.

Probably the simplest method to run a program like this is to use a Desktop file in your autostart directory. In the /home/pi/.config/autostart directory create a file test.desktop:

[Desktop Entry]
Name=test
Type=Application
Exec=java -jar test.jar
Terminal=true
Hidden=false

Here is a simple Java Swing test program:

Code: Select all

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JFrame {
    public test() {
        super("test");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        add(new JLabel("test",JLabel.CENTER),BorderLayout.CENTER);
        setSize(200,150);
        setVisible(true);
        setLocationRelativeTo(null);
    }

    public static void main(String... args) {
        EventQueue.invokeLater(() -> new test());
    }
}
Copy and paste the test program to ~
Compile test.java: javac test.java
Jar it: jar cvfe test.jar test *.class
Delete the .class files rm *.class

It should run after the desktop is up. Now you should be able to get your program running.

ashish_r
Posts: 5
Joined: Thu Mar 22, 2018 5:10 pm

Re: auto execution jar file after raspberry start up using cron job

Fri Mar 23, 2018 4:00 pm

:lol: :lol: :o its working knute sir ,,, thanks a lot

Return to “General discussion”