Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #7128
| From | Danno <danmwall-google@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: JNI accessing a class that instantiates another class |
| Date | 2011-08-15 12:28 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <391b55eb-b8cc-413e-9c58-21ce7e81b934@v7g2000vbk.googlegroups.com> (permalink) |
| References | <d48afffd-0ff8-4cc4-a63b-45ffe01224e9@o11g2000yql.googlegroups.com> <871uwma9kd.fsf@mid.deneb.enyo.de> |
On Aug 15, 1:06 pm, Florian Weimer <f...@deneb.enyo.de> wrote:
> * Danno:
>
> > I'm taking my first stab at JNI. I have a main Java class that
> > instantiates another class. In my C++ code, I am using the FindClass
> > function for the main class and calling the method in the main class
> > using GetStaticMethodId/CallStaticIntMethod. The C++ code is
> > successfully finding the Java class and calling the method that I
> > specified in the GetStaticMethoId function. The only problem is, when
> > the Java class attempts to create an instance of this second, inner
> > class, it dies. I don't get an error, it just doesn't work.
>
> Please post code demonstrating the problem. Have you checked if
> -Xcheck:jni reports anything useful?
Thank you for taking the time to reply to my post. I have attached my
code below. I have not used -Xcheck:jni before. I may not fully
understand how to use it, but the way I understand it, it is a command
line argument. The java program, however, is not being called from a
command line. I have a MicroFocus COBOL program that is calling a C++
dll that starts a JVM and invokes a Java method via JNI. Is there a
way to run -Xcheck:jni under that scenario?
Here is my code...
C++:
#include <jni.h>
#include <windows.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include "IGVIEWER_JNI.H"
using namespace std;
bool initJNI() {
JNILoaded = false;
JavaVMOption options[1];
JavaVMInitArgs vm_args;
long status;
options[0].optionString = "-Djava.class.path=.";
memset(&vm_args, 0, sizeof(vm_args));
vm_args.version = JNI_VERSION_1_4;
vm_args.nOptions = 1;
vm_args.options = options;
status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
if (status == 0) {
cls = env->FindClass("IgViewerJNI");
if(cls ==0) {
jvm->DestroyJavaVM();
return false;
} else {
JNILoaded = true;
return true;
}
}
else {
return false;
}
}
extern ULONG WINAPI IMGINIT_JNI () {
jmethodID mid;
bool bResults;
if (!JNILoaded) {
bResults = initJNI();
if (!bResults) {
return 8;
}
}
mid = env->GetStaticMethodID(cls, "initializeViewer", "()I");
if(mid !=0) {
return env->CallStaticIntMethod(cls, mid);
} else {
return 8;
}
}
Java:
import javax.swing.JOptionPane;
public class IgViewerJNI {
public static int initializeViewer(){
try {
// ** THIS IS WHERE IT DIES - I didn't include
the IgViewerInit class code since it seemed unnecessary.
IgViewerInit viewInit = new IgViewerInit();
return viewInit.initializeViewer();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error - "
+ e.getMessage());
return 8;
}
}
public static void main(String args[]) {
}
}
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
JNI accessing a class that instantiates another class Danno <danmwall-google@yahoo.com> - 2011-08-15 07:55 -0700
Re: JNI accessing a class that instantiates another class Florian Weimer <fw@deneb.enyo.de> - 2011-08-15 19:06 +0200
Re: JNI accessing a class that instantiates another class Danno <danmwall-google@yahoo.com> - 2011-08-15 12:28 -0700
Re: JNI accessing a class that instantiates another class Florian Weimer <fw@deneb.enyo.de> - 2011-08-15 21:50 +0200
Re: JNI accessing a class that instantiates another class Roedy Green <see_website@mindprod.com.invalid> - 2011-08-15 15:10 -0700
csiph-web