Calling Java Class Methods from C with JNI
2016-03-29 14:30
495 查看
http://hildstrom.com/projects/jni/index.html
calling Java class methods from C code with JNI is the best approach. I leveraged existing examples and web pages heavily, but many of them did not include a Makefile or any sort of build notes. This is what worked for me: hello_world.tar.gz.
Calling Java Class Methods from C with JNI
It never fails. There is some great piece of code that will save you tons of time, but it is written in Java and your project is written in C or C++. Okay, I've really only needed this a few times, but sometimescalling Java class methods from C code with JNI is the best approach. I leveraged existing examples and web pages heavily, but many of them did not include a Makefile or any sort of build notes. This is what worked for me: hello_world.tar.gz.
helloWorld.java
public class helloWorld{ public static void main(String[] args){ System.out.println("Hello, World"); } public static int square(int input){ int output = input * input; return output; } public static int power(int input, int exponent){ int output,i; output=1; for(i=0;i<exponent;i++){ output *= input; } return output; } }
hello_world.c
/* These three pages were instrumental while trying to get this working. I started with existing examples, got them working, and updated them to make sure I understood how things worked. If you are trying this for the first time, I suggest adding a new method with different arguments to the Java class and then calling it from this C program. Use 'javap -s -p helloWorld.class' to get the new method signature. After that is working, package the class in a jar file, update the class path in this C source file, and get that working. http://www.inonit.com/cygwin/jni/invocationApi/c.html http://www.codeproject.com/Articles/22881/How-to-Call-Java-Functions-from-C-Using-JNI http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniexamp.html */ #include <stdio.h> #include <jni.h> JNIEnv* create_vm(JavaVM **jvm) { JNIEnv* env; JavaVMInitArgs args; JavaVMOption options; args.version = JNI_VERSION_1_6; args.nOptions = 1; options.optionString = "-Djava.class.path=./"; args.options = &options; args.ignoreUnrecognized = 0; int rv; rv = JNI_CreateJavaVM(jvm, (void**)&env, &args); if (rv < 0 || !env) printf("Unable to Launch JVM %d\n",rv); else printf("Launched JVM! :)\n"); return env; } void invoke_class(JNIEnv* env) { jclass hello_world_class; jmethodID main_method; jmethodID square_method; jmethodID power_method; jint number=20; jint exponent=3; hello_world_class = (*env)->FindClass(env, "helloWorld"); main_method = (*env)->GetStaticMethodID(env, hello_world_class, "main", "([Ljava/lang/String;)V"); square_method = (*env)->GetStaticMethodID(env, hello_world_class, "square", "(I)I"); power_method = (*env)->GetStaticMethodID(env, hello_world_class, "power", "(II)I"); (*env)->CallStaticVoidMethod(env, hello_world_class, main_method, NULL); printf("%d squared is %d\n", number, (*env)->CallStaticIntMethod(env, hello_world_class, square_method, number)); printf("%d raised to the %d power is %d\n", number, exponent, (*env)->CallStaticIntMethod(env, hello_world_class, power_method, number, exponent)); } int main(int argc, char **argv) { JavaVM *jvm; JNIEnv *env; env = create_vm(&jvm); if(env == NULL) return 1; invoke_class(env); return 0; }
Makefile
all: run helloWorld.class: helloWorld.java /usr/java/latest/bin/javac helloWorld.java hello_world: hello_world.c gcc -o hello_world \ -L /usr/java/latest/jre/lib/amd64/server/ \ -ljvm \ -I /usr/java/latest/include/ \ -I /usr/java/latest/include/linux/ \ hello_world.c run: helloWorld.class hello_world export LD_LIBRARY_PATH="/usr/java/latest/jre/lib/amd64/server/" ./hello_world clean: rm -f helloWorld.class hello_world
Building and Test Output
[BUILDROOT-RHEL5 hello_world]# make /usr/java/latest/bin/javac helloWorld.java gcc -o hello_world \ -L /usr/java/latest/jre/lib/amd64/server/ \ -ljvm \ -I /usr/java/latest/include/ \ -I /usr/java/latest/include/linux/ \ hello_world.c export LD_LIBRARY_PATH="/usr/java/latest/jre/lib/amd64/server/" ./hello_world Launched JVM! :) Hello, World 20 squared is 400 20 raised to the 3 power is 8000
相关文章推荐
- 在Java的Spring框架中配置Quartz的教程
- Java反射与内省
- 新手 Spring 疑惑
- java 获得字符串中最大重复子串长度
- JavaSe基础(5)-- 类和对象
- js--jsp--java编码转换问题
- struts+spring+hibernate感悟
- spring ContextLoaderListener的作用
- struts例子
- java之double类型数值的比较
- eclipse或myeclipse中的项目用配置的Tomact启动问题
- JAVA中的getClass().getClassLoader()
- Invalid Gradle JDK configuration found?
- java-final关键字
- 打开32位eclipse启动界面闪一下就消失
- Java学习笔记--同步辅助类
- Java学习·变量类型
- JAVA第三周作业(从键盘输入若干数求和)
- 浅谈Java中的hashcode方法
- Struts2中date标签乱码问题解决