Using com.sun.crypto.provider.SunJCE in OSGi Bundle

Go To


I am trying to use com.sun.crypto.provider.SunJCE (for HMAC) in an OSGi Bundle.

I am using the maven-bundle-plugin with these configs:


When i execute my bundle I get this error:

Caused by: java.lang.ClassNotFoundException: com.sun.crypto.provider.SunJCE

How can I get the code in my OSGi bundle to have access to com.sun.crypto.provider?

2012-04-04 03:41
by empire29


Looking at your maven config, one thing which jumps out is that making all your package imports optional is unwise. It undoes many of the benefits OSGi gives you by deferring problems to execution time, instead of letting you know at bundle start time that something isn't right. Fail slow instead of fail fast, in other words! It's only appropriate for dependencies which are genuinely optional.

Of course, your ultimate aim is to not fail at all, rather than just failing faster. As a starting point to understanding what's happening, have you opened up your built bundle and checked that the crypto packages are actually being embedded? It's a good idea to check your build is doing what you hope it is.

However, in this particular case, since this is a class which you would normally expect the JVM to provide, I'd suggest checking your boot classpath to ensure it's available, dropping the embed dependency, and using the org.osgi.framework.system.packages.extra property instead of embedding the dependency.

2012-04-04 04:28
by Holly Cummins


Why do you need the com.sun.crypto class? In general, you can access the crypto functions through the java.* API? E.g.:

 KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
 SecretKey key = keyGen.generateKey();
 Mac mac = Mac.getInstance(key.getAlgorithm());
 String str = "This message will be digested";
 byte[] utf8 = str.getBytes("UTF8");
 byte[] digest = mac.doFinal(utf8);
2013-04-05 13:07
by Peter Kriens