In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.