在Debian系统中启动Java服务,您需要遵循以下步骤:
sudo apt update
sudo apt install openjdk-11-jdk
// HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
使用以下命令编译Java程序:
javac HelloWorld.java
使用以下命令将编译后的类文件打包成JAR文件:
jar cvfe HelloWorld.jar HelloWorld HelloWorld.class
/etc/systemd/system/目录下创建一个名为java-service.service的文件:sudo nano /etc/systemd/system/java-service.service
将以下内容粘贴到文件中,根据实际情况修改ExecStart和WorkingDirectory:
[Unit]
Description=Java Service
After=network.target
[Service]
Type=simple
User=<your-user>
Group=<your-group>
ExecStart=/usr/bin/java -jar /path/to/your/jar-file/HelloWorld.jar
Restart=on-failure
[Install]
WantedBy=multi-user.target
保存并关闭文件。
sudo systemctl daemon-reload
sudo systemctl start java-service
sudo systemctl status java-service
如果一切正常,您的Java服务现在应该已经启动并运行了。
sudo systemctl enable java-service
现在,您的Java服务将在Debian系统启动时自动运行。