static void Main(string[] args) { string codeBase = Assembly.GetExecutingAssembly().CodeBase; UriBuilder uri = new UriBuilder(codeBase); string workingDir = Path.GetDirectoryName(Uri.UnescapeDataString(uri.Path)) + Path.DirectorySeparatorChar; // Instantiate the JNI interface assembly JavaNativeInterface jni = new JavaNativeInterface(); Dictionary <string, string> options = new Dictionary <string, string>(); // Setting the class path to the jar that containes the classes to use options.Add("-Djava.class.path", workingDir + "target\\SampleJavaApplication-0.0.1-SNAPSHOT.jar"); // If your jar need other jars as dependencies, you may need to add them in the classpath : // + ";" + workingDir + "target\\dependency.jar"); // Load a new JVM jni.LoadVM(options, false); try { IntPtr SampleApplicationClass = IntPtr.Zero; IntPtr SampleApplicationObject = jni.InstantiateJavaObject("org/daisy/jnet/SampleApplication", out SampleApplicationClass); string testString = jni.CallMethod <string>(SampleApplicationClass, SampleApplicationObject, "getTestString", "()Ljava/lang/String;"); Console.WriteLine(testString); SampleApplicationObject = jni.InstantiateJavaObject("org/daisy/jnet/SampleApplication", out SampleApplicationClass, "(Ljava/lang/String;)V", "I'm a string sent from C#"); testString = jni.CallMethod <string>(SampleApplicationClass, SampleApplicationObject, "getTestString", "()Ljava/lang/String;"); Console.WriteLine(testString); } catch (Exception e) { Console.WriteLine(e.ToString()); } }
static void Main(string[] args) { // JVM Log option Params Dictionary Dictionary<string, string> jvmParameters = new Dictionary<string, string>(); JavaNativeInterface Java = new JavaNativeInterface(); // Set class path string strKey = "-Djava.class.path"; string strValue = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); jvmParameters.Add(strKey, strValue); Java.LoadVM(jvmParameters, false); // Instance Create: HelloWorld Java.InstantiateJavaObject("HelloWorld"); // Print java version System.Console.WriteLine(Java.JavaVersion()); // Params lists for JNI List<object> jniParam = new List<object>(); // Call HelloWorldProcedure() jniParam.Add("WestWoodForever"); Java.CallVoidMethod("HelloWorldProcedure", "(Ljava/lang/String;)V", jniParam); // Call AddTwoNumbers() jniParam.Clear(); jniParam.Add(2); jniParam.Add(5); jniParam.Add("WestWoodForever"); string returnJNI = Java.CallMethod<int>("AddTwoNumbers", "((IILjava/lang/String;)I", jniParam).ToString(); System.Console.WriteLine(returnJNI); Java.Dispose(); }
private void btLoadJVM_Click(object sender, EventArgs e) { Java = new JavaNativeInterface(); // Set the path where the java class is located AddJVMOption("-Djava.class.path", Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)); Java.LoadVM(pair, false); Java.InstantiateJavaObject(tbClassName.Text); statusStrip1.Items[0].Text = "JVM Loaded and class instantiated"; }
static void Main(string[] args) { // JVM Log option Params Dictionary Dictionary <string, string> jvmParameters = new Dictionary <string, string>(); JavaNativeInterface Java = new JavaNativeInterface(); // Set class path string strKey = "-Djava.class.path"; string strValue = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); jvmParameters.Add(strKey, strValue); Java.LoadVM(jvmParameters, false); // Instance Create: HelloWorld Java.InstantiateJavaObject("HelloWorld"); // Print java version System.Console.WriteLine(Java.JavaVersion()); // Params lists for JNI List <object> jniParam = new List <object>(); // Call HelloWorldProcedure() jniParam.Add("WestWoodForever"); Java.CallVoidMethod("HelloWorldProcedure", "(Ljava/lang/String;)V", jniParam); // Call AddTwoNumbers() jniParam.Clear(); jniParam.Add(2); jniParam.Add(5); jniParam.Add("WestWoodForever"); string returnJNI = Java.CallMethod <int>("AddTwoNumbers", "((IILjava/lang/String;)I", jniParam).ToString(); System.Console.WriteLine(returnJNI); Java.Dispose(); }
public void AutoOpen() { try { RegisterFunctions(); IntelliSenseServer.Install(); AppDomain cDomain = AppDomain.CurrentDomain; pAssemblyLocation = cDomain.BaseDirectory; //Even though jar tvf <> can expose the classes of a jar ExcelDna-packed into an assembly //javap might not be able to find the class, so holding on.... // //Here, we will first try to use the concept that a C# assembly is like a Java jar // //and the ExcelDNA packer can include the core jar (note there can be no lzma compression // //which might required an old-style ExcelDNA Post-Build line). // String pWDSJniPMML = Assembly.GetExecutingAssembly().FullName; // pWDSJniPMML = pAssemblyLocation+"\\" // +pWDSJniPMML.Substring(0, pWDSJniPMML.IndexOf(", Version") - 1) + "-AddIn64-packed.xll"; //The dependency on slf4j-simple has been packaged into the jar, this //enables finding all of the HDF5 dependencies from the HDFView location. //The jarhdf[5] and hdfobject jars still depend on external libs/dlls //that should be on the path. However, locations can be passed in on //the Excel command line with /o:WDS:<libname>:<liblocation> String pWDSJniPMML = pAssemblyLocation + "\\WDS-JniPMML-0.5.0.jar"; { DirectoryInfo aDirectoryInfo = new DirectoryInfo(pAssemblyLocation); FileInfo[] lFileInfo = aDirectoryInfo.GetFiles("WDS*JniPMML*.jar"); if (lFileInfo == null || lFileInfo.Length == 0) { pWDSJniPMML = pAssemblyLocation + "\\Resources\\WDS-JniPMML-0.5.0.jar"; } } String pjarhdf5dir = ""; String pjarhdf5 = ""; String pjarhdfdir = ""; String pjarhdf = ""; String phdfobjectdir = ""; String phdfobject = ""; //String pslf4j = ""; String pHDFView = "Unk"; String pHDF5 = "Unk"; //parse out the command line arguments in case /o:WDS - options are being used String[] args = System.Environment.GetCommandLineArgs(); for (int i = 0; i < args.Length; i++) { if (args[i].StartsWith("/o:WDS:WDS-JniPMML")) { String[] parts = args[i].Split(':'); pWDSJniPMML = parts[parts.Length - 1]; } else if (args[i].StartsWith("/o:WDS:jarhdf5")) { String[] parts = args[i].Split(':'); pjarhdf5 = parts[parts.Length - 1]; } else if (args[i].StartsWith("/o:WDS:jarhdf")) { String[] parts = args[i].Split(':'); pjarhdf = parts[parts.Length - 1]; } else if (args[i].StartsWith("/o:WDS:hdfobject")) { String[] parts = args[i].Split(':'); phdfobject = parts[parts.Length - 1]; } //else if ( args[i].StartsWith("/o:WDS:slf4j") ) { //String[] parts = args[i].Split(':'); //pslf4j = parts[parts.Length - 1]; //} else if (args[i].StartsWith("/o:WDS:path:HDFView")) { String[] parts = args[i].Split(':'); pHDFView = parts[parts.Length - 1]; } else if (args[i].StartsWith("/o:WDS:path:HDF5")) { String[] parts = args[i].Split(':'); pHDF5 = parts[parts.Length - 1]; } } //if not provided with command lines, check the paths if (pHDFView.Equals("Unk") || pHDF5.Equals("Unk")) { //pull the locations for HDFView and HDF5 from the system path String p = System.Environment.GetEnvironmentVariable("PATH"); if (pHDFView.Equals("Unk")) { pHDFView = PathElementOf(p, "HDFView") + "\\lib"; } if (pHDF5.Equals("Unk")) { pHDF5 = PathElementOf(p, "HDF5\\1") + "\\lib"; } } if (pWDSJniPMML == "") { DirectoryInfo aDirectoryInfo = new DirectoryInfo(pAssemblyLocation); FileInfo[] lFileInfo = aDirectoryInfo.GetFiles("WDS*JniPMML*.jar"); pWDSJniPMML = lFileInfo[0].FullName; } if (pjarhdf5 == "") { //Use the jarhdf from the HDFView location first //DirectoryInfo aDirectoryInfo = new DirectoryInfo(pHDF5); DirectoryInfo aDirectoryInfo = new DirectoryInfo(pHDFView); FileInfo[] lFileInfo = aDirectoryInfo.GetFiles("jarhdf5*.jar"); pjarhdf5 = lFileInfo[0].FullName; pjarhdf5dir = System.IO.Path.GetDirectoryName(pjarhdf5); } if (pjarhdf == "") { //DirectoryInfo aDirectoryInfo = new DirectoryInfo(pHDF5); DirectoryInfo aDirectoryInfo = new DirectoryInfo(pHDFView); FileInfo[] lFileInfo = aDirectoryInfo.GetFiles("jarhdf-*.jar"); pjarhdf = lFileInfo[0].FullName; pjarhdfdir = System.IO.Path.GetDirectoryName(pjarhdf5); } //if ( pslf4j == "" ) { // //the slf4j-simple should be with HDF5 // //DirectoryInfo aDirectoryInfo = new DirectoryInfo(pHDFView); // //FileInfo[] lFileInfo = aDirectoryInfo.GetFiles("slf4j-api*.jar"); // DirectoryInfo aDirectoryInfo = new DirectoryInfo(pHDF5); // FileInfo[] lFileInfo = aDirectoryInfo.GetFiles("slf4j-simple*.jar"); // pslf4j = lFileInfo[0].FullName; //} if (phdfobject == "") { DirectoryInfo aDirectoryInfo = new DirectoryInfo(pHDFView); FileInfo[] lFileInfo = aDirectoryInfo.GetFiles("hdfobj*.jar"); phdfobject = lFileInfo[0].FullName; phdfobjectdir = System.IO.Path.GetDirectoryName(phdfobject); } java_class_path = pWDSJniPMML + ";" + phdfobject + ";" + pjarhdf5 + ";" + pjarhdf //+ ";" + pslf4j ; java_module_path = phdfobjectdir; if (!java_module_path.Contains(pjarhdf5dir)) { java_module_path += pjarhdf5dir; } if (!java_module_path.Contains(pjarhdfdir)) { java_module_path += pjarhdfdir; } if (!pair.ContainsKey("-Djava.class.path")) { pair.Add("-Djava.class.path", java_class_path); } //java_module = "hdfobject;jarhdf5;jarhdf;slf4j.simple"; //java_module = "hdfobject;jarhdf5;jarhdf"; //if ( !pair.ContainsKey("--add-modules") ) //pair.Add("--add-modules", java_module); if (!pair.ContainsKey("-Djava.module.path")) { pair.Add("-Djava.module.path", java_module_path); } if (!pair.ContainsKey("-Djava.library.path")) { pair.Add("-Djava.library.path", pHDFView + ";" + pHDF5); } //if (!pair.ContainsKey("-D64")) pair.Add("-D64", "" ); if (!pair.ContainsKey("-Xcheck:jni ")) { pair.Add("-Xcheck:jni ", ""); } //initialize JNI object Java = new JavaNativeInterface(); Java.LoadVM(pair, false); Java.InstantiateJavaObject(java_init_class_name); java_init_classid = Java.FindClassID(java_init_class_name); __JniPMML = new com.WDataSci.JniPMML.JniPMML(Java, java_init_classid); } catch (Exception e) { throw new com.WDataSci.WDS.WDSException("Error into AutoLoad", e); } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); }