Calling .NET Method in Java Class with String Input and Output

779 views
Skip to first unread message

sebastianr

unread,
Mar 5, 2012, 7:24:49 AM3/5/12
to jni4net
Hi,

i think its a simple Problem which i'd like to resolve and i found
jni4net which maybe the right technology for a solution. But i didn't
get my "project" to run.
What i'd like to do is calling my .NET Function "Public Function
Main(ByVal xmlstring As String) As String" (VB.NET: String = xmlstring
Input and As String = return value also string type) out of a java
class:
public static void main(String arsg[]) throws IOException {
Bridge.init();
Bridge.LoadAndRegisterAssemblyFrom(new java.io.File("StartDll.dll"));
}
But here i got stuck and didn't know how to get ahead. So i tried it
with the proxygen.exe which brought me 2 Files from my .NET Dll:
in clr a file with .generated.cs ending and in jvm a .java Class File.
I start debug and get:
Exception in thread "main" java.lang.UnsatisfiedLinkError:
execall.StartWrapper.__ctorStartWrapper0(Lnet/sf/jni4net/inj/
IClrProxy;)V
at execall.StartWrapper.__ctorStartWrapper0(Native Method)
at execall.StartWrapper.<init>(StartWrapper.java:16)
at execall.Execall.main(Execall.java:11)
I hope someone could help me or can offer me another idea how to solve
this problem.
Greetings
Sebastian

Pavel Šavara

unread,
Mar 6, 2012, 4:03:42 AM3/6/12
to jni...@googlegroups.com
Show us the code which calls StartWrapper constructor.


sebastianr

unread,
Mar 6, 2012, 4:41:13 AM3/6/12
to jni4net
Hi again,

the code above is wrong i know now, i am now at this point:

i generated the java code with proxygen:

package test;

@net.sf.jni4net.attributes.ClrType
public class StartWrapper extends system.Object {

//<generated-proxy>
private static system.Type staticType;

protected StartWrapper(net.sf.jni4net.inj.INJEnv __env, long
__handle) {
super(__env, __handle);
}

@net.sf.jni4net.attributes.ClrConstructor("()V")
public StartWrapper() {
super(((net.sf.jni4net.inj.INJEnv)(null)), 0);
test.StartWrapper.__ctorStartWrapper0(this);
}

@net.sf.jni4net.attributes.ClrMethod("()V")
private native static void
__ctorStartWrapper0(net.sf.jni4net.inj.IClrProxy thiz);

@net.sf.jni4net.attributes.ClrMethod("(LSystem/String;)LSystem/
String;")
public native java.lang.String Main(java.lang.String xmlstring);

public static system.Type typeof() {
return test.StartWrapper.staticType;
}

private static void InitJNI(net.sf.jni4net.inj.INJEnv env,
system.Type staticType) {
test.StartWrapper.staticType = staticType;
}
//</generated-proxy>
}

Then i import this to my java Main:

package test;

import java.io.IOException;
import net.sf.jni4net.Bridge;
import test.StartWrapper;

public class MyClass {

public static void main(String arsg[]) throws IOException {
Bridge.init();
Bridge.LoadAndRegisterAssemblyFrom(new
java.io.File("StartDll.dll"));
StartWrapper sw = new StartWrapper();
String answer = sw.Main("Test");
}

}

So now i have the same error but in debug i have more steps before
getting the error above. At one point he says "ClassNotFoundException"
but there is no more info in eclipse.

I hope this is the code you wanted to see.

Greetings

On Mar 6, 10:03 am, Pavel Šavara <pavel.sav...@gmail.com> wrote:
> Show us the code which calls StartWrapper constructor.
>
> On Mon, Mar 5, 2012 at 1:24 PM, sebastianr
> <sebastian.roem...@datatronic.de>wrote:

Pavel Šavara

unread,
Mar 6, 2012, 6:18:42 AM3/6/12
to jni...@googlegroups.com
We could not get any further without full exception information. What class ? What stack trace ?

sebastianr

unread,
Mar 6, 2012, 7:35:49 AM3/6/12
to jni4net
Okay i think you request this Stack:

Thread [main] (Suspended)
ClassNotFoundException(Object).<init>() line: 37
ClassNotFoundException(Throwable).<init>(String, Throwable) line:
286
ClassNotFoundException(Exception).<init>(String, Throwable) line: not
available
ClassNotFoundException(ReflectiveOperationException).<init>(String,
Throwable) line: not available
ClassNotFoundException.<init>(String) line: not available
URLClassLoader$1.run() line: not available
URLClassLoader$1.run() line: not available
AccessController.doPrivileged(PrivilegedExceptionAction<T>,
AccessControlContext) line: not available [native method]
Launcher$ExtClassLoader(URLClassLoader).findClass(String) line: not
available
Launcher$ExtClassLoader(ClassLoader).loadClass(String, boolean) line:
not available
Launcher$AppClassLoader(ClassLoader).loadClass(String, boolean) line:
not available
Launcher$AppClassLoader.loadClass(String, boolean) line: not
available
Launcher$AppClassLoader(ClassLoader).loadClass(String) line: not
available
MyClass.main(String[]) line: 12

That comes up when calling "new StartWrapper();" in MyClass.java.
Then i come to this code line in StartWrapper.java:
test.StartWrapper.__ctorStartWrapper0(this);
And get that Trace:
Thread [main] (Suspended)
UnsatisfiedLinkError(Object).<init>() line: 37
UnsatisfiedLinkError(Throwable).<init>(String) line: 264
UnsatisfiedLinkError(Error).<init>(String) line: 70
UnsatisfiedLinkError(LinkageError).<init>(String) line: 55
UnsatisfiedLinkError.<init>(String) line: not available
StartWrapper.__ctorStartWrapper0(IClrProxy) line: not available
[native method]
StartWrapper.<init>() line: 25
MyClass.main(String[]) line: 12

The Console Output of eclipse is this:
Exception in thread "main" java.lang.UnsatisfiedLinkError:
test.StartWrapper.__ctorStartWrapper0(Lnet/sf/jni4net/inj/IClrProxy;)V
at test.StartWrapper.__ctorStartWrapper0(Native Method)
at test.StartWrapper.<init>(StartWrapper.java:25)
at test.MyClass.main(MyClass.java:12)
ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return
code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [../../../src/share/back/
util.c:838]

I've put the StartWrapper.java in src/test/.

On Mar 6, 12:18 pm, Pavel Šavara <pavel.sav...@gmail.com> wrote:
> We could not get any further without full exception information. What class
> ? What stack trace ?
>
> On Tue, Mar 6, 2012 at 10:41 AM, sebastianr <sebastian.roem...@datatronic.de

Pavel Šavara

unread,
Mar 6, 2012, 8:19:28 AM3/6/12
to jni...@googlegroups.com
What is your full platform info ? Versions etc

sebastianr

unread,
Mar 6, 2012, 8:48:12 AM3/6/12
to jni4net
- Windows 7 Ultimate SP1 32bit

- java version "1.7.0_03"
Java(TM) SE Runtime Environment (build 1.7.0_03-b05)
Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode, sharing)

- .NET 4.0 v4.0.30319

- jni4net 0.8.6.0



On Mar 6, 2:19 pm, Pavel Šavara <pavel.sav...@gmail.com> wrote:
> What is your full platform info ? Versions etc
>
> On Tue, Mar 6, 2012 at 1:35 PM, sebastianr
> <sebastian.roem...@datatronic.de>wrote:

Pavel Šavara

unread,
Mar 6, 2012, 9:00:10 AM3/6/12
to jni...@googlegroups.com
Hmmm, strange. I didn't see this message before

"ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183):  [../../../src/share/back/util.c:838]"


sebastianr

unread,
Mar 6, 2012, 10:52:45 AM3/6/12
to jni4net
The error seems to be not related to my error(http://bugs.sun.com/
view_bug.do?bug_id=6476706) that says
Exception in thread "main" java.lang.UnsatisfiedLinkError.

In other forums this errormessage is related to "function not found",
so the dll is loaded but the function can't be found.
But i don't know where the problem is that he cannot find the class.
Could i debug through the sources of jni also? Maybe the name of the
class in the proxy is different or something?

On Mar 6, 3:00 pm, Pavel Šavara <pavel.sav...@gmail.com> wrote:
> Hmmm, strange. I didn't see this message before
>
> "ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code =
> -2
> JDWP exit error AGENT_ERROR_NO_JNI_ENV(183):  [../../../src/share/back/
> util.c:838]"
>
> On Tue, Mar 6, 2012 at 2:48 PM, sebastianr
> <sebastian.roem...@datatronic.de>wrote:
Reply all
Reply to author
Forward
0 new messages