如何在 Ubuntu Linux 启动时运行 Java 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22712080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:25:37  来源:igfitidea点击:

How to run Java application on startup of Ubuntu Linux

javalinuxsocketsubuntuinit

提问by Ram

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.

我有一个需求,我需要开发读取 TCP/IP 套接字的应用程序。我成功地将该程序制作为没有 GUI 的 Java 程序,这意味着一旦程序运行它就开始侦听 Socket 并在 Netbeans IDE 的帮助下获得响应。现在根据我的要求,我必须在 Linux 系统启动后立即开始执行该程序。

Actually I am very novice in Java and Linux Platform, so have few doubts..

实际上我在 Java 和 Linux 平台方面非常新手,所以几乎没有怀疑..

  1. Is my Socket Program with no GUI is fine to be run as per my requirement.
  2. How can I write script to run jar on Linux Boot up, I got to know. init.dis meant for this.
  1. 我的没有 GUI 的 Socket 程序是否可以按照我的要求运行。
  2. 我如何编写脚本以在 Linux 启动时运行 jar,我知道了。init.d是为了这个。

采纳答案by PbxMan

Ideally you should create a service wrapper for your java application and then make this service run on startup example here.

理想情况下,您应该为 Java 应用程序创建一个服务包装器,然后在此处的启动示例中运行此服务。

Use
sudo update-rc.d mytestserv defaultsto run your service wrapper on startup on Ubuntu

用于
sudo update-rc.d mytestserv defaults在 Ubuntu 上启动时运行您的服务包装器

回答by Will

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

首先创建一个小的 shell 脚本来从终端启动你的 java 程序。由于您已打包为 jar,因此请查看JAR Files as Applications部分,特别是该部分。

This may be sufficient: (although you'll want to use the full path to Java)

这可能就足够了:(尽管您需要使用 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.dto start it on boot. See herefor a very simple example of using update-rc.d

一旦从脚本开始,您就可以使用标准的 linux 工具来启动脚本。将它放入/etc/rc.local,或者在您使用 Ubuntu 时,使用update-rc.d在启动时启动它。有关使用的非常简单的示例,请参见此处update-rc.d

Hope this helps,

希望这可以帮助,

Will

将要