从 C++ 调用 Java
我试图按照发现的示例从 C++ 调用 Java 虚拟机 这里:
基本上我有一个小的Java程序:
public class TestJNIInvoke
{
public static void main(String[] args)
{
System.out.println(args[0]);
}
}
然后我有一个C++程序,我想创建一个JVM并调用TestJNIInvoke
类:
#include <jni.h>
#include <cstdlib>
#define PATH_SEPARATOR ';' /* define it to be ':' on Solaris */
#define USER_CLASSPATH "." /* where Prog.class is */
using namespace std;
int main() {
JNIEnv *env;
JavaVM *jvm;
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jclass stringClass;
jobjectArray args;
#ifdef JNI_VERSION_1_2
JavaVMInitArgs vm_args;
JavaVMOption options[1];
options[0].optionString =
"-Djava.class.path=" USER_CLASSPATH;
vm_args.version = 0x00010002;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = JNI_TRUE;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
#else
JDK1_1InitArgs vm_args;
char classpath[1024];
vm_args.version = 0x00010001;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
/* Append USER_CLASSPATH to the default system class path */
sprintf(classpath, "%s%c%s",
vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
vm_args.classpath = classpath;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm, &env, &vm_args);
#endif /* JNI_VERSION_1_2 */
if (res < 0) {
fprintf(stderr, "Can't create Java VM\n");
exit(1);
}
cls = (*env)->FindClass(env, "TestJNIInvoke");
if (cls == NULL) {
goto destroy;
}
mid = (*env)->GetStaticMethodID(env, cls, "main",
"([Ljava/lang/String;)V");
if (mid == NULL) {
goto destroy;
}
jstr = (*env)->NewStringUTF(env, " from CPP!");
if (jstr == NULL) {
goto destroy;
}
stringClass = (*env)->FindClass(env, "java/lang/String");
args = (*env)->NewObjectArray(env, 1, stringClass, jstr);
if (args == NULL) {
goto destroy;
}
(*env)->CallStaticVoidMethod(env, cls, mid, args);
destroy:
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionDescribe(env);
}
(*jvm)->DestroyJavaVM(jvm);
}
但是当我尝试编译C++时程序我收到此错误:
c:\java\JNI> g++ -I"c:\Program Files\Java\jdk1.7.0\include"-I"c:\ProgramFiles\Java\jdk1.7.0\include\win32" -c TestJNIInvoke.cpp
TestJNIInvoke.cpp: In function 'int main()':
TestJNIInvoke.cpp:20:31: warning: deprecated conversion from string constant to
'char*'
TestJNIInvoke.cpp:44:18: error: base operand of '->' has non-pointer type 'JNIEn
v'
TestJNIInvoke.cpp:49:18: error: base operand of '->' has non-pointer type 'JNIEn
v'
TestJNIInvoke.cpp:54:19: error: base operand of '->' has non-pointer type 'JNIEn
v'
TestJNIInvoke.cpp:58:26: error: base operand of '->' has non-pointer type 'JNIEn
v'
TestJNIInvoke.cpp:59:19: error: base operand of '->' has non-pointer type 'JNIEn
v'
TestJNIInvoke.cpp:63:12: error: base operand of '->' has non-pointer type 'JNIEn
v'
TestJNIInvoke.cpp:66:16: error: base operand of '->' has non-pointer type 'JNIEn
v'
TestJNIInvoke.cpp:67:16: error: base operand of '->' has non-pointer type 'JNIEn
v'
TestJNIInvoke.cpp:69:12: error: base operand of '->' has non-pointer type 'JavaVM'
有什么想法吗?
谢谢
I am trying to invoke the Java Virtual machine from C++ following the example found here:
Basically I have a small Java program:
public class TestJNIInvoke
{
public static void main(String[] args)
{
System.out.println(args[0]);
}
}
Then I have a C++ program that I want to create a JVM and call the TestJNIInvoke
class:
#include <jni.h>
#include <cstdlib>
#define PATH_SEPARATOR ';' /* define it to be ':' on Solaris */
#define USER_CLASSPATH "." /* where Prog.class is */
using namespace std;
int main() {
JNIEnv *env;
JavaVM *jvm;
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jclass stringClass;
jobjectArray args;
#ifdef JNI_VERSION_1_2
JavaVMInitArgs vm_args;
JavaVMOption options[1];
options[0].optionString =
"-Djava.class.path=" USER_CLASSPATH;
vm_args.version = 0x00010002;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = JNI_TRUE;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
#else
JDK1_1InitArgs vm_args;
char classpath[1024];
vm_args.version = 0x00010001;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
/* Append USER_CLASSPATH to the default system class path */
sprintf(classpath, "%s%c%s",
vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
vm_args.classpath = classpath;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm, &env, &vm_args);
#endif /* JNI_VERSION_1_2 */
if (res < 0) {
fprintf(stderr, "Can't create Java VM\n");
exit(1);
}
cls = (*env)->FindClass(env, "TestJNIInvoke");
if (cls == NULL) {
goto destroy;
}
mid = (*env)->GetStaticMethodID(env, cls, "main",
"([Ljava/lang/String;)V");
if (mid == NULL) {
goto destroy;
}
jstr = (*env)->NewStringUTF(env, " from CPP!");
if (jstr == NULL) {
goto destroy;
}
stringClass = (*env)->FindClass(env, "java/lang/String");
args = (*env)->NewObjectArray(env, 1, stringClass, jstr);
if (args == NULL) {
goto destroy;
}
(*env)->CallStaticVoidMethod(env, cls, mid, args);
destroy:
if ((*env)->ExceptionOccurred(env)) {
(*env)->ExceptionDescribe(env);
}
(*jvm)->DestroyJavaVM(jvm);
}
But When I try to compile the C++ program I get this error:
c:\java\JNI> g++ -I"c:\Program Files\Java\jdk1.7.0\include"-I"c:\ProgramFiles\Java\jdk1.7.0\include\win32" -c TestJNIInvoke.cpp
TestJNIInvoke.cpp: In function 'int main()':
TestJNIInvoke.cpp:20:31: warning: deprecated conversion from string constant to
'char*'
TestJNIInvoke.cpp:44:18: error: base operand of '->' has non-pointer type 'JNIEn
v'
TestJNIInvoke.cpp:49:18: error: base operand of '->' has non-pointer type 'JNIEn
v'
TestJNIInvoke.cpp:54:19: error: base operand of '->' has non-pointer type 'JNIEn
v'
TestJNIInvoke.cpp:58:26: error: base operand of '->' has non-pointer type 'JNIEn
v'
TestJNIInvoke.cpp:59:19: error: base operand of '->' has non-pointer type 'JNIEn
v'
TestJNIInvoke.cpp:63:12: error: base operand of '->' has non-pointer type 'JNIEn
v'
TestJNIInvoke.cpp:66:16: error: base operand of '->' has non-pointer type 'JNIEn
v'
TestJNIInvoke.cpp:67:16: error: base operand of '->' has non-pointer type 'JNIEn
v'
TestJNIInvoke.cpp:69:12: error: base operand of '->' has non-pointer type 'JavaVM'
Any ideas?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使包含相同的头文件,Java 本机接口也对 C 和 C++ 使用两种不同的接口。
在 C++ 中,它是:
而不是(对于 C):
因此,在两个地方需要
env
的 C 函数调用就变成了 C++ 中方便的成员函数调用。这在本机方法参数部分中提到Java 本机接口 6.0 规范。
Even though you include the same header file, the Java Native Interface uses two different interfaces for C and C++.
In C++, it's:
instead of (for C):
So the C function call that requires
env
in two places becomes a convenient member function call in C++.This is mentioned in the Native Method Arguments section of the Java Native Interface 6.0 Specification.
我的猜测是,您正在尝试根据您正在使用的命令行来编译 win32 标头。您是否尝试过
-I"c:\ProgramFiles\Java\jdk1.7.0\include\solaris
(假设这是您的平台,基于源代码中较高的注释)。My guess would be that you're attempting to compile against the win32 headers given the command line you're using. Have you tried
-I"c:\ProgramFiles\Java\jdk1.7.0\include\solaris
instead (assuming that's your platform based on the comment higher up in the source).