本文介绍如何将java运行环境、jar程序一起打包成一个deb格式的安装包,创建桌面图标,通过点击图标可使用系统自带浏览器快捷访问web服务的URL,同时注册服务并配置好开机自启。
准备环境
- 对应linux内核版本的debian或ubuntu或deepin环境虚拟机
- jar包
- 对应版本的java安装包
准备deb包结构
- 创建项目目录结构,debian目录即为用来打包的目录
mkdir debpackage
cd debpackage
mkdir -p debian/usr/share/helloapp
mkdir -p debian/usr/share/applications
mkdir -p debian/etc/systemd/system
mkdir -p debian/DEBIAN
将 java 运行环境 jre 目录拷贝到 debian/usr/share/helloapp 目录
将 jar包拷贝到 debian/usr/share/helloapp 目录下
将应用图标文件 icon.png 拷贝到 debian/usr/share/helloapp 目录下
创建启动脚本
vi debian/usr/share/helloapp/start.sh
#!/bin/bash
JAVA_HOME=/usr/share/helloapp/jre
$JAVA_HOME/bin/java -jar /usr/share/helloapp/helloword.jar
chmod +x debian/usr/share/helloapp/start.sh
- 创建systemd服务文件
vi debian/etc/systemd/system/helloapp.service
[Unit]
Description=小工具
After=network.target
[Service]
ExecStart=/usr/share/helloapp/start.sh
WorkingDirectory=/usr/share/helloapp
Restart=always
[Install]
WantedBy=multi-user.target
- 创建桌面快捷方式
vi debian/usr/share/applications/helloapp.desktop
[Desktop Entry]
Version=1.0
Categories=Application
Name=helloapp
Comment=myapp
Encoding=UTF-8
Exec=/usr/share/helloapp/start.sh
Icon=/usr/share/helloapp/img/icon.png
StartupNotify=true
Terminal=false
Type=Application
X-Deepin-Vendor=user-custom
- 如果应用使用浏览器访问URL,则修改该文件,用浏览器打开URL:
[Desktop Entry]
Version=1.0
Name=helloapp
Name[zh_CN]=小工具
Type=Application
Encoding=UTF-8
Comment=Open My Java Application in Browser
Exec=xdg-open http://127.0.0.1:13099
Icon=/usr/share/helloapp/icon.png
StartupNotify=true
Terminal=false
X-Deepin-Vendor=user-custom
Categories=Network;WebBrowser;
- 想用特定浏览器(如 Chrome/Firefox),修改
.desktop
文件,并添加对应浏览器到Depends
(如google-chrome-stable
)。
Exec=google-chrome http://127.0.0.1:13099
或
Exec=firefox http://127.0.0.1:13099
- 创建Debian控制文件
vi debian/DEBIAN/control
Package: helloapp
Version: 1.0
Section: utils
Priority: optional
Architecture: all
Depends: xdg-utils
Maintainer: Aaron
Description: java小工具
Architecture: all
:适用于所有架构(纯脚本/URL启动),amd64Depends: xdg-utils
:确保xdg-open
可用(用于打开浏览器)
- 创建安装后处理脚本
vi debian/DEBIAN/postinst
#!/bin/sh
# Enable and start the service
systemctl daemon-reload
systemctl enable helloapp.service
systemctl start helloapp.service
# Update desktop database
update-desktop-database /usr/share/applications
exit 0
chmod +x debian/DEBIAN/postinst
- 创建卸载前处理脚本
vi debian/DEBIAN/prerm
#!/bin/sh
# Stop and disable the service
systemctl stop helloapp.service
systemctl disable helloapp.service
exit 0
chmod +x debian/DEBIAN/prerm
构建Deb包
chmod -R 775 debian
dpkg-deb -b debian helloapp_1.0_arm.deb
测试安装
dpkg -i helloapp_1.0_arm.deb
常用操作命令
dpkg --list
dpkg-query -l
dpkg -r helloapp
# 列出特定软件包的详细信息
dpkg -L helloapp
dpkg -s helloapp
apt list helloapp
报错:(未解决,在debian虚拟机上安装报错,但是在银河麒麟系统上安装正常)
Error in file "/usr/share/applications/evince.desktop": "" is an invalid MIME type ("" does not contain a subtype)