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".