Billy Liu
Billy Liu

Reputation: 126

How to use log4j in Embedded JDK compact 1/2/3

I try to port existing Java project into embedded environment. But I found log4j-1.2.14 could not work under such environment. Since it has dependency with Beans.

I build the EJDK8 compact 2 environment. More information about EJDK8: http://www.oracle.com/technetwork/java/embedded/resources/tech/compact-profiles-overview-2157132.html

When start the project, it has the following exceptions.

Exception in thread "main" java.lang.NoClassDefFoundError: java/beans/IntrospectionException [java] at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:649) [java] at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:612) [java] at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:509) [java] at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:415) [java] at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:441) [java] at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:470) [java] at org.apache.log4j.LogManager.(LogManager.java:122) [java] at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:66) [java] at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:270) [java] at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:281) [java] at com.truviso.system.Application.(Application.java:36) [java] Caused by: java.lang.ClassNotFoundException: java.beans.IntrospectionException [java] at java.net.URLClassLoader$1.run(Unknown Source) [java] at java.net.URLClassLoader$1.run(Unknown Source) [java] at java.security.AccessController.doPrivileged(Native Method) [java] at java.net.URLClassLoader.findClass(Unknown Source) [java] at java.lang.ClassLoader.loadClass(Unknown Source) [java] at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) [java] at java.lang.ClassLoader.loadClass(Unknown Source) [java] ... 11 more

After reading log4j source code, I found there are many dependencies with java.beans.*. The the beans namespace is not supported by EJDK. In this case, what kinds logging framework we could use? The log4j is widely used in many libraries. Do we have some other choices?

Upvotes: 3

Views: 1504

Answers (3)

Cleiton Bueno
Cleiton Bueno

Reputation: 419

The jrecreate build tool based on some profile to provide the jre to use in your application. You can select the smallest footprint as compact1, compact2 and compact3 and test your application. But sometimes some applications need to access more resources, the options above are:

--vm server or all being size much higher.

My case resolved with --vm server, dependency using java/beans/IntrospectionException.

Solution alternative:

$ cd ejdk1.8.0_51
$ export JAVA_HOME=linux_i586/jre
$ ./bin/jrecreate.sh --vm server --dest version-server-jre8

Building JRE using Options {
ejdk-home: /home/bueno/Downloads/ejdk1.8.0_51
    dest: /home/bueno/Downloads/ejdk1.8.0_51/version-server-jre8
    target: linux_i586
    vm: server
    runtime: jre
    debug: false
    keep-debug-info: false
    no-compression: false
    dry-run: false
    verbose: false
    extension: []
}

Target JRE Size is 49.379 KB (on disk usage may be greater).
Embedded JRE created successfully

More information: docs.oracle.com/javase/8/embedded/develop-apps-platforms/

Upvotes: 2

Guz
Guz

Reputation: 307

This way seems to work:

Create a new "full profile" with jrecreate (defaults to making a full profile when no parameter is given.)

Copy java/bean and com/sun/beans from the full/lib/rt.jar to your compact profile /lib/rt.jar

edit the /lib/meta-index file and add the line "/com/sun/beans" (again, you can have a look at the "full" meta-index file and copy the line)

Upvotes: 1

Stephen C
Stephen C

Reputation: 719307

In this case, what kinds logging framework we could use? The log4j is widely used in many libraries. Do we have some other choices?

Well, java.util.logging is in the compact1 profile, so that is an obvious candidate.

But if you are using a library that depends directly on log4j, then you are kind of stuck. You either need to "butcher" log4j (bad idea), or you need to change the library's logging framework. If you do the latter, consider changing it to slf4j ... which will allow you to use a variety of "back end" logging frameworks, depending on your application or platform requirements.

Upvotes: 3

Related Questions