问题
I have a requirement where I need to develop application that reads TCP/IP Socket. I successfully made the program as Java program with No GUI means as soon as program runs it starts listening to the Socket and gets the response with the help of Netbeans IDE. Now as per my requirement i have to start execution of this program as soon as Linux system Booted.
Actually I am very novice in Java and Linux Platform, so have few doubts..
- Is my Socket Program with no GUI is fine to be run as per my requirement.
- How can I write script to run jar on Linux Boot up, I got to know.
init.d
is meant for this.
回答1:
Ideally you should create a service wrapper for your java application and then make this service run on startup example here.
Usesudo update-rc.d mytestserv defaults
to run your service wrapper on startup on Ubuntu
回答2:
So two things you'll need to do:
First create a small shell script to start your java program from a terminal. As you have packaged as a jar have a look at this, specifically the JAR Files as Applications
section.
This may be sufficient: (although you'll want to use the full path to Java)
#!/bin/bash
java -jar path_to_jar_file
You should be able to run your script and successfully start your program.
Once you've got it starting from a script you can use standard linux tools to start the script. Either putting it in /etc/rc.local
, or as you're using Ubuntu, use update-rc.d to start it on boot. See here for a very simple example of using update-rc.d
Hope this helps,
Will
来源:https://stackoverflow.com/questions/22712080/how-to-run-java-application-on-startup-of-ubuntu-linux