Home Unable to get a native library (.so) to execute in grails 3
Reply: 1

Unable to get a native library (.so) to execute in grails 3

Saqib Ahmed
1#
Saqib Ahmed Published in 2018-01-02 13:14:57Z

I'm using a native library in my java code which is working perfectly through and through. When I use the same library in grails 3 application, it gets loaded fine, but when I call a native method, it throws UnsatisfiedLinkError exception.

I'm using following code in both cases:

try{
    System.loadLibrary("TrippleDes")
    String plainText = "passw0rd.!";
    String cipher = JniWrapper.encrypt(plainText);

    String orgStr = JniWrapper.decrypt(cipher);
    System.out.println("Original text: " + plainText);
    System.out.println("Cipher text: " + cipher);
    System.out.println("Restored text: " + orgStr);
}catch (Exception ex){
    System.out.println(ex.getMessage());
}

For simple java code, it works fine with the relevant output. For same code in grails, it throws following exception. I tried to call the native function directly from groovy class as well as wrapping it up in a java class but to no avail. Stacktrace doesn't unveil any substantial debugging probes, but for completeness, here goes:

java.lang.UnsatisfiedLinkError: com.ef.apps.licensing.JniWrapper.encrypt(Ljava/lang/String;)Ljava/lang/String;
    at com.ef.apps.licensing.JniWrapper.encrypt(Native Method)
    at com.ef.apps.licensing.licCheck.check(licCheck.java:7)
    at com.ef.apps.licensing.licCheck$check.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
    at umm1.BootStrap$_closure1.doCall(BootStrap.groovy:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1426)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1215)
    at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1125)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1089)
    at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1125)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
    at groovy.lang.Closure.call(Closure.java:414)
    at groovy.lang.Closure.call(Closure.java:408)
    at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:516)
    at grails.util.Environment.executeForEnvironment(Environment.java:509)
    at grails.util.Environment.executeForCurrentEnvironment(Environment.java:485)
    at org.grails.web.servlet.boostrap.DefaultGrailsBootstrapClass.callInit(DefaultGrailsBootstrapClass.java:62)
    at org.grails.web.servlet.context.GrailsConfigUtils.executeGrailsBootstraps(GrailsConfigUtils.java:65)
    at org.grails.plugins.web.servlet.context.BootStrapClassRunner.onStartup(BootStrapClassRunner.groovy:53)
    at grails.boot.config.GrailsApplicationPostProcessor.onApplicationEvent(GrailsApplicationPostProcessor.groovy:256)
    at grails.boot.config.GrailsApplicationPostProcessor.onApplicationEvent(GrailsApplicationPostProcessor.groovy)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:166)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:382)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:336)
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:877)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:79)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:381)
    at grails.boot.GrailsApp.run(GrailsApp.groovy:370)
    at grails.boot.GrailsApp$run.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
    at umm1.Application.main(Application.groovy:8)

What am I missing here?

Operating System: Ubuntu 16.04 with Linux 4.4.0-104-generic #127-Ubuntu x86_64 x86_64 x86_64 GNU/Linux

Grails Version: 3.2.2

JDK Version:

  • openjdk version "1.8.0_151"
  • OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)
  • OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)
Ejaz Ahmed
2#
Ejaz Ahmed Reply to 2018-01-02 16:01:43Z

Groovy uses different class loader than java. You'll have to workout in following direction: 1. When you load library, explicitly tell which loader to use. 2. Fork JVM in Grails so that it uses loader which has path to that library set.

Here is similar problem foe Grails2 UnsatisfiedLinkError when using a JNI native library from Grails application

I'd love to know which path you took and how do you resolve in Grails3.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.335004 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO