English 中文(简体)
在C++/c[复制]中使用JAR档案
原标题:Use JAR file in c++/c [duplicate]
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
How to access the Java method in a C++ application

I need to use JAR file in c++ program. i.e. from c++ i need to call java function, for example, In java there is a function who accept 2 integer and return addition of that, Now i need to call this function from c++. Please guide me Thanks in advance.

最佳回答

You need to use the Java Invocation API, described here. This example code (from that link) shows how to load in a Java Virtual Machine and use it to call a static Java method named test with an int argument, located in the class Main. In this example, the path to the JAR file would be set using the vm_args.classpath variable.

#include <jni.h>       /* where everything is defined */
...
JavaVM *jvm;       /* denotes a Java VM */
JNIEnv *env;       /* pointer to native method interface */
JDK1_1InitArgs vm_args; /* JDK 1.1 VM initialization arguments */
vm_args.version = 0x00010001; /* New in 1.1.2: VM version */
/* Get the default initialization arguments and set the class 
 * path */
JNI_GetDefaultJavaVMInitArgs(&vm_args);
vm_args.classpath = ...;
/* load and initialize a Java VM, return a JNI interface 
 * pointer in env */
JNI_CreateJavaVM(&jvm, &env, &vm_args);
/* invoke the Main.test method using the JNI */
jclass cls = env->FindClass("Main");
jmethodID mid = env->GetStaticMethodID(cls, "test", "(I)V");
env->CallStaticVoidMethod(cls, mid, 100);
/* We are done. */
jvm->DestroyJavaVM();

如果你想要使用一种非静态的方法,则该守则将仅略有不同,其余部分则在解释了你们需要知道的一切。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签