我一直在从
java到jni4net读取c#(dll)函数,在核心java中我已成功从dll函数中获取值,但现在我创建了一个Dynamic Web Project并尝试在servlet中使用相同的功能.但是现在只有dll文件成功加载,该函数不会被称为succesfuly.以下是我到现在为止所做的尝试:
我的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); } }
我在核心java中做到这一点的唯一区别是我没有使用完整路径,而是仅使用“lib / ADHelper.j4n.dll”作为路径但不知何故它在servlet中不起作用所以我将其更改为完成路径.无论如何,dll文件成功加载.
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; } }
当我运行proxygen命令时,underdscore被混合到名称ADHelper类.在dll文件中有两个名为ADHelper和ADHelper的类
函数Login()也更改为Login2()但我的servlet无法识别Login2(),而Login()被识别.
生成的Java类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; } }
所有映射都是正确的,但我的Login函数给出了unfisfiedLinkError.感谢您在阅读时有耐心,请解决我的问题.
控制台出现以下错误:
*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)
也许你可以测试class-loader在不同的上下文中从不同的类加载器加载你的文件.
原文链接:https://www.f2er.com/windows/441497.html