UnsatisfiedLinkError exception while working with dll and java jni4net

早过忘川 提交于 2019-12-10 14:31:50

问题


I have been working on reading c#(dll) function from java through jni4net and in core java I have succeeded in getting the value from the dll function but now I have created one Dynamic Web Project and tried to use same functionality in servlet. But now only the dll file is loaded succesfully, the function is not called succesfuly. Below is what I tried till now:

My Servlet:

public class LoginProcess extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {

            Bridge.setVerbose(true);
            Bridge.init();
            Console.WriteLine("Hello .NET world!\n");

            Bridge.LoadAndRegisterAssemblyFrom(new File("C:/Users/ashish.it/workspace/FinalJniWeb/WebContent/WEB-INF/lib/ADHelper.j4n.dll"));

        } catch (IOException e) {
            e.printStackTrace();
        }
        Enum output;
        output=ADHelper.Login("user", "pass");
        System.out.println(output);
    }
}

The only difference when I made this in core java was that I didn't used complete path instead I used only "lib/ADHelper.j4n.dll" for path but somehow it was not working in servlet so I changed it to complete path. Anyways the dll file is loaded succesfully.

ADHelper.generated.cs

namespace ADHelper {
    public partial class ADHelper_ {

methods.Add(global::net.sf.jni4net.jni.JNINativeMethod.Create(@__type, "Login", "Login2", "(Ljava/lang/String;Ljava/lang/String;)Lsystem/Enum;"));

  private static global::net.sf.jni4net.utils.JniHandle Login2(global::System.IntPtr @__envp, global::net.sf.jni4net.utils.JniLocalHandle @__class, global::net.sf.jni4net.utils.JniLocalHandle UserName, global::net.sf.jni4net.utils.JniLocalHandle Password) {
            // (Ljava/lang/String;Ljava/lang/String;)Lsystem/Enum;
            // (LSystem/String;LSystem/String;)LADHelper/ADHelper+LoginResult;
            global::net.sf.jni4net.jni.JNIEnv @__env = global::net.sf.jni4net.jni.JNIEnv.Wrap(@__envp);
            global::net.sf.jni4net.utils.JniHandle @__return = default(global::net.sf.jni4net.utils.JniHandle);
            try {
            @__return = global::net.sf.jni4net.utils.Convertor.StrongC2Jp<global::ADHelper.ADHelper.LoginResult>(@__env, global::ADHelper.ADHelper.Login(global::net.sf.jni4net.utils.Convertor.StrongJ2CString(@__env, UserName), global::net.sf.jni4net.utils.Convertor.StrongJ2CString(@__env, Password)));
            }catch (global::System.Exception __ex){@__env.ThrowExisting(__ex);}
            return @__return;
        }
}

The underdscore was mingled to the name ADHelper class when I ran proxygen command. in dll file there are two classes named ADHelper and ADHelper

The function Login() was also changed to Login2() but Login2() is not recognized by my servlet whereas Login() is recognized.

Generated Java class ADHelper.java

package adhelper;

@net.sf.jni4net.attributes.ClrType
public class ADHelper extends system.Object { 

private static system.Type staticType;

    protected ADHelper(net.sf.jni4net.inj.INJEnv __env, long __handle) {
            super(__env, __handle);
    }

    @net.sf.jni4net.attributes.ClrConstructor("()V")
    public ADHelper() {
            super(((net.sf.jni4net.inj.INJEnv)(null)), 0);
        adhelper.ADHelper.__ctorADHelper0(this);
    }

@net.sf.jni4net.attributes.ClrMethod("(LSystem/String;LSystem/String;)LADHelper/ADHelper+LoginResult;")
    public native static system.Enum Login(java.lang.String UserName, java.lang.String Password);

 public static system.Type typeof() {
        return adhelper.ADHelper.staticType;
    }

 private static void InitJNI(net.sf.jni4net.inj.INJEnv env, system.Type staticType) {
        adhelper.ADHelper.staticType = staticType;
    }
}

All the mapping is correct but my Login function is giving unsatisfiedLinkError. Thanks for having patience while reading, please give solution to my problem.

Following error is coming on console:

*All Dll file loaded message*
Jun 3, 2015 10:56:39 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet LoginProcess threw exception
java.lang.UnsatisfiedLinkError: adhelper.ADHelper.Login(Ljava/lang/String;Ljava/lang/String;)Lsystem/Enum;
    at adhelper.ADHelper.Login(Native Method)
    at com.karvy.login.LoginProcess.doGet(LoginProcess.java:62)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:662)

回答1:


Maybe you can test for class-loader loading your file from different class-loaders in different context.




回答2:


I considered it to be the classloader problem which it was not when I later suppressed my files to load from same classloader.

Further examination shown that the path I was giving was direct path to the file in the directory, and was not pointing to the same direction as of file in servlet context scope. For that I had to create servlet context object and after that getResuorce method was called so that I can get my file up and ready to be used.

Here is code to get file from servlet context scope:

ServletContext context = getServletContext();
String realPath = context.getRealPath("/WEB-INF/lib/ADHelper.j4n.dll");
File file = new File(realPath);

Thank you @surabhi-rai for your support. I have posted this asnwer for if in future anyone gets this problem can come here. Thank you and welcome. ☻




回答3:


Get your file reference through object of servlet context and then calling getResource method on it.

Right now you are pointing to local dir which is not in servlet context scope.



来源:https://stackoverflow.com/questions/30612127/unsatisfiedlinkerror-exception-while-working-with-dll-and-java-jni4net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!