Thursday 28 January 2021

Java8: Compact Profiles

Java8 provides a feature that enables Java Applications to use subset of the APIs provided by entire JDK. This feature is called compact profiles. Using compact profiles, we can deploy the application with the subset APIs instead of entire JDK APIs.

 

Why Compact Profiles?

Prior to Java8, to deploy a Java application, we need to deploy entire Java APIs also. With compact profiles, we can reduce memory footprint by deploying only subset profile. Compact profiles are interim solution to Java9 modularity system (Project Jigsaw).

 

Java8 defines 3 compact profiles (compact1, compact2 and compact3). Each profile is a subset of its predecessor. For example, compact2 contains all the copact1 packages plus some new packages.

 


Following table summarizes the compact profiles and their comprising packages.

 

compact1

compact2

compact3

java.io

 

java.lang

 

java.lang.annotation

 

java.lang.invoke

 

java.lang.ref

 

java.lang.reflect

 

java.math

 

java.net

 

java.nio

 

java.nio.channels

 

java.nio.channels.spi

 

java.nio.charset

 

java.nio.charset.spi

 

java.nio.file

 

java.nio.file.attribute

 

java.nio.file.spi

 

java.security

 

java.security.cert

 

java.security.interfaces

 

java.security.spec

 

java.text

 

java.text.spi

 

java.time

 

java.time.chrono

 

java.time.format

 

java.time.temporal

 

java.time.zone

 

java.util

 

java.util.concurrent

 

java.util.concurrent.atomic

 

java.util.concurrent.locks

 

java.util.function

 

java.util.jar

 

java.util.logging

 

java.util.regex

 

java.util.spi

 

java.util.stream

 

java.util.zip

 

javax.net

 

javax.net.ssl

 

javax.script

 

javax.security.auth

 

javax.security.auth.callback

 

javax.security.auth.login

 

javax.security.auth.spi

 

javax.security.auth.x500

 

javax.security.cert

java.rmi

 

java.rmi.activation

 

java.rmi.dgc

 

java.rmi.registry

 

java.rmi.server

 

java.sql

 

javax.rmi.ssl

 

javax.sql

 

javax.transaction

 

javax.transaction.xa

 

javax.xml

 

javax.xml.datatype

 

javax.xml.namespace

 

javax.xml.parsers

 

javax.xml.stream

 

javax.xml.stream.events

 

javax.xml.stream.util

 

javax.xml.transform

 

javax.xml.transform.dom

 

javax.xml.transform.sax

 

javax.xml.transform.stax

 

javax.xml.transform.stream

 

javax.xml.validation

 

javax.xml.xpath

 

org.w3c.dom

 

org.w3c.dom.bootstrap

 

org.w3c.dom.events

 

org.w3c.dom.ls

 

org.w3c.dom.views

 

org.xml.sax

 

org.xml.sax.ext

 

org.xml.sax.helpers

java.lang.instrument

 

java.lang.management

 

java.security.acl

 

java.util.prefs

 

javax.annotation.processing

 

javax.lang.model

 

javax.lang.model.element

 

javax.lang.model.type

 

javax.lang.model.util

 

javax.management

 

javax.management.loading

 

javax.management.modelmbean

 

javax.management.monitor

 

javax.management.openmbean

 

javax.management.relation

 

javax.management.remote

 

javax.management.remote.rmi

 

javax.management.timer

 

javax.naming

 

javax.naming.directory

 

javax.naming.event

 

javax.naming.ldap

 

javax.naming.spi

 

javax.security.auth.kerberos

 

javax.security.sasl

 

javax.sql.rowset

 

javax.sql.rowset.serial

 

javax.sql.rowset.spi

 

javax.tools

 

javax.xml.crypto

 

javax.xml.crypto.dom

 

javax.xml.crypto.dsig

 

javax.xml.crypto.dsig.dom

 

javax.xml.crypto.dsig.keyinfo

 

javax.xml.crypto.dsig.spec

 

org.ietf.jgss

 

How to compile a class using given compact profile?

Syntax

javac -profile {compact1|compact2|compact3} JavaClass

 

Example

javac -profile compact1 App.java

 

App.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class App {

	public static void main(String args[]) throws IOException {

		try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));) {
			System.out.println("Enter your name");
			String name = br.readLine();

			System.out.println("Hello " + name);
		}

	}
}


Since java.io package is part of compact1 profile, we can use this profile to compile App.java. Execute the command ‘javac -profile compact1 App.java’ to compile App.java using compact1 profile.

$javac -profile compact1 App.java
$ls
App.class	App.java


How to list dependencies in a class file?

Use jdeps tool to list the dependencies in a class file.

 

jdeps App.class

 

This command generate below output.

$jdeps App.class 
App.class -> /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home/jre/lib/rt.jar
   <unnamed> (App.class)
      -> java.io                                            
      -> java.lang


You can even get more verbose output using -verbose option.

$jdeps -verbose -P App.class
App.class -> /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home/jre/lib/rt.jar (compact1)
   App                                                -> java.io.BufferedReader                             compact1
   App                                                -> java.io.IOException                                compact1
   App                                                -> java.io.InputStream                                compact1
   App                                                -> java.io.InputStreamReader                          compact1
   App                                                -> java.io.PrintStream                                compact1
   App                                                -> java.io.Reader                                     compact1
   App                                                -> java.lang.Object                                   compact1
   App                                                -> java.lang.String                                   compact1
   App                                                -> java.lang.StringBuilder                            compact1
   App                                                -> java.lang.System                                   compact1
   App                                                -> java.lang.Throwable                                compact1


Create smaller Java Platform using jrecreate tool

Download ‘Oracle Java SE Embedded version 8’ from below link.

https://www.oracle.com/java/technologies/java-embedded-java-se-downloads.html

 

Extract the downloaded zip file. You will see ‘ejdk1.8.0/’ folder after extraction.

 

Go to ‘bin’ directory of ejdk1.8.0 folder, you will see jrecreate.sh file.

$ls
jrecreate.bat        jrecreate.config.properties   jrecreate.sh


Execute following command to generate jre with compact1 profile.

 

jrecreate.sh -d ./compact1-jre/ -p compact1

 

Above command generates a folder ‘compact1-jre’ in current directory.

$jrecreate.sh -d ./compact1-jre/ -p compact1
Building JRE using Options {
    ejdk-home: /Users/krishna/Downloads/ejdk1.8.0
    dest: /Users/krishna/Documents/technical-documents/programs/./compact1-jre
    target: linux_arm_vfp_hflt
    vm: minimal
    runtime: compact1 profile
    debug: false
    keep-debug-info: false
    no-compression: false
    dry-run: false
    verbose: false
    extension: []
}


Target JRE Size is 10,513 KB (on disk usage may be greater).

Embedded JRE created successfully

 

Now you can use compact1-jre to ship App.class file. Hopw you enjoyed this tutorial.




Previous                                                    Next                                                    Home

1 comment:

  1. superb explanation, its really worth to get knowledge from this.

    ReplyDelete