java项目导入dll_如何将.dll导入Android java项目(使用eclipse)

Java Native Interface (JNI)

Java Native Interface (JNI) is one of

the intersting interface by java By

using Java Native Interface (JNI) you

can operate with other applications

and libraries.

JNI是Java的本机编程接口,是JDK的一部分.使用JNI,您可以使用其他语言(如C,C)编写的其他应用程序和库.但是基本的问题出现在何时才能使用JNI?

>您需要一些特定于平台的信息,标准Java类库可能不支持应用程序所需的与平台相关的功能.

>您有一些用其他语言编写的库应用程序,并且您希望在Java应用程序中使用它.

>您希望Java应该与某种低级编程语言进行交互.

下面给出简单示例;看看那些方法有’原生’KeyWord:

public native void displayHelloWorld();

public native void displayOther();

private native String getLine(String prompt);

我们将要使用的DLL是firstJNI.DLL这个DLL可以由VC或borland生成.我们稍后会讨论.

//firstJNI.java

class firstJNI

{

public native void displayHelloWorld();

public native void displayOther();

private native String getLine(String prompt);

static {

System.loadLibrary("firstJNI");//This is firstJNI.DLL

/*if generated by borland

System.loadLibrary("firstjni");//This is firstjni.dll

*/

}

public static void main(String[] args)

{

firstJNI JN=new firstJNI();

JN.displayHelloWorld();

JN.displayOther();

String input = JN.getLine("Enter Some Thing ");

System.out.println("You Entered " + input);

}

}

使用编译上面的代码(这是什么意思?)

prompt>javac firstJNI.java

然后使用创建头文件(这是什么意思?)

prompt>javah javah -jni HelloWorld

这将创建firstJNI.h文件.在标题文件中,您将看到

-------------------------------------

JNIEXPORT void JNICALL Java_firstJNI_displayHelloWorld

(JNIEnv *,jobject);

/*

* Class: firstJNI

* Method: displayOther

* Signature: ()V

*/

JNIEXPORT void JNICALL Java_firstJNI_displayOther

(JNIEnv *,jobject);

/*

* Class: firstJNI

* Method: getLine

* Signature: (Ljava/lang/String;)Ljava/lang/String;

*/

JNIEXPORT jstring JNICALL Java_firstJNI_getLine

(JNIEnv *,jobject,jstring);

----------------------------------------------

不要编辑标题文件

现在让我们看看如何使用VC生成DLL,点击:File-> New-> Win32Dynamic-Link Library

给出名字和选择

一个简单的DLL项目

你将会拥有

firstJNI.CPP文件

下面给出了firstJNI.cpp文件

// MYVCDLL.cpp : Defines the entry point for the DLL application.

//

#include "stdafx.h"

#include "D:\Kanad\Study\codeToad Articles\firstJNI.h"

#include "jni.h" //can copy or give full path

#include

BOOL APIENTRY DllMain( HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved

)

{

return TRUE;

}

extern "C" __declspec(dllexport) int getMemorySize();

//And your function definition should look like this:

extern "C" __declspec(dllexport) int getMemorySize()

{ //do something

MEMORYSTATUS memoryStatus;

int MB=1024*1024 ;

double memSize;

memoryStatus.dwLength=sizeof(MEMORYSTATUS);

GlobalMemoryStatus(&memoryStatus);

__int64 size= memoryStatus.dwTotalPhys;

memSize=(double)size/MB;

printf("\nTotal Memory %.0lf MB",ceil(memSize));

return 0;

}

JNIEXPORT void JNICALL

Java_firstJNI_displayHelloWorld(JNIEnv *env,jobject obj)

{

printf("Hello world! This is using VC++ DLL\n");

}

JNIEXPORT void JNICALL

Java_firstJNI_displayOther(JNIEnv *env,jobject obj)

{

printf("Hello world! This is using VC++ DLL Other Function \n");

getMemorySize();

}

JNIEXPORT jstring JNICALL

Java_firstJNI_getLine(JNIEnv *env,jobject obj,jstring enter)

{

char buf[128];

const char *str = env->GetStringUTFChars(enter,0);

printf("\n%s",str);

env->ReleaseStringUTFChars(enter,str);

scanf("%s",buf);

return env->NewStringUTF(buf);

}

现在我对如何在我的java应用程序中使用C/C++编写的.dll文件有疑问.我正在使用Eclipse开发android的应用程序,我有一些dll文件,我没有他们的源…我如何在我的项目中使用它们?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值