以下内容均来自互联网,感谢你们的分享,我只是使用的时候看这方便,可以称呼我“搬运工”

如有不合适的地方请与我联系,我会及时改正

首先你可能会遇见以下错误

第一个错误是你在vs编译器没有选择使用release版的,而是用debug版的

详细见http://blog.csdn.net/niuxinlong/article/details/4176612

F:\java>java testdll
Exception in thread "main" java.lang.UnsatisfiedLinkError: F:\java\testdll.dll:
应用程序无法启动,因为应用程序的并行配置不正确。有关详细信息,请参阅应用程序事件
日志,或使用命令行 sxstrace.exe 工具。
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:)
at java.lang.Runtime.loadLibrary0(Runtime.java:)
at java.lang.System.loadLibrary(System.java:)
at testdll.<clinit>(testdll.java:)

第二个是在64位运行了一个32位dll

C:\testjni\testdll\x64\Debug>java testdll
Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\testjni\testdll\x6
\Debug\testdll.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:)
at java.lang.Runtime.loadLibrary0(Runtime.java:)
at java.lang.System.loadLibrary(System.java:)
at testdll.<clinit>(testdll.java:)
Could not find the main class: testdll. Program will exit.

第三个当然是编码问题喽,编译class时使用encoding参数

C:\Users\Dmeck\Desktop\Java\winbox>javac -encoding gb2312 Testwin.java

C:\Users\Dmeck\Desktop\Java\winbox>java Testwin

中文乱码                                                               

—————————————————————Body———————————————————————————————————

好了下面一个xp简单实现,

环境:java1.6、MinGw、sublime、xp32、vs2008

在硬盘准备一个目录

编写一个java加载JNi的本地实现

testdll.java

public class testdll
{
static
{
System.loadLibrary("testdll");
}
public native static int get();
public native static void set(int i);
public static void main(String[] args)
{
testdll test = new testdll();
test.set(10);
System.out.println(test.get());
}
}

cmd进入以上目录

运行

javac testdll.java
得到class
javah testdll
得到c的头文件

此处有两种方式:

一个把java安装目录的include目录

jni.h以及32目录下的jni_md.h复制当前工作目录

第二个是直接进行下一步

下一步:查看并修改刚刚编译的头文件testdll.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class testdll */ #ifndef _Included_testdll
#define _Included_testdll
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: testdll
* Method: get
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_testdll_get
(JNIEnv *, jclass); /*
* Class: testdll
* Method: set
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_testdll_set
(JNIEnv *, jclass, jint); #ifdef __cplusplus
}
#endif
#endif

修改全局引用为本地目录引用

#include <jni.h>
改为
#include ”jni.h“

之后在当前目录编写cpp实现头文件的方法

#include "testdll.h"
int i = ;
JNIEXPORT jint JNICALL Java_testdll_get(JNIEnv *, jclass)
{
return i;
} JNIEXPORT void JNICALL Java_testdll_set(JNIEnv *, jclass, jint j)
{
i = j;
}

接着运行gcc

当然本例是winxp32位默认编译是32位dll

以下是博客参考博客地址
https://www.zhihu.com/question/36666057/answer/68516501
事实上很多情况下那样调用都是不成功的,生成dll代码一般这样最完整

gcc -Wl,--add-stdcall-alias -I "C:\Program Files\Java\jdk1.6.0_31\include" -I "C:\Program Files\Java\jdk1.6.0_31\include\win32" -shared -o testdll.dll testdll.cpp
当你使用gcc的-m64编译64位dll会出现
C:\Documents and Settings\Administrator\桌面\java1>gcc -m64 -Wl,--add-stdcall-al
ias -I "C:\Program Files\Java\jdk1.6.0_31\include" -I "C:\Program Files\Java\jdk
1.6.0_31\include\win32" -shared -o testdll.dll testdll.cpp
testdll.cpp::: sorry, unimplemented: -bit mode not compiled in
#include "testdll.h"

所以这时候用编译工具最合适了vs2008很老,但相关博客比较多

 
本例是在windows下做的,生成的是dll文件。并且名称要与java中需要调用的一致,这里就是testdll.dll (跟testdll.java没有关系)
把testdll.dll 拷贝到testdll.class的目录下,java testdll运行它,就可以观察到结果了
C:\Documents and Settings\Administrator\桌面\java1>java testdll

下面介绍vs2008下编译64位

参考http://blog.csdn.net/a_little_e/article/details/45397557

——————————————————————————END——————————————————————————————————

同样最后我们只需要三个文件

testdll.class、testdll32.dll、testdll64.dll

当然testdll.class需要对应的testdll.java源码,来修改

System.loadLibrary("testdll"); 
对应的加载库

java调用c/c++代码简单实现以及遇见的坑的更多相关文章

  1. JNI_最简单的Java调用C/C++代码

    JNI_最简单的Java调用C/C++代码 JNI.是Java Native Interface的简称,中文是"Java本地调用".通过这种技术能够做到下面两点: Java程序中的 ...

  2. JAVA调用c/c++代码

    JNI是Java Native Interface的缩写,中文为JAVA本地调用.使用JNI可以很方便的用我们的Java程序调用C/C++程序.很多时候,某些功能用Java无法实现,比如说涉及到底层驱 ...

  3. 最简单的Java调用C/C++代码的步骤

    1)首先在Java类中声明一个native的方法 (2)使用javah命令生成包含native方法声明的C/C++头文件 (3)按照生成的C/C++头文件来写C/C++源文件 (4)将C/C++源文件 ...

  4. java调用webservice,比较简单方便的方法。

    首先,请同学们自行了解webservice的基础知识. 个人理解,webservice约等于使用http+xml技术进行跨平台的数据交互. http和xml我们都很熟悉了,把他们两个组合到一起就是we ...

  5. java调用c++函数的简单笔记

    java使用jni调用c++动态库函数. 步骤: 1.编写java测试代码如下: public class CallNativeDemo { native void func(); native do ...

  6. Java调用WebService就是这么简单

    https://cloud.tencent.com/developer/article/1080966

  7. java调用第三方命令,process.waitfor()挂起(你不知道的坑)

    我们常在java中运行第三方程序,如sh.python,java提供一个Runtime.exec()方法,生成一个Process对象.今天在使用这个方法的时候,发现接口半天没有返回数据.查了一下,原来 ...

  8. (转)java调用python脚本

    这篇博客旨在吐血分享今天遇到的java调用python脚本遇到的坑,折腾了3个多小时终于可以跑通了,代码超级短,但网上的好多资料都是抄来抄去的,很少有能够直接跑通的,尤其是针对你的python文件中用 ...

  9. java调用kettle_导入jar包(1)

    版权声明:本文为博主原创文章,未经博主允许不得转载. Java调用Kettle执行任务或转换,需要使用Kettle中的jar,可以先导入lib目录中的几个基本的jar,如:kettle-core.ja ...

随机推荐

  1. shell查找进程并终止

    创建kill.sh文件,内容如下: port= #一.根据端口号查询对应的pid,两种都行 pid=$(netstat -nlp | grep :$port | awk '{print $7}' | ...

  2. django model中的save()方法

    Model.save(force_insert=False, force_update=False, using=DEFAULT_DB_ALIAS, update_fields=None) id和pk ...

  3. 【OCP 12c】最新CUUG OCP-071考试题库(62题)

    62.(13-17)choose the best answer: You need to list the employees in DEPARTMENT_ID 30 in a single row ...

  4. AI下载步骤

    ai下载地址:https://www.adobe.com/cn/creativecloud/catalog/desktop.html?promoid=PTYTQ77P&mv=other 破解器 ...

  5. 【BZOJ2440】[中山市选2011]完全平方数

    题意描述 原题 一句话描述: 求第K个不是完全平方数的倍数的数. K≤$10^{9}$ ------------------------------------------ 题解: 首先,直接求第$k ...

  6. How to write date range query in Nest ElasticSearch client?

    Looking at the source code, there are two overloads of the OnField method. When I use the the that t ...

  7. [Linux] 如何在 Linux 中提取随机数

    如何在 Linux 中提取随机数 一.设备文件 /dev/random & /dev/urandom 字符特殊文件 /dev/random 和 /dev/urandom (存在于Linux 1 ...

  8. FutureTask与Fork/Join

    在学习多线程的过程中,我们形成了一种思维习惯.那就是对于某个耗时操作不再做同步操作,让他分裂成一个线程之后执行下一步,而线程执行耗时操作.并且我们希望在我们需要它返回的时候再去调用它的结果集.好比我们 ...

  9. python基础知识梳理----6set 集合的应用

    集合内容简介: set 一: 集合简介 集合set集合是python的一个基本数据类型.一般不是很常用set.中的元素是不重复的.无序的.里里面的元素必须是可hash的tuple,bool),str, ...

  10. 递归实现进制转换(C++版)

    上次呢,我们留下了一道题,今天我们来一起看一看: 题目链接:https://www.cnblogs.com/gaozirong/p/10547434.html 这是我写的程序,大家可以对照参考一下(C ...