惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Webroot Blog
Webroot Blog
U
Unit 42
A
About on SuperTechFans
宝玉的分享
宝玉的分享
月光博客
月光博客
C
CERT Recently Published Vulnerability Notes
P
Privacy International News Feed
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
P
Privacy & Cybersecurity Law Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
S
Securelist
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Spread Privacy
Spread Privacy
L
Lohrmann on Cybersecurity
Apple Machine Learning Research
Apple Machine Learning Research
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
B
Blog
I
Intezer
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
V
V2EX
L
LangChain Blog
AI
AI
G
GRAHAM CLULEY
T
Tor Project blog
人人都是产品经理
人人都是产品经理
D
Docker
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
I
InfoQ
Y
Y Combinator Blog
C
Comments on: Blog
GbyAI
GbyAI
www.infosecurity-magazine.com
www.infosecurity-magazine.com
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
H
Help Net Security
Vercel News
Vercel News
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿

博客园 - 郄永军

用触发器实现表的同步操作 Quartz CronTrigger最完整配置说明 - 郄永军 - 博客园 如何优化JAVA程序开发,提高JAVA性能 使用Hibernate+Middlegen实现自动代码生成简介 Web.xml加载顺序 java性能优化 Java NIO简介 选用ibatis和hibernate的区别 Fusioncharts 参数 dwr介绍 ASP.NET缓存:方法分析和实践示例 比较同一数据库不同版本间数据表之间差异 如何动态执行存储过程或函数 NTKO正文控件的使用技巧 - 郄永军 - 博客园 Dorado开发框架下保存附件存草稿功能 - 郄永军 - 博客园 JS中的关于类型转换的性能优化 - 郄永军 - 博客园 如何把ini文件转换为xml 生成随即的数值 获取文件大小的java程序
如何用java调用c++编写成的DLL
郄永军 · 2010-12-14 · via 博客园 - 郄永军

虽然java已经能够帮我们做了很多事情,几乎大部分的东西现在都可以用java来编写,但是有很多时候,用c++能够更好的实现系统的一些功能,因此,在java中调用c++编写的东西就显得十分的必要。这边文章将为你介绍用java调用vc++编写的工程的dll文件。

1.

。编写java的类,这个类中System.loadLibrary()是加载动态链接库,SallyDLL是由c++产生的文件,等下将有介绍,
public native int add(int num1, int num2);
是一个声明的方法,该方法的实现是由c++完成的,在java中可以跟一般
的方法一样调用。

1.     package testJNI.test;

2.      

3.     public class TestDLL

4.     {

5.         static

6.         {

7.             System.loadLibrary("SallyDLL");

8.         }

9.         

10.        public native int add(int num1, int num2);

11.    }

12.     

13.     

2..编译上面的类。我使用eclipse写的,自动编译,打开cmd,进入到java工程的bin下。
使用命令:javah -classpath . -jni testJNI.test.TestDLL
这时会生成.h文件:testJNI_test_TestDLL.h
.h
文件内容如下:
) -

1.     #include <jni.h>

2.      

3.     #ifndef _Included_testJNI_test_TestDLL

4.     #define _Included_testJNI_test_TestDLL

5.     #ifdef __cplusplus

6.     extern "C" {

7.     #endif

8.     JNIEXPORT jint JNICALL Java_testJNI_test_TestDLL_add

9.       (JNIEnv *, jobject, jint, jint);

10.     

11.    #ifdef __cplusplus

12.    }

13.    #endif

14.    #endif

15.     

3..vc++建立DLL工程,
  
注:将testJNI_test_TestDLL.h    copy到工程下
      
jdk/include下的jniport.h   copy到工程下
      
jdk/include下的jni.h    copyVC++安装目录下的include   C:\Program Files\Microsoft Visual Studio   9.0\VC\include  下面

4..

编写add方法的实现,新建testDll.cpp  代码如下:

1.     #include "stdafx.h"

2.     #include "testJNI_test_TestDLL.h"

3.      

4.     JNIEXPORT jint JNICALL Java_testJNI_test_TestDLL_add

5.       (JNIEnv * env, jobject obj, jint num1, jint num2)

6.     {

7.             return num1 + num2;

8.     }

5..Build C++工程,得到DLL文件,将文件的名改为,   SallyDLL.dll   以上面一致,并将dll文件放到jdkbin

6..

测试,编写java测试代码 java中调用c++

1.     public class TestDLLMain

2.     {

3.      

4.         

5.         public static void main(String[] args)

6.         {

7.             // TODO Auto-generated method stub

8.             TestDLL test = new TestDLL();

9.             System.out.println(test.add(20, 30));

10.        }

11.     

12.    }

13.     

14.     

可以看到输出为 50通过上面的方法就实现了java调用c++编写的东西