Simple Java deserialization in C#

117 views
Skip to first unread message

Bart Verstrynge

unread,
Dec 15, 2016, 5:19:17 AM12/15/16
to jni4net
I stumbled upon jni4net yesterday and it looks like the perfect solution to our nasty problem. We have a serialized Java class that we need some data from, inside a C# codebase. I have the original Java .class files, but it looks like when the Java process is actually trying to do read the object from the serialized data, it can't find this .class definition.

var bridgeSetup = new BridgeSetup();
bridgeSetup.AddClassPath("TestData.class");
Bridge.CreateJVM(bridgeSetup);

var s = new java.io.ObjectInputStream(new FileInputStream("file.dat"));
var v = s.readObject();

The JVM will marshall an exception through saying 'java.lang.ClassNotFoundException' on the class TestData, which should be loaded correctly (I would think?)

What could I be missing here?

Thanks,

Bart

Pavel Šavara

unread,
Dec 15, 2016, 5:26:36 AM12/15/16
to jni4net
the class path should be pointing to folder, which is root of namespace represented by nested folders. 
Your .class file need to be inside the namespace folder as it was it's compiled full name.
Such full name would be in your data to deserialize, I guess.
Eventually jar file is just zip with same folder-namespace structure.

Another thought, I seems to me that for this scenario IKVM project may be better choice of technology. It would have smoother deployment story.

Pavel

--
--
You received this message because you are subscribed to jni...@googlegroups.com
http://groups.google.com/group/jni4net?hl=en-GB?hl=en-GB
http://jni4net.sf.net/

---
You received this message because you are subscribed to the Google Groups "jni4net" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jni4net+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bart Verstrynge

unread,
Dec 15, 2016, 5:53:23 AM12/15/16
to jni4net
Thanks for the quick response!

I'm indeed getting a fully qualified class name that is not found; i.e. "com/a/b/TestData.class"

I also tried defining the original jar or even its folder root, i.e.

bridgeSetup.AddClassPath("test.jar");
// or
bridgeSetup
.AddClassPath("test"); // extracted from the .jar

Any of those will give me the same warning at the same location (when actual deserialization is applied). 

I also stumbled upon IKVM, but if I get jni4net to work it looks like a cleaner solution to me.

Op donderdag 15 december 2016 11:26:36 UTC+1 schreef Pavel Savara:
To unsubscribe from this group and stop receiving emails from it, send an email to jni4net+u...@googlegroups.com.

Pavel Šavara

unread,
Dec 15, 2016, 6:29:11 AM12/15/16
to jni4net
the class file may have references other classes, which may prevent it from loading. Also make sure that your java version is compatible with the class file version.

To unsubscribe from this group and stop receiving emails from it, send an email to jni4net+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages