java8有个很好的特性,即JEP161(http://openjdk.java.net/jeps/161 ),该特性定义了Java SE平台规范的一些子集,使java应用程序不需要整个JRE平台即可部署和运行在小型设备上。开发人员可以基于目标硬件的可用资源选择一个合适的JRE运行环境。
好处
1.更小的Java环境需要更少的计算资源。
2.一个较小的运行时环境可以更好的优化性能和启动时间。
3.消除未使用的代码从安全的角度总是好的。
4.这些打包的应用程序可以下载速度更快。
概念
紧凑的JRE分3种,分别是compact1、compact2、compact3,他们的关系是compact1<compact2<compact3,他们包含的API如下图所示
使用javac根据profile编译应用程序
javac –bootclasspath, or javac –profile <profile>
如果不符合compact的api,则报错。
$ javac -profile compact2 Test.java
Test.java:7: error: ThreadMXBean is not available in profile 'compact2'
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
^
Test.java:7: error: ManagementFactory is not available in profile 'compact2'
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
^
2 errors
使用工具开发的效果
JPEDS工具使用
java8新增一个工具,用来分析应用程序所依赖的profile,有三个参数比较常用 -p,-v,-r
import java.util.Set;
import java.util.HashSet;
public class Deps {
public static void main(String[] args) {
System.out.println(Math.random());
Set<String> set = new HashSet<>();
}
}
************** PROFILE ********************
jdeps -P Deps.class
Deps.class -> /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/rt.jar
<unnamed> (Deps.class)
-> java.io compact1
-> java.lang compact1
-> java.util compact1
************** VERBOSE ********************
jdeps -v Deps.class
Deps.class -> /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/rt.jar
Deps (Deps.class)
-> java.io.PrintStream
-> java.lang.Math
-> java.lang.Object
-> java.lang.String
-> java.lang.System
-> java.util.HashSet
************** RECURSIVE ********************
jdeps -R Deps.class
Deps.class -> /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/rt.jar
<unnamed> (Deps.class)
-> java.io
-> java.lang
-> java.util
/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/jce.jar -> /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/rt.jar
javax.crypto (jce.jar)
-> java.io
-> java.lang
-> java.lang.reflect
-> java.net
-> java.nio
-> java.security
-> java.security.cert
-> java.security.spec
-> java.util
-> java.util.concurrent
-> java.util.jar
-> java.util.regex
-> java.util.zip
-> javax.security.auth
-> sun.security.jca JDK internal API (rt.jar)
-> sun.security.util JDK internal API (rt.jar)
-> sun.security.validator JDK internal API (rt.jar)
javax.crypto.interfaces (jce.jar)
-> java.lang
-> java.math
-> java.security
javax.crypto.spec (jce.jar)
-> java.lang
-> java.math
-> java.security.spec
-> java.util
/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/rt.jar -> /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/jce.jar
java.security (rt.jar)
-> javax.crypto JDK internal API (jce.jar)
sun.security.util (rt.jar)
-> javax.crypto JDK internal API (jce.jar)
-> javax.crypto.interfaces JDK internal API (jce.jar)
-> javax.crypto.spec JDK internal API (jce.jar)
在linux上构建profile
$ hg clone http://hg.openjdk.java.net/jdk8/jdk8/
$ cd jdk8
$ make images profiles :
## Finished profiles (build time 00:00:27)
----- Build times -------
Start 2013-03-17 14:47:35
End 2013-03-17 14:58:26
00:00:25 corba
00:00:15 demos
00:01:50 hotspot
00:00:24 images
00:00:21 jaxp
00:00:31 jaxws
00:05:37 jdk
00:00:43 langtools
00:00:18 nashorn
00:00:27 profiles
00:10:51 TOTAL
-------------------------
Finished building Java(TM) for target 'images profiles'
$ cd images
$ ls -d *image
j2re-compact1-image j2re-compact2-image j2re-compact3-image j2re-image j2sdk-image