Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create DLL file from java?

    • 0
    • 6
    • 0
    • 4
    • 0
    • 0
    • 0
    • 0
    • 25.3k
    Comment on it

    Hello Guy's
    This Blog will guide how to create DLL file from java using JNI.
    Please follow the below instructions.
    First create a java file that contains the native method and it loads the DLL file.
    Create TestDLL.java and put below code :

    1. public class TestDLL
    2. {
    3. private native String getMsg(); // native method
    4. static
    5. {
    6. System.loadLibrary(TestDLL); // load dll file
    7. }
    8. public static void main(String ars[])
    9. {
    10. System.out.println(new TestDLL.getMsg()); // Access the method of dll file
    11. }
    12. }

    Compile java file:

    1. javac TestDLL.java

    Create an c header file using javah as follows:

    1. Javah jni TestDLL

    That c header file looks like

    1. /* DO NOT EDIT THIS FILE it is machine generated */
    2. #include <jni.h>
    3. /* Header for class TestDLL */
    4. #ifndef _Included_TestDLL
    5. #define _Included_TestDLL
    6. #ifdef __cplusplus
    7. extern C {
    8. #endif
    9. /*
    10. *Class: TestDLL
    11. *Method: getMsg
    12. *Signature: ()Ljava/lang/String;
    13. */
    14. JNIEXPORT jstring JNICALL Java_TestDLL_getMsg
    15. (JNIEnv *,jobject);
    16. #ifdef cplusplus
    17. }
    18. #endif
    19. #endif

    After that create a c++ file that defines the native method which is declared in java file, and after creating c header file it will show in that header file as follows :

    1. JNIEXPORT jstring JNICALL Java_TestDLL_getMsg
    2. (JNIEnv *, jobject)

    Note: for creating c++ file Dev C++ tool is best for me and for you also.
    The c++ file is :

    1. #include<jni.h>
    2. #include<stdio.h>
    3. #include<string.h>
    4. #include TestDLL.h
    5. JNIEXPORT jstring JNICALL Java_TestDLL_getMsg
    6. (JNIEnv *env,jobject obj)
    7. {
    8. return env->NewStringUTF(Welcome to findnerd.com);
    9. }

    After creating c++ file create a project in dev c++ :
    1. New Project ->DLL
    2. Enter project Name TestDLL and checked on C++ Project.
    3. Click on OK Button.

    After that close the default open files without saving them.
    Add C header file and C++ file to the project.
    Right click on project and go to project option
    1. Project Option -> Directories -> Include Directories
    2. Add both java directories path
    i.e

    1. root\java\jdk7\include
    2. root\java\jdk7\include\win32.


    3. Click on OK button.
    Go to Execute menu and click on Rebuild All or press Ctrl+F11
    It creates the dll file after that when we run the code via below command, we got output .

    1. java TestDLL

    OutPut :

    1. Welcome to findnerd.com

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: