Current location - Quotes Website - Personality signature - Please tell me about the NullPointerException problem in C calling Java implementation in JNI programming
Please tell me about the NullPointerException problem in C calling Java implementation in JNI programming

The two programming languages ????java and C, the mutual calls between them:

1. To call dll written in C, java can use JNI or Jawin open source project (the second one is recommended method).

2. C calls Java variables and methods and interacts with Java classes through JNI (Java Native Interface).

----Operation steps (only the second one is summarized)-----

(1) Compile C program with vc6.0, development environment settings: Tools--》 Options-->Tools, under the Tools tab: select "include files" and add the header file directory: C:\Program Files\Java\jdk1.5.0\include and C:\Program Files\Java\jdk1.5.0\include\win32 ;Select under "Libary files" and add the LIB directory: C:\Program Files\Java\jdk1.5.0\lib. It will be compiled into an exe file.

Execution program environment settings: Add the Path environment variable: C:\Program Files\Java\jdk1.5.0\jre\bin\client (the directory where jvm.dll is located). If you do not add the path, you will be prompted to execute. jvm.dll was not found.

(2) GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");

//( [Ljava/lang/String;)V is the main() signature

Execute in the java program directory: javap -s -p ClassDemo (Note: ClassDemo.java has been compiled)

Take the statement below main:: Signature: ([Ljava/lang/String;)V

(3) Attached code example:

java program

import java.io.*;

public class DemoMain{

public static void main(String[] args) throws java.io.IOException, java.lang.NullPointerException

{

System.out.println("This is a test.");

}

}

C Program:

#ifndef __cplusplus

#define __cplusplus

#endif

#include "jni.h"

#include lt;stdio.hgt;

#include lt;stdlib.hgt;

#include lt;windows.hgt;

#pragma comment (lib, "C:\\Program Files\\Java\\jdk1.5.0\\lib\\jvm.lib") // Dynamically call lib

#pragma warning(disable: 4129) // Turn off warning, 4129

void main() {

LoadLibrary("C:\\Program Files\\Java\jre1.5.0\\bin\\client\\jvm.dll "); // Dynamically call dll

JavaVM *jvm;

JNIEnv *env;

JavaVMInitArgs vm_args;

JavaVMOption options[ 3];

options[0].optionString = "-Djava.compiler=NONE";

options[1].optionString = "-Djava.classpath=.";

options[2].optionString = "-verbose:jni";

vm_args.version = JNI_VERSION_1_4;

vm_args.nOptions = 3;

vm_args.options = options;

vm_args.ignoreUnrecognized = JNI_TRUE;

jint res = JNI_CreateJavaVM(amp; jvm, (void**)amp; env, amp; vm_args); // Create a virtual machine

if (res lt; 0) {

fprintf(stderr, "Can't create Java VM\n");

exit(1);

p>

};

jclass cls = env-gt; FindClass("DemoMain");

if (cls == 0) printf("Sorry, I can' t find the class");

fprintf(stdout, "This is invokeSimplified4.\n");

jmethodID get_main_id;

if(cls != NULL)

{

get_main_id =env-gt;GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");

fprintf(stdout, "This is invokeSimplified5.\n");

if(get_main_id != NULL )

{

jclass string = env- gt; FindClass("java/lang/String");

jobjectArray args = env-gt; NewObjectArray(0, string, NULL);

fprintf(stdout, "This is invokeSimplified6.\n");

int i = env-gt; CallIntMethod(cls, get_main_id, args);

fprintf(stdout, i "This is invokeSimplified7.\n" );

}

}

jvm-gt; DestroyJavaVM();

fprintf(stdout, "Java VM destroy\n ");

}