Rob Anderson
Rob Anderson

Reputation: 2407

OSGi (Felix) on Android: NoClassDefFoundError: sun/reflect/ConstructorAccessorImpl

I try to run a bundle that obviously uses sun.reflect package.

First some more backgroundinformation: I'm using Apache Felix on Android. And i also added sun.reflect as a system extra package.

String extrapackets =  "sun.reflect; version=\"1.0.0\"";

config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, extrapackets);

config.put("felix.bootdelegation.implicit", "false");

"sun.reflect" is also shown in the exportinglist of the system bundle (using headers cmd). And it is imported by my bundle. It also got correct versionnumbers at the export and importing stuff.

Errormsg:

java.lang.NoClassDefFoundError: sun/reflect/ConstructorAccessorImpl
at sun.misc.Unsafe.defineClass(Native Method)
at sun.reflect.ClassDefiner.defineClass(ClassDefiner.java:45)
at sun.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:381)
at java.security.AccessController.doPrivileged(Native Method)
at sun.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:377)
at sun.reflect.MethodAccessorGenerator.generateConstructor(MethodAccessorGenerator.java:76)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:30)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
...

Upvotes: 3

Views: 2031

Answers (3)

sigpwned
sigpwned

Reputation: 7443

In case anyone else runs across this issue, there may be a quicker fix.

I hit this bug when using reflection in OSGi running on a Sun/Oracle JVM. It turns out this is a known issue. The suggested fix of setting -Dsun.reflect.noInflation=true at the JVM level will cause the class not to be referenced, at the cost of ignoring an optimization.

I tried the fix, and it worked for me.

Upvotes: 1

Frank Lee
Frank Lee

Reputation: 2748

Did you also import the package in your bundle? Just adding it to your systembundles.extra is not enough.

Upvotes: 0

Kartik Sankaran
Kartik Sankaran

Reputation: 290

As far as I know, the sun.* packages are internal packages used in the Oracle JVM you install on your computer. Android, on the other hand, does not use Sun's (now Oracle's) implementation of the JVM. Android offers an API similar to Java SE, but has its own implementation of the VM (called Dalvik VM).

So, you won't find the sun.* packages on Android. You need to get the source code of the bundle you using, and compile it against the Android libraries (using an Android project in eclipse, or using Ant). Then, repackage it into a jar, and then you can deploy it without it referencing the sun.* packages.

Kartik

Upvotes: 0

Related Questions