JNI and Delphi Issue

Hello, I am trying to link a Java application to some Modula-2 DLLs and I am using a wrapper I have written in Delphi. I have downloaded the Delphi unit JNI.pas from http://home.pacifier.com/~mmead/jni/delphi/JEDI/DOCS/delphi-jni-1.html -> this contains methods to connect Delphi and Java.

I have Java code that I have looks like this:

Java Code:

publicclass DLLTest{

public DLLTest(){

}

publicnative String getName();

static{

System.loadLibrary("c:\\saphire8\\HelloWorldImpl.dll");

}

publicstaticvoid main(String args[]){

DLLTest t =new DLLTest();

System.out.println(t.getName());//Just gets a name from some database

}

}

Delphi code (dll)

library HelloWorldImpl

uses

ShareMem,Dialogs,JNI,Classes,API_Risk,SageRiskUtil;

//Just some libaries I use, JNI is the one mentioned above

var

JVM: TJNIEnv;

procedure Ext_Get_Name(var name : API_Risk.NameType); stdcall;

external'sagerisk.dll' name'Project_Get_Name';

//The type API_Risk.NameType is just a character array of size 25

function Java_DLLTest_getName(PEnv: PJNIEnv; Obj: JObject):JString;

var

name : API_Risk.NameType;

name1: String;

name2: JString;

begin

JVM:=TJNIEnv.Create(PEnv);

Ext_Get_Name(name);

CharArraytoString(name,name1);//This is defined in one of those libaries

name2:=JVM.StringToJString(name);//Defined in JNI.pas

Result:= name2;

end;

exports

Java_DLLTest_getName;

end.

I place these things in the proper directories and I run...I get the following error:

#

# An unexpected error has been detected by HotSpot Virtual Machine:

#

# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x02ca2bde, pid=5400, tid=5160

#

# Java VM: Java HotSpot(TM) Client VM (1.5.0_08-b03 mixed mode, sharing)

# Problematic frame:

# C [HelloWorldImpl.dll+0x52bde]

#

# An error report file with more information is saved as hs_err_pid5400.log

#

# If you would like to submit a bug report, please visit:

#http://java.sun.com/webapps/bugreport/crash.jsp

#

The Java connects to the .dll just fine, I have verified that.

That variable JVM is an instance of the Java Native Interface in Delphi. If I leave that out and don't try and convert that string, just print out what I get from the Ext_get_Name name function with Delphi's ShowMessage(string), I do not get that error. If I even call up an instance of JVM with

TJNIEnv.Create(PEnv) and don't do anything with it, it gives me the same error. I have no idea what is causing this problem. If anyone has any familiarity with Delphi and Java I would love some input. Thanks in advance.

[3620 byte] By [jork_berfkina] at [2007-10-3 2:35:41]
# 1
Presumably you have tested that delphi code will call your modula code.The only suggestion I can make is that the delphi generator was intended to work with a specific version of java. And you are not using that version.
jschella at 2007-7-14 19:34:30 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

Yes, I have ensured that the Delphi is correctly calling the Modula. I will look into the version issue. Thanks.

Also, I noticed something else in my code that was peculiar. I actually have a second test method in there that looks like this (in the Delphi):

procedure Java_DLLTest_displayCustomText(PEnv: PJNIEnv; Obj: JObject; text: JString);

{$IFDEF WIN32} stdcall; {$ENDIF}

{$IFDEF LINUX} cdecl; {$ENDIF}

var

name: String;

JVM:TJNIEnv;

begin

JVM:=TJNIEnv.Create(PEnv);

name:=JVM.JStringtoString(text);

JVM.FreeInstance;

ShowMessage(name);

end;

You can see that I call the JVM like before. This time I called this function first and it worked. Then I called the getName one and if gave me the error in my first post. Actually, now that i am looking at my code, I call it one more time. So I call the JVM variable twice before I call it in the getName and it does not crash until getName. Note that I call it the same way both times with the JVM.FreeInstance at the end.

Message was edited by:

jork_berfkin

jork_berfkina at 2007-7-14 19:34:30 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

see my webpage, it has a modified JNI.pas file and an example project.

What you should look at in the delphi example is how to try-catch all native exceptions and wrap to java.lang.Exception.

Then run you java test code and see what java exceptions says, it may give you some information to track it down.

[url]http://koti.mbnet.fi/akini/java/jni/[/url]

whome0a at 2007-7-14 19:34:30 > top of Java-index,Java HotSpot Virtual Machine,Specifications...