简体   繁体   English

Java Project,包含适用于不同平台的本机库

[英]Java Project with native libraries for different platforms

I have a java project that uses JOGL and therefore needs to include different native libraries for different platforms. 我有一个使用JOGL的java项目,因此需要为不同的平台包含不同的本机库。 Now I want to set up the eclipse project to automatically chose the correct libraries. 现在我想设置eclipse项目以自动选择正确的库。 In the .classpath file you can specify one native location: .classpath文件中,您可以指定一个本机位置:

<classpathentry kind="lib" path="lib/jogl/jar/jogl.all.jar">
    <attributes>
        <attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="de.yogularm.desktop/lib/jogl/windows-amd64"/>
    </attributes>
</classpathentry>

If multiply classpathentries specify a native location, Eclipse also tries to load the libraries for other platforms and fails. 如果multiply classpathentries指定本机位置,Eclipse还会尝试为其他平台加载库并失败。

How can I make eclipse chose the directory? 我怎么能让eclipse选择目录? I want to share the project and make setup for further developers as easy as possible. 我希望尽可能简单地共享项目并为其他开发人员进行设置。

You can create several .classpath files (.classpath_Windows, .classpath_Linux etc.) and then create symbolic link to required version (Win/Lin/Mac) named ".classpath" 您可以创建多个.classpath文件(.classpath_Windows,.classpath_Linux等),然后创建指向名为“.classpath”的所需版本(Win / Lin / Mac)的符号链接
The problem will appear whan you want to change any entry inside any .classpath_XXXXXXX - then you will have to update other .classpath files. 如果您想更改任何.classpath_XXXXXXX中的任何条目,则会出现问题 - 然后您必须更新其他.classpath文件。

Eclipse lacks the feature you want. Eclipse缺少您想要的功能。 So, you can do it from Java. 所以,你可以用Java做到这一点。

Instead of calling System.loadLibrary , call System.load . 而不是调用System.loadLibrary ,调用System.load This requires you to write code to calculate the correct pathname based on the current platform. 这要求您编写代码以根据当前平台计算正确的路径名。 You'll need to use -D to pass a value in to say what the current platform is, or read it from a file. 您需要使用-D传递值来说明当前平台是什么,或者从文件中读取它。

This removes java.library.path from the equation. 这将从等式中删除java.library.path

If your JNI library has dependencies, you will also need to set PATH, LD_LIBRARY_PATH, or DYLD_LIBRARY_PATH. 如果您的JNI库具有依赖项,则还需要设置PATH,LD_LIBRARY_PATH或DYLD_LIBRARY_PATH。 Unless you take the further step of looking at https://github.com/bimargulies/jni-origin-testbed . 除非你采取进一步的步骤来查看https://github.com/bimargulies/jni-origin-testbed For Windows, however, the necessary machinations with the delay-loader aren't in there. 然而,对于Windows,使用延迟加载器的必要的机器人不在那里。

I may be wrong, but I think the native libraries are things like DLLs, shared objects, etc., and don't go on the classpath at all. 我可能是错的,但我认为本机库是DLL,共享对象等等,并且根本不会进入类路径。 Each environment will have something like a PATH variable or the like, which will need to include the native libraries. 每个环境都有类似PATH变量之类的东西,需要包含本机库。 The user may have to some configuration (either within Eclipse, or via environment variables). 用户可能需要进行一些配置(在Eclipse中,或通过环境变量)。

Libraries are not searched within the classpath, the system PATH and the property java.library.path . 不在类路径,系统PATH和属性java.library.path搜索库。

In your case may be a custom ClassLoader implementation, especially the findLibrary(..) and loadLibrary(..) methods. 在您的情况下可能是自定义ClassLoader实现,尤其是findLibrary(..)loadLibrary(..)方法。 This would allow to select an appropriate library based on the detected OS. 这将允许基于检测到的OS选择适当的库。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM