C调用java例子

本文介绍了如何使用JNI(Java Native Interface)从C代码调用Java方法。详细步骤包括:1) 编译Java源码;2) 编译C代码,指定jni.h和jni_md.h路径,以及jvm.dll位置;3) 注册jvm.dll到系统。提供了实验代码的下载链接和一个简单的Makefile示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实验代码已打包,下载地址

实现原理:使用JNI提供的接口来反射得到Java方法,进行调用。jni.h在JDK里(jdk1.8.0_121/include/)。

(通过JNI可以实现java调用C,也可以实现C调用java)

 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:

#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;
}

 # 编译java

javac HelloWorld.java

 # 编译C

gcc -D__int64="long long" -Ic:/java/jdk1.8.0_121/include/ -Ic:/java/jdk1.8.0_121/include/win32 -ohello_world hello_world.c -L c:/java/jdk1.8.0_121/jre/bin/server -ljvm

要点

a)     使用64位的JDK需要添加-D__int64="longlong"

b)     通过-I参数指定jni.h、jni_md.h的路径。

c)     通过-L指定jvm.dll的路径。

这3个文件都在JDK文件夹里面,可以通过搜索找到具体的路径。

#运行程序

hello_world

如果报找不到jvm.dll的错,把jvm.dll(路径:jdk1.8.0_121\jre\bin\server)注册到系统。

具体如何注册参考这篇文章:https://jingyan.baidu.com/article/fb48e8be3212766e632e147f.html

可以写Makefile一步到位,Makefile:

all: run
 
HelloWorld.class: HelloWorld.java
       javac HelloWorld.java

hello_world: hello_world.c
       gcc -D__int64="long long" -Ic:/java/jdk1.8.0_121/include/-Ic:/java/jdk1.8.0_121/include/win32 -o hello_world hello_world.c -Lc:/java/jdk1.8.0_121/jre/bin/server -ljvm

run: HelloWorld.class hello_world
       ./hello_world

clean:
       rm -f HelloWorld.class hello_world

 编译和运行:

>make

输出结果:

javac HelloWorld.java
gcc -Ic:/java/jdk1.8.0_121/include/ -Ic:/java/jdk1.8.0_121/include/win32 -ohello_world hello_world.c -L c:/java/jdk1.8.0_121/jre/bin/server -ljvm
./hello_world
Launched JVM! :)
Hello, World
20 squared is 400
20 raised to the 3power is 8000

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值