简体   繁体   English

在Xpages中从服务器端javascript / java调用C代码?

[英]Calling C code from server-side javascript/java in Xpages?

I'm trying to call some third-party C code from an Xpage in IBM Domino. 我试图从IBM Domino中的Xpage调用一些第三方C代码。 I have a button that has an eventHandler whose action creates a Java object and then tries to execute a method, which simply tries to load and run a simple test C function (via JNA) in a DLL. 我有一个按钮,它有一个eventHandler,它的动作创建一个Java对象,然后尝试执行一个方法,它只是尝试在DLL中加载并运行一个简单的测试C函数(通过JNA)。 (This is running under Windows 7.) Here is the Java code: (这是在Windows 7下运行。)这是Java代码:

interface CLibrary extends StdCallLibrary {       
      int hello();
}

public class MyHelloWorld {

    public void testImage() {
        CLibrary INSTANCE2 = (CLibrary) Native.loadLibrary("helloWorld", CLibrary.class);
        INSTANCE2.hello();

And here is the C code: 这是C代码:

#include <stdio.h>

int
hello()
{
  printf ("Hello World!\n");
}

I've compiled the C code to a dll, and I've imported the .dll as a Resources->Files in Designer. 我已将C代码编译为dll,并且我已将.dll作为资源 - >文件导入设计器中。 I've also put the jna.jar file in the ext directory on the server. 我还将jna.jar文件放在服务器上的ext目录中。 When I try to run all of this, I get a 500 error. 当我尝试运行所有这些时,我得到500错误。 There's a lot going on here, so I'm not sure what the problem is. 这里有很多事情,所以我不确定问题是什么。 Logfiles don't reveal much. 日志文件没有透露太多。 Has anyone done this before? 有没有人这样做过? To summarize, I want to be able go from XPage->Java->C. 总而言之,我希望能够从XPage-> Java-> C。 Thanks! 谢谢!

coincidentally I just did this last night with Xpages, JNA and nnotes.dll. 巧合的是,我昨晚用Xpages,JNA和nnotes.dll做了这个。

I suspect that your library is not being loaded in the loadLibrary stage. 我怀疑您的库未在loadLibrary阶段加载。

nnotes.dll is able to be loaded from xpages without any extra setup, presumably because it is on the system path, however in your case you are supplying your own dll, and I am not totally sure that your dll will be found in the Resources - Files section during loadLibrary() stage. nnotes.dll能够从xpages加载而无需任何额外的设置,大概是因为它在系统路径上,但是在你的情况下你提供自己的dll,我不完全确定你的dll会在参考资料中找到 - loadLibrary()阶段的文件部分。

If you haven't read this article by Jesper Kiaer http://nevermind.dk/nevermind/blog.nsf/subject/calling-the-domino-c-api-from-an-xpage-or-a-java-agent Read that article, and then try out his example of NSFDbSpaceUsage, but do it in an XPage. 如果你还没有读过Jesper Kiaer的这篇文章http://nevermind.dk/nevermind/blog.nsf/subject/calling-the-domino-c-api-from-an-xpage-or-a-java-agent阅读那篇文章,然后尝试他的NSFDbSpaceUsage示例,但是在XPage中完成。 (he does it in a Java agent but just do in an xpage instead). (他在Java代理中完成,但只是在xpage中执行)。 that way you know JNA will be working with XPages ok. 那样你知道JNA将使用XPages好。

Once that is working, then can I suggest trying out JNA + your custom dll in a regular Java project, without any Notes/Domino involvement, just to verify that it works in isolation? 一旦它工作,那么我可以建议在常规Java项目中尝试JNA +您的自定义DLL,而不需要任何Notes / Domino参与,只是为了验证它是否独立工作? You can set the property jna.library.path to add the custom location of the dll. 您可以设置属性jna.library.path以添加dll的自定义位置。 If you also set the property jna.debug_load=true you can see in the console which paths that JNA is looking in for your dll, by default it is the system paths and some other convention to do with classpath and platform. 如果你还设置了属性jna.debug_load=true你可以在控制台中看到JNA正在为你的dll查找哪些路径,默认情况下它是系统路径以及与类路径和平台有关的其他约定。 If it can't find it you will get an UnsatisfiedLinkError, so keep playing around with the library path until you can load your dll. 如果找不到它,你将得到一个UnsatisfiedLinkError,所以继续玩库路径,直到你可以加载你的DLL。 Then at least you will know there is nothing wrong with your dll and JNA. 那么至少你会知道你的dll和JNA没有任何问题。 (You may even be able to set those properties dynamically in your xpage if you don't want to do the isolated plain java application test.) (如果您不想进行单独的普通Java应用程序测试,您甚至可以在xpage中动态设置这些属性。)

Then last step is to play around with the location of the dll to get it loading in your xpages. 然后最后一步是使用dll的位置来加载它在xpages中。 Try using jna.library.path, try the standard locations, read up on the loadLibrary method to find out where the default locations are. 尝试使用jna.library.path,尝试标准位置,阅读loadLibrary方法以找出默认位置的位置。

I don't know if this is an option for you, but I called C code from ssjs by calling a lotusscript agent that executed the actual c function. 我不知道这是否适合您,但是我通过调用执行实际c函数的lotusscript代理来调用ssjs中的C代码。 That worked like a charm. 这就像一个魅力。 A big difference from your scenario is I was calling domino specific C, not a custom dll. 与你的场景有很大不同的是我在调用domino特定的C,而不是自定义dll。

You should check our Java's Native interface: 您应该检查我们的Java的Native接口:

 The Java Native Interface (JNI) is a programming framework that enables Java code running 
 in a Java Virtual Machine (JVM) to call and to be called by native applications (programs 
 specific to a hardware and operating system platform) and libraries written in other 
 languages such as C, C++ and assembly.

You can find out more about JNI here . 您可以在此处找到有关JNI的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Java应用程序禁用MongoDB上的服务器端JavaScript - How to disable server-side JavaScript on MongoDB from Java Application 需要在HTML + JavaScript的客户端和Java / C ++的服务器端程序之间进行通信 - Need to communicate between Client-side in HTML+JavaScript and Server-side program in Java/C++ 服务器端(Java)上的HTML页面内是否可能调用JavaScript代码? - It is a possible call JavaScript code inside HTML page on server-side (Java)? Java中的脚本编制-Java 1.5中服务器端类文件中的javascript - scripting in java - javascript from a server-side class file in Java 1.5 从服务器端Java代码调用一个方法到一个IOS APP并接收其同步响应 - invoking a method from Server-side Java code to an IOS APP and receiving its synchronous response Cloudfront Signed Cookies在java / javascript中完整的服务器端实现 - Cloudfront Signed Cookies full server-side implementation in java/javascript 我们如何在Firebase中添加服务器端Java代码? - How can we add a server-side Java code in Firebase? Web浏览器上Java,ReactJs代码的服务器端渲染 - Server-side rendering of Java, ReactJs code on a web browser 如何在 Java 中的服务器端代码上启用 CORS - How to enable CORS on server-side code in java 服务器端Java错误 - Server-side Java error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM