
Steps:
1. Create a virtual machine;
2. Get class;
3. Instantiate the object: obtain the construction method (the method name is "
4. Calling method: It is divided into obtaining method, constructing method and calling method.
Operation method:
1. Compile: javac Hello.java
2. javap -p -s Hello.class: View Signature
3. gcc -I/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/ -I/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/linux / -o caller caller.c -L /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server -ljvm
4. Execute: LD_LIBRARY_PATH=/usr/lib/ jvm/java-8-openjdk-amd64/jre/lib/amd64/server ./caller
Example:
(1)call_static_method
#include <stdio.h>
#include <jni.h>
JNIEnv* create_vm(JavaVM** jvm, JNIEnv** env)
{
JavaVMInitArgs args;
JavaVMOption options[1];
args.version = JNI_VERSION_1_6;
args.nOptions = 1;
options[0].optionString = "-Djava.class.path=./";
args.options = options;
args.ignoreUnrecognized = JNI_FALSE;
return JNI_CreateJavaVM(jvm, (void **)env, &args);
}
int main(int argc, char **argv)
{
JavaVM* jvm;
JNIEnv* env;
jclass cls;
int ret = 0;
jmethodID mid;
/* 1. create java virtual machine */
if(create_vm(&jvm, &env))
{
printf("can not create jvm\n");
return -1;
}
/* 2. get class */
cls = (*env)->FindClass(env, "Hello");
if(cls == NULL)
{
printf("can not find hello class\n");
ret = -1;
goto destory;
}
/* 3. create object */
/* 4. call method
* 4.1 get method
* 4.2 create parameter
* 4.3 call method
*/
mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V");
if(mid == NULL)
{
ret = -1;
printf("can not get method\n");
goto destory;
}
(*env)->CallStaticVoidMethod(env, cls, mid, NULL);
destory:
(*jvm)->DestroyJavaVM(jvm);
return ret;
}(2)call_non_static_method
#include <stdio.h>
#include <jni.h>
JNIEnv* create_vm(JavaVM** jvm, JNIEnv** env)
{
JavaVMInitArgs args;
JavaVMOption options[1];
args.version = JNI_VERSION_1_6;
args.nOptions = 1;
options[0].optionString = "-Djava.class.path=./";
args.options = options;
args.ignoreUnrecognized = JNI_FALSE;
return JNI_CreateJavaVM(jvm, (void **)env, &args);
}
int main(int argc, char **argv)
{
JavaVM* jvm;
JNIEnv* env;
jclass cls;
int ret = 0;
jmethodID mid;
jmethodID cid;
jobject jobj;
jstring jstr;
int r;
/* 1. create java virtual machine */
if(create_vm(&jvm, &env))
{
printf("can not create jvm\n");
return -1;
}
/* 2. get class */
cls = (*env)->FindClass(env, "Hello");
if(cls == NULL)
{
printf("can not find hello class\n");
ret = -1;
goto destory;
}
/* 3. create object
*
*/
cid = (*env)->GetMethodID(env, cls, "<init>", "()V");
if(cid == NULL)
{
printf("can not get construct method\n");
ret = -1;
goto destory;
}
jobj = (*env)->NewObject(env, cls, cid);
if(jobj == NULL)
{
printf("can not create object\n");
ret = -1;
goto destory;
}
/* 4. call method
* 4.1 get method
* 4.2 create parameter
* 4.3 call method
*/
mid = (*env)->GetMethodID(env, cls, "sayhello_to", "(Ljava/lang/String;)I");
if(mid == NULL)
{
ret = -1;
printf("can not get method\n");
goto destory;
}
jstr = (*env)->NewStringUTF(env, "287787472@qq.com");
r = (*env)->CallIntMethod(env, jobj, mid, jstr);
printf("ret = %d\n", r);
destory:
(*jvm)->DestroyJavaVM(jvm);
return ret;
}php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!
The above is the detailed content of How to call java in c. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools





