1

I have used JNI4NET framework to invoke .Net code from Java. JNI4NET has generated proxy class for .Net code. One of the methods accept system.Object as an input parameter.

I want to send String value as input to that method. I have wrote the below code for that-

        String s = "test";
        Object b = s;
        system.Object object = (system.Object) b;

And passing this obj as an input to proxy method. The above code throws runtime exception java.lang.String cannot be cast to system.Object. Any help or pointers will be useful.

1 Answers1

2

java.lang.String can be cast to java.lang.Object (though there's no reason to cast it - you can simply assign it to an Object reference as you do in - Object b = s;), not to system.Object.

I did some searching. It looks like this might help :

String s = "test";
system.String b = new system.String(s);
system.Object object = b;

Since system.String is a sub-class of system.Object, you can pass it to your test method.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Thanks for the quick response Eran. In this case, how can pass a String value to system.Object parameter ? –  Jan 19 '15 at 08:50
  • @Sanjay What is `system.Object`? Is it a class you created? What constructors does it have? – Eran Jan 19 '15 at 08:51
  • Its a .Net equivalent method created by JNI4NET framework in Java. Below is the syntax- public native boolean test(java.lang.String AVariableFullName, system.Object Value); –  Jan 19 '15 at 08:54
  • Second parameter of this native method accepts only system.Object value. I want to pass String value. –  Jan 19 '15 at 08:55
  • Thanks a lot Eran. This looks closure to the solution. However I am getting Type mismatch: cannot convert from java.lang.String to system.String When trying to pass String value while creating system.String –  Jan 19 '15 at 09:05
  • Eran, still getting Type mismatch: cannot convert from java.lang.String to system.String, while creating system.String –  Jan 19 '15 at 09:09
  • I tried the below code- system.String b = new String(s); system.Object object = b; return tracer.WriteVariable(variableName, object); –  Jan 19 '15 at 09:17
  • 1
    shouldn't it be `system.String b = new system.String(s);`? – Tom Jan 19 '15 at 09:18
  • yes that works. But it gives runtime exception Exception in thread "main" java.lang.UnsatisfiedLinkError: net.sf.jni4net.Bridge.Convert(Ljava/lang/String;)J –  Jan 19 '15 at 09:18
  • The above solution allow me to pass String value at compile, it throws runtime exception :-( –  Jan 19 '15 at 09:28
  • 1
    @Sanjay Perhaps you are missing some jar in your library path when you run the code. – Eran Jan 19 '15 at 09:30