Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to call java class in C language project

Shulou Source: shulou.com Published: 2022-06-02 08:27:23 09月19日 Update

It is believed that many inexperienced people don't know what to do about how to call java class in C language project. therefore, this paper summarizes the causes and solutions of the problem. I hope you can solve this problem through this article.

First you need to write the java class

Public class Sample {public static void test2 () {System.out.println ("java printed helloword");}}

This is a very simple java class with only one java method in it. We're going to call this method through C later. Jvm only knows class. First, you have to compile it, java Sample.java. Get a Sample.class file in the current directory. This file will be loaded by our C program later.

Then write C code.

# include / / introduce the header file of jni. The following methods for loading and calling classes are implemented through # include int main () {JavaVMOption options [1]; JNIEnv * env; JavaVM * jvm; JavaVMInitArgs vm_args; long status; jclass cls; jmethodID mid; jint square; jboolean not Options [0] .optionString = "- Djava.class.path=."; / / set classpath memset (& vm_args, 0, sizeof (vm_args)); vm_args.version = JNI_VERSION_1_2; vm_args.nOptions = 1; vm_args.options = options; status = JNI_CreateJavaVM (& jvm, (void**) & env, & vm_args) If (status! = JNI_ERR) {/ / cls= (* env)-> FindClass (env, "java/lang/Object"); cls= (* env)-> FindClass (env, "Sample"); / / find this class through the FindClass function. Here you need to add that the description of the class matches. If it is Object, it should be java/lang/Object printf ("find the class\ n"). If (cls! = 0) {/ / mid= (* env)-> GetStaticMethodID (env,cls, "main", "(I) I"); mid= (* env)-> GetStaticMethodID (env,cls, "main", "([Ljava/lang/String;) V"); / / find the method through the function, which should be the method descriptor. Printf ("find the method\ n"); if (mid! = 0) {printf ("exec method\ n"); square = (* env)-> CallStaticIntMethod (env, cls, mid, NULL) / / execution method printf ("Result of intMethod:% d\ n", square);} mid = (* env)-> GetStaticMethodID (env, cls, "booleanMethod", "(Z) Z") If (mid! = 0) {not = (* env)-> CallStaticBooleanMethod (env, cls, mid, 1); printf ("Result of booleanMethod:% d\ n", not) }} else {printf ("not found\ n");} (* jvm)-> DestroyJavaVM (jvm); return 0;} else return-1;}

An error will be reported if you directly gcc because the jni header file is not in the system header file. And the dynamic link library is not in the system default library. So write a Makefile:

Compile: gcc calljvm.c-I $JAVA_HOME/include/-I $JAVA_HOME/include/linux/-L$JAVA_HOME/jre/lib/amd64/server/-ljvm-o calljvm

The execution of make should be compiled smoothly, but the execution will report an error. Because libjvm.so is not in / usr/lib. So you have to add the jvm library to the system library path.

Export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/amd64/:$JAVA_HOME/jre/lib/amd64/servr

By default, the system will look for dynamic link libraries in / usr/lib. If you have LD_LIBRARY_PATH, you will first find it in the path specified by LD_LIBRARY_PATH. If you can't find it, go to / usr/lib.

After reading the above, have you mastered how to call the method of java class in the C language project? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Method file system function language project content dynamic more path link problem report compile helpless for this code reason only for this Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info vpn NVidia Shulou Technology Microsoft