Java or .NET? Why not both (when it is the only viable path)?
jni4net is a proven interop library for Java and .NET. Two brief examples developed by jni4net below merely require that you to specify the jni4net dependency in the (Visual Studio or Eclipse) project.
Calling Java from .NET
using java.io;
using java.lang;
using java.util;
using net.sf.jni4net;
using net.sf.jni4net.adaptors;
namespace helloWorldFromCLR
{
public class Program
{
private static void Main()
{
// create bridge, with default setup
// it will lookup jni4net.j.jar next to jni4net.n.dll
Bridge.CreateJVM(new BridgeSetup(){Verbose=true});
// here you go!
java.lang.System.@out.println("Hello Java world!");
// OK, simple hello is boring, let's play with Java properties
// they are Hashtable realy
Properties javaSystemProperties = java.lang.System.getProperties();
// let's enumerate all keys.
// We use Adapt helper to convert enumeration from java o .NET
foreach (java.lang.String key in Adapt.Enumeration(javaSystemProperties.keys()))
{
java.lang.System.@out.print(key);
// this is automatic conversion of CLR string to java.lang.String
java.lang.System.@out.print(" : ");
// we use the hashtable
Object value = javaSystemProperties.get(key);
// and this is CLR ToString() redirected to Java toString() method
string valueToString = value.ToString();
java.lang.System.@out.println(valueToString);
}
// Java output is really Stream
PrintStream stream = java.lang.System.@out;
// it implements java.io.Flushable interface
Flushable flushable = stream;
flushable.flush();
}
}
}
Calling .NET from Java
import net.sf.jni4net.Bridge;
import java.io.IOException;
import java.lang.String;
import system.*;
import system.Object;
import system.io.TextWriter;
import system.collections.IDictionary;
import system.collections.IEnumerator;
public class Program {
public static void main(String[] args) throws IOException {
// create bridge, with default setup
// it will lookup jni4net.n.dll next to jni4net.j.jar
Bridge.setVerbose(true);
Bridge.init();
// here you go!
Console.WriteLine("Hello .NET world!\n");
// OK, simple hello is boring, let's play with System.Environment
// they are Hashtable realy
final IDictionary variables = system.Environment.GetEnvironmentVariables();
// let's enumerate all keys
final IEnumerator keys = variables.getKeys().GetEnumerator();
while (keys.MoveNext()) {
// there hash table is not generic and returns system.Object
// but we know is should be system.String, so we could cast
final system.String key = (system.String) keys.getCurrent();
Console.Write(key);
// this is automatic conversion of JVM string to system.String
Console.Write(" : ");
// we use the hashtable
Object value = variables.getItem(key);
// and this is JVM toString() redirected to CLR ToString() method
String valueToString = value.toString();
Console.WriteLine(valueToString);
}
// Console output is really TextWriter on stream
final TextWriter writer = Console.getOut();
writer.Flush();
}
}
(verbose commenting by Pavel Savara, a jni4net contributor)
References:
http://zamboch.blogspot.com/2009/10/how-calling-from-net-to-java-works.html
http://zamboch.blogspot.com/2009/11/how-calling-from-java-to-net-works-in.html
https://github.com/jni4net/jni4net/tree/master/content/samples
No comments:
Post a Comment