0

I want to run a jar file at startup. So i referred here. I ran the steps manually as indicated by Magpie, i e., using autostart and it worked without problem. But I tried the same in java, i e., from creating script to copying the file to /etc/xdg/autostart. All the files are created safely and there is no exception or error. But my jar file doesn't run on startup.

The script contents are as follows:

#!/bin/bash

### BEGIN INIT INFO
# Provides:          testscript.sh
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

java -jar /home/user/Documents/Parthe/StartupTest/Start.jar

The contents of .desktop file is as follows:

[Desktop Entry]
Name=test
Exec=/usr/bin/testscript.sh
Type=Application
Terminal=false

The java program I used to create these files is as follows:

private void writeScript() {
        File f = new File("/usr/bin/testscript.sh");
        if (!(f.exists())) {

            try {
                f.createNewFile();
                BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));
                bw.write("#!/bin/bash");
                bw.newLine();
                bw.newLine();
                bw.append("### BEGIN INIT INFO");
                bw.newLine();
                bw.append("# Provides:          testScript");
                bw.newLine();
                bw.append("# Required-Start:    $remote_fs $syslog");
                bw.newLine();
                bw.append("# Required-Stop:     $remote_fs $syslog");
                bw.newLine();
                bw.append("# Default-Start:     2 3 4 5");
                bw.newLine();
                bw.append("# Default-Stop:      0 1 6");
                bw.newLine();
                bw.append("# Short-Description: Start daemon at boot time");
                bw.newLine();
                bw.append("# Description:       Enable service provided by daemon.");
                bw.newLine();
                bw.append("### END INIT INFO");
                bw.newLine();
                bw.newLine();
                bw.append("java -jar /home/user/Documents/Parthe/StartupTest/Start.jar");
                bw.newLine();
                bw.flush();
                bw.close();
            } catch (IOException ex) {
                Logger.getLogger(TestStartup.class.getName())
                        .log(Level.SEVERE, null, ex);
            }
        }
        writeDesktopFile();

    }

    private void writeDesktopFile() {

        JOptionPane.showMessageDialog(null, "inside Write Desktop");
        File f = new File("/usr/share/applications/test.desktop");
        if (!(f.exists())) {
            try {
                f.createNewFile();
                JOptionPane.showMessageDialog(null, "File created");
                BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));
                bw.write("[Desktop Entry]");
                bw.newLine();
                bw.append("Name=test");
                bw.newLine();
                bw.append("Exec=/usr/bin/testscript.sh");
                bw.newLine();
                bw.append("Type=Application");
                bw.newLine();
                bw.append("Terminal=false");
                bw.flush();
                bw.close();
                moveFile();
            } catch (IOException ex) {
                Logger.getLogger(TestStartup.class.getName())
                        .log(Level.SEVERE, null, ex);
            }
        }
    }

    private void moveFile() {
        try {
            Process p = Runtime.getRuntime().exec("cp "
                    + "/usr/share/applications/test.desktop "
                    + "/etc/xdg/autostart/");
            Runtime.getRuntime().exec("chmod +x /usr/bin/testScript");
        } catch (IOException ex) {
            Logger.getLogger(TestStartup.class.getName())
                    .log(Level.SEVERE, null, ex);
        }
    }

The jar file which has to run at startup contains a print statement and a Joptionpane message dialog both printing "Success".

Community
  • 1
  • 1
Joker
  • 81
  • 12
  • Most probably you forgot to `chmod +x /usr/bin/testscript.sh`. – basin May 23 '14 at 11:43
  • Where do you expect the print statement to be printed? You configured `Terminal=false` so the print statement is nowhere printed. The JoptionPane can't be displayed because the jar file is executed before the X server started. – Absurd-Mind May 23 '14 at 11:44
  • He said: "Joptionpane message dialo" – basin May 23 '14 at 11:49
  • Doesn't change the problem, if there is no X server, a message dialog can't be displayed. – Absurd-Mind May 23 '14 at 11:50
  • I repeated Parthe's steps, created a simple jar file that displays a message dialog. It works: the message is shown after reboot – basin May 23 '14 at 12:46
  • @basin see `moveFile()` method, I changed the permissions. When running manually the message dialog is displayed. But when edit scripts via java and run it, it won't start on reboot. – Joker May 24 '14 at 03:46
  • @Absurd-Mind I have a X server. How do I make the script run after X server starts? But when operated manually, the message dialog runs, how is that possible? – Joker May 24 '14 at 03:49
  • Maybe the problem is with letter case in `moveFile()`: change `testScript` to `testscript`? – basin May 24 '14 at 07:34
  • @basin I too thought that might be a problem. I changed it to `testscript`, made the necessary corrections and re did the whole process. Same results, manual execution works not java execution – Joker May 24 '14 at 08:56
  • What do you mean by "manual execution"? run in terminal `/usr/bin/testscript.sh`. Does it work? Open `/etc/xdg/autostart/` in file manager and click `test.desktop`. Does it work? – basin May 24 '14 at 13:19
  • @basin That's what I meant by manual execution. Sorry if that confused you. When I write the scripts and execute it from terminal it works but when I do the same in java, it doesn't run. Is the problem with the `gedit file.desktop &`? It is said that '&' is used to keep the file open and execute.If that's the case, then there shouldn't be any problem right? – Joker May 26 '14 at 04:34
  • @Absurd-Mind I tried running with `Terminal=true`. Nothing happened. What should actually be the result when we change `Terminal=true`? – Joker May 26 '14 at 04:42
  • @basin I tried clicking on `test.desktop` but it shows `Untrusted application launcher`. – Joker May 26 '14 at 05:22
  • Write the files in java, then run in terminal: `/usr/bin/testscript.sh` . What it prints? – basin May 26 '14 at 07:23
  • @basin I tried that. It worked perfectly. Weird thing is a new line after `Terminal=false` made it work. Has anyone any idea on this? – Joker May 26 '14 at 07:26
  • I just noticed, why in your program the path is `/usr/share/applications` and not `/etc/xdg/autostart` ? Why not create that file in right place instead of moving it? – basin May 26 '14 at 07:40
  • Next, I hope you have xdg-open installed. Write the files in java and run in terminal: `xdg-open /etc/xdg/autostart/test.desktop` – basin May 26 '14 at 07:42
  • @basin File is written directly in `etc/xdg/autostart`. When I open with `xdg-open`, the script opens in Libre office – Joker May 26 '14 at 09:01
  • Ok then, when you click the desktop file, it tells you it's not trusted. Does this error dialog have a button like 'launch anyway'? What if you copy paste that desktop file as non-root to your home folder and click it there? – basin May 26 '14 at 09:10
  • @basin There is no 'launch anyway' button. I am not able to access it as non-root as it is in `/etc`. – Joker May 26 '14 at 09:30
  • You should at least be able to read that file as non-root. Maybe that's the problem? – basin May 26 '14 at 12:06
  • @basin I am able to read and write from terminal but on click it shows `Untrusted Application Launcher` – Joker May 26 '14 at 12:14

0 Answers0