Java工程项目打包成一个安装包从而在电脑上部署

我们都知道生物信息其实包含方方面面非实验工作,只是应用在生物中,解决生物学问题。一句话总结:生信是个框,不做实验都往里面装。(不喜勿喷)

0. 本文背景:

假设你是一个Java开发人员,你撰写了一个专门用于处理生物问题的图形界面软件。你在Java的集成开发平台(IDE)已经开发好了这个软件。现在你需要把项目所有的东西(源代码,配置文件,图文资料,以及Java运行环境,一下简称JRE)都打包成为一个安装包。然后使得用户只要下载了这个安装包,再双击安装,然后一路点击next(也就是下一步)安装好这个软件,使用即可。

下面我们来介绍一下如何来打包这个由Java撰写的软件。

这里先介绍一个Java的常识,高级编程语言一般可以粗略分成两类:第一种是编译型语言,第二种是脚本型语言。前者写完源代码后,需要编译一下,生成可执行的二进制文件(因为计算机只懂0和1);后者没有编译环节,直接调用解释器即可运行源代码文件。
Java严格来分属于编译型语言,它的源代码是.java文件,可执行文件需要编译成.class文件。源代码最基本的单位是类,类可以组织成包,包可以再打成jar文件。Jar文件分为可执行的jar文件与不可执行的jar文件,后者可以认为是一种类似rar,zip的压缩文件格式。在jar文件中有一个声明jar文件的配置文件,它永远位于jar文件中的"/META-INF/MANIFEST.MF"
可执行jar文件与不可执行jar文件的区别在于前者的"MANIFEST.MF"文件中包含了启动类与依赖库文件的目录。

所以对应于这篇文章的标题来说,打包与部署其实更准确的来说是:一个config目录,一个resource目录,一个lib目录,一个jar文件,和一个JRE(也是一个目录)。以window系统为例如何打包成为一个exe安装包,然后点击下一步下一步安装之后桌面出现一个快捷方式,双击快捷方式即可使用软件!
当然,开发者也可以直接以上面那种形式发布,毕竟对于可执行jar文件来说,双击文件也可以运行软件(前提是你的电脑配置了Java运行环境)。但是对于很多人来说那可能不正规,而且容易把源代码暴露。

实例1:工程项目只有源代码

工程文件目录结构:

仅仅包含一个jar文件;jar文件中只有一个简单的类。(啊?你刚才不是说你写了一个专门处理生物问题的软件吗?怎么只有一个类?我们从实例出发先来一个比较小的例子,或者你想想一个应用场景,这个软件只有你自己用。)

-ydl.test
        GraphicsDemo.class

MANIFEST.MF 文件的内容为:

Manifest-Version: 1.0
Class-Path: . 
Main-Class: ydl.test.GraphicsDemo

打开PowerShell,然后敲入如下代码:

javapackager -deploy -native exe -outdir nativeBuild -srcdir testDeploy1 -appclass ydl.test.GraphicsDemo -name testDep1 -outfile testDep1

当然记得将jdk的bin目录加入环境变量,注意是jdk,JRE只是jave的运行环境,不包含开发工具。然后运行,结果报如下错误:

没有基础 JDK。程序包将使用系统 JRE。
没有基础 JDK。程序包将使用系统 JRE。
检测到 [iscc.exe] 版本 0, 但需要版本 5。
由于配置问题, 跳过了打包程序EXE 安装程序: 找不到 InnoSetup 编译器 (iscc.exe)。
修复建议:   从 http://www.jrsoftware.org 下载 InnoSetup 5 或更高版本, 然后将其添加到 PATH。

那就去官网下载innoSetup5吧,注意这里有一个坑。那就是如果你下载的是6.0以上版本,javapackager命令打包的时候会显示找不到iscc.exe程序。我用的jdk版本是 8u 191。所以还是不要尝鲜的好,用稳定的5.6版本版。安装好后记得将安装路径放入环境变量。

同样运行代码,这时打包通过,直接生成了exe格式的安装包。

下面的问题就是怎么样使用"javapackager"这个JDK自带的软件了!!

2. 如何使用 javapackager

windows系统的官方说明文档:
https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javapackager.html

类Unix系统的官方说明文档:

https://docs.oracle.com/javase/8/docs/technotes/tools/unix/javapackager.html

3. 需求

现在我们需要的是类MAC OS 操作系统的打包。下面我暂时一一说明各个参数。
啊!生物信息其实就是这样大部分时间是在琢磨这个软件怎么用!

程序命令: javapackager

Performs tasks related to packaging and signing Java and JavaFX applications.
介绍这个命令的主要功能是打包Java撰写的软件。

Note: javapackager is not available on the Solaris platform.
注意这个打包工具在Solaris平台上不可用

Synopsis 总体使用方式

这个命令总的使用方式如下,他必须有一个 commandoptions 是可选的。推荐一篇文章
):这里有一份比较完整的打包命令行。

javapackager command [options]

command:
The task that should be performed.
用户想执行的任务。

options
One or more options for the command separated by spaces.
上面任务的详细参数,如果你不想用默认参数的话。

那么对于有哪些command可以使用呢?

command 英文解释 中文解释
-createbss Converts CSS files into binary form. 将css文件转换成二级制文件的格式。这个显然是给Java新出的GUI框架Javafx用的,因为Javafx支持css美化。
-createjar Produces a JAR archive according to other parameters. 生成jar文件。个人觉得这个可能不大会去用,因为一般的IDE都有功能直接生成可执行的jar文件。
-deploy Assembles the application package for redistribution. By default, the deploy task generates the base application package, but it can also generate a self-contained application package if requested. 部署,一般需要的核心功能,可以组装用于分发的安装包。默认情况下,这个部署任务产生基本的安装包,但是它也可以产生把所有需要东西都打包在一起的一个应用。
-makeall Performs compilation, createjar, and deploy steps as one call, with most arguments predefined, and attempts to generate all applicable self-contained application packages. The source files must be located in a folder called src, and the resulting files (JAR, JNLP, HTML, and self-contained application packages) are put in a folder called dist. This command can only be configured in a minimal way and is as automated as possible. 这个可谓是懒人必备,就是一个命令行,执行编译,生成可执行的Jar文件,再进行部署。但是我觉得这个容易导致除了问题之后,不知道哪一步出了问题。
-signjar Signs JAR file(s) with a provided certificate. 注册一个正规软件的签名吧,windows可能这个选项不会引起注意。以MAC OS为例,如果你的软件是要上架到app store的话,这个肯定是很需要的。

下面我们只讲我们所需要的 -deploy command.

4. Options for the deploy Command

刚才说了,javapackager 的总体使用方式是:

javapackager command [options]

下面介绍 -deploy对应的[options]

-allpermissions
If present, the application will require all security permissions in the JNLP file.
JNLP所需对于打包没什么用。

-appclass app-class
Qualified name of the application class to be executed.
就是一个Jar问价的启动类,对应于可执行文件中的启动类。这里需要用户显式的声明一下。

-argument arg
An unnamed argument to be inserted into an <fx:argument> element in the JNLP file.
JNLP所需,貌似是javafx需要的,我们暂时不用。

-Bbundler-argument=value
Provides information to the bundler that is used to package a self-contained application. See Arguments for Self-Contained Application Bundlers for information on the arguments for each bundler.
提供打成一个完整的包的所需信息。这个对于我们打包是个很重要的参数。注意它的使用方法,斜体是具体的option和值。后面的-native会指明是什么Bundler。

-callbacks
Specifies user callback methods in generated HTML. The format is the following:

"name1:value1,name2:value2,..."
HTML相关,我们不需要。

-description description
Description of the application.
软件的说明。

-embedCertificates
If present, the certificates will be embedded in the JNLP file.
JNLP相关的内置证书,暂时不需要。

-embedjnlp
If present, the JNLP file will be embedded in the HTML document.
不需要。

-height height
Height of the application.
软件的高度

-htmlparamfile file
Properties file with parameters for the resulting application when it is run in the browser.
不需要

-isExtension
If present, the srcfiles(后面的另一个参数) are treated as extensions.

-name name
Name of the application.
软件名

-native type
Generate self-contained application bundles (if possible). Use the -B option to provide arguments to the bundlers being used. If type is specified, then only a bundle of this type is created. If no type is specified, all is used.
产生一个所有东西都在一块的一个安装包。苹果的话,他有dmg或者pkg两种格式。这里它叫做bundle。如果你设置了 type,那么对应的bundle就会产生。不设置的话,所有能够生成的bundle都会产生。

The following values are valid for type:
下面是type所有设置的值:其实我们只要pkg或者dmg就行了。

all: Runs all of the installers for the platform on which it is running, and creates a disk image for the application. This value is used if type is not specified.

installer: Runs all of the installers for the platform on which it is running.

image: Creates a disk image for the application. On OS X, the image is the .app file. On Linux, the image is the directory that gets installed.

dmg: Generates a DMG file for OS X.

pkg: Generates a .pkg package for OS X.

mac.appStore: Generates a package for the Mac App Store.

rpm: Generates an RPM package for Linux.

deb: Generates a Debian package for Linux.

-nosign
If present, the bundle generated for self-contained applications is not signed by the bundler. The default for bundlers that support signing is to sign the bundle if signing keys are properly configured. This attribute is ignored by bundlers that do not support signing. At the time of the 8u40 release of the JDK, only OS X bundlers support signing.

-outdir dir
Name of the directory that will receive generated output files.
生成的安装包所在的目录。

-outfile filename
Name (without the extension) of the file that will be generated.
产生的html,JNLP等单个文件的文件名。对于我们的目标文件,如exe,msi,pkg,dmg来说这些不大重要。

-paramfile file
Properties file with default named application parameters.
如果软件能够传入参数,那么可以设置一个properties文件,然后把参数放里面

-preloader* preloader-class*
Qualified name of the JavaFX preloader class to be executed. Use this option only for JavaFX applications. Do not use for Java applications, including headless applications.
JavaFx相关

-srcdir dir
Base directory of the files to package.
你要打包的所有目录,这个会作为一个基本位置。一般对我们打包而言。我们可以用一个.,或者一个./dir

-srcfiles files
List of files in the directory specified by the -srcdir option. If omitted, all files in the directory (which is a mandatory argument in this case) will be used. Files in the list must be separated by spaces.
你要打包的目录,就是-srcdir dir里面的目录。

-templateId
Application ID of the application for template processing.

-templateInFilename
Name of the HTML template file. Placeholders are in the following form:

-templateOutFilename
Name of the HTML file that will be generated from the template.

-title title
Title of the application.
标题

-vendor vendor
Vendor of the application.
vendor就是一个实现者,请看这一篇https://stackoverflow.com/questions/17428081/what-is-a-vendor-when-talking-about-java
我想开发的机构名称可以写在这里。

-width width
Width of the application.

-updatemode update-mode
Sets the update mode for the JNLP file.

Arguments for Self-Contained Application Bundlers

还是看官网吧,那边写的很全。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,391评论 0 13
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,608评论 0 3
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa阅读 8,816评论 0 6
  • 个人笔记,方便自己查阅使用 Contents Java LangAssignment, ReferenceData...
    freenik阅读 1,311评论 0 6
  • 晚上我和妈妈定好要去晨练。听到这个消息,我兴奋不已。因为我从来没有在家里面还晨练过,甚至睡觉都在兴奋中。 ...
    裴丽云阅读 440评论 1 2