简体   繁体   English

Intellij IDEA Java项目和JNI

[英]Intellij IDEA Java project and JNI

Is it possible for the pure Java project, created at Intellij IDEA, to use .so files (like Android project does)? 在Intellij IDEA创建的纯Java项目是否可以使用.so文件(就像Android项目一样)?

Surfing over internet I can not find any information about it. 通过互联网冲浪我找不到有关它的任何信息。 Sounds very strange. 听起来很奇怪。

UPDATE: 更新:

What I have in Android is a simple script: 我在Android中拥有的是一个简单的脚本:

def ndkBuild;
def ndkBuildingDir = mainSrcDir;
def hasNdk = false;
if (System.env.ANDROID_NDK_HOME != null) {
    hasNdk = true;
    ndkBuild = new File(System.env.ANDROID_NDK_HOME, ndkBuildFilename())
}

commandLine ndkBuild, "--directory", ndkBuildingDir, "APP_BUILD_SCRIPT=${ndkBuildingDir}/native/Android.mk", "NDK_APPLICATION_MK=${ndkBuildingDir}/native/Application.mk"

And I can not found any documentation of doing such things at Java project. 而且我在Java项目中找不到任何有关执行此类操作的文档。

In short, yes. 简而言之,是的。

However it will stop being a pure Java project and will introduce a dependency on a system the native binary is compiled on/written for. 但是,它将不再是纯Java项目,并且会引入对本机二进制文件进行编译/编写的系统的依赖性。

To do this, write JNi code, like you would do on android (as you seem to be familiar with that), compile into a dynamic library with certain compiler flags and then use System.loadLibrary("foo"); 为此,请像在android上那样编写JNi代码(您似乎对此很熟悉),使用某些编译器标志编译成动态库,然后使用System.loadLibrary("foo"); in your Java code, just like you would on an android project. 在您的Java代码中,就像在android项目上一样。

There's really a lot more to this though, and there's quite a few tutorials online that describe how to do this. 但是,实际上还有很多其他内容,并且在线上有很多教程描述了如何执行此操作。 I would suggest to try to solve the problem in Java before opting for JNI though as there's a lot of places to stumble and cause serious bugs in JNI. 我建议在选择JNI之前尝试解决Java中的问题,因为有很多地方会绊倒并导致JNI中的严重错误。

I've never written any JNI code in IDEA though, I'm sure it's possible but it's not really a C or C++ IDE, so you might want to use something else for that part. 我从来没有在IDEA中编写过任何JNI代码,我敢肯定这是可能的,但它实际上不是C或C ++ IDE,因此您可能想在此部分中使用其他功能。

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

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