How to import .dll into a java project for Android (working with eclipse)

Java Native Interface (JNI)

Java Native Interface (JNI) is one of the intersting interfaces through java By using Java Native Interface (JNI) can work with other applications and libraries .

JNI is a native Java programming interface that is part of the JDK. Using JNI, you can work with other applications and libraries written in another language, such as C, C ++. But the main question is, when should you use JNI?

  • You want some specific platform information, and the standard Java class library may not support the platform-specific features your application needs.
  • You have a library application written in another language, and you want to use it in your java application.
  • You want Java to interact with a low-level programming language.

The following is a simple example; See that methods have a "native" KeyWord:

public native void displayHelloWorld();
public native void displayOther();
private native String getLine(String prompt);

The DLL we are going to use is firstJNI.DLL. This DLL can be generated by VC ++ or borland. What we will discuss later.

//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); 
     }
}

Compile the above code using (What does this mean?)

prompt>javac firstJNI.java

Then create a header file with (What does this mean?)

prompt>javah javah -jni HelloWorld

This will create the firstJNI.h file. In the header file you will see

-------------------------------------
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);
----------------------------------------------

Do not edit file header

, DLL V++, : - > - > Win32Dynamic-Link 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 <math.h>

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);

}

, DLL , ++/C java. android Eclipse, dll, ... ?

+5
4

-, - , , JNI.

JNI , , , . , , javah util , cpp - jni Java cpp.

dll, DLL, . , , , DLL-, DLL- - , . , , JNI, Java , , , DLL .

, SWIG, ++, dll . -, ++ , - (.i .swg), SWIG .

+3

, Windows (DLL) Android. Android .so.

+1

Java, Android:

DLL Java

C/++ DLL Java

, C/++, DLL Windows, Java J/Invoke.

Microsoft Visual Studio 2005 DLL ++ Java Eclipse Java IDE. IDE . "" , Visual Studio Eclipse - , !

, Windows 2000, XP, Vista 2003.

J/Invoke Java SE. Java, JDK 5.0 . Java .

Continue reading ...

0
source

All Articles