1

Using a Java program, I added a .desktop file in /etc/xdg/autostart to run a script when a user logs in. The script didn't run at user login, and when I checked the start up applications dialog manually, it wasn't added to it.

The file I added is /etc/xdg/autostart/Startup.desktop, and the script I want to be run on user login is /usr/bin/Startscript. The code I wrote to add the desktop file is as follows:

private void writeDesktopFile() {

        File f = new File("/etc/xdg/autostart/Startup.desktop");
        if (!(f.exists())) {
            try {
                f.createNewFile();
                BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));
                bw.write("[Desktop Entry]");
                bw.newLine();
                bw.append("Name=Startup");
                bw.newLine();
                bw.append("Exec=Startscript");
                bw.newLine();
                bw.append("Type=Application");
                bw.newLine();
                bw.append("Terminal=false");
                bw.newLine();
                bw.append("Categories=GNOME;GTK;Utility;");
                bw.newLine();
                bw.append("X-Ubuntu-Gettext-Domain=Startscript");
                bw.newLine();
                bw.flush();
                bw.close();
                Runtime.getRuntime().exec("chmod +x /usr/bin/Startscript");
            } catch (IOException ex) {
                Logger.getLogger(LinuxStartup.class.getName())
                        .log(Level.SEVERE, null, ex);
            }
        }

Why isn't my script being run on start up?

  • 2
    Is there another Startup.desktop in another autostart directory, such as $HOME/.config/autostart/? What are the actual contents of /etc/xdg/autostart/Startup.desktop after running your program? – Dan Getz Jul 1 '14 at 12:19
  • @DanGetz Yes there was another script in /.config/autostart. I removed it and tried but same result. What causes the script to automatically insert in /.config/autostart folder? – Joker Jul 4 '14 at 4:07
  • Was that file exactly equal to the one you had created in /etc/xdg/autostart/? Or was it a different file? Once again, it's important to check the contents of these .desktop files. – Dan Getz Jul 4 '14 at 11:00
-1

Java is a non script language so you need to compile the code.

I recommend compiling it into a jar bundle and then write a Shell Script to launch the jar.

#!/bin/sh

java -jar <compiled jar>.jar
exit 0
  • I m compiling the jara and it running the jar file from the script. But it doesn't work. When we add a .desktop file it should be automatically added to Startup Applications. In my case that doesn't happen, that is why I asked help here my compiled jar works fine when I run the script. – Joker Jul 1 '14 at 6:20
  • 3
    OP uses Java only to create a desktop shortcut and has problem with shortcut creation. – SkyDan Jul 1 '14 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.