public void Invoke(params string[] args) { try { // http://bigjavablog.blogspot.com/2008/08/load-jar-files-and-java-classes.html if (Method != null) { var main_args = new string[0]; this.Method.invoke(null, new object[] { main_args }); } else { /* We need and URL to load the jar file. */ URL u = new File(this.AssemblyPath).toURL(); /* Load jar file using URLClassLoader. */ URLClassLoader cl = new URLClassLoader(new URL[] { u }); if (NativeAssemblyPath != null) { var nc = cl.loadClass("jni.CPtrLibrary"); var ncf = nc.getField("LibraryPath"); ncf.set(null, NativeAssemblyPath); } var c = cl.loadClass(this.TypeName); foreach (var m in c.getMethods()) { if (m.getDeclaringClass() == c) { if (m.getName() == "main") { this.Method = m; var main_args = new string[0]; this.Method.invoke(null, new object[] { main_args }); } } } } } catch (csharp.ThrowableException ex) { var exo = (object)ex; Console.WriteLine("error!"); Console.WriteLine(ex.Message + " - " + ex.ToString()); ErrorHandler(exo); } }
public string execClass(string className) { if (TEST_IN_SAME_PROCESS) { try { ClassLoader loader = new URLClassLoader(new URL[] { new File(tmpdir).toURI().toURL() }, ClassLoader.getSystemClassLoader()); Class mainClass = (Class)loader.loadClass(className); Method mainMethod = mainClass.getDeclaredMethod("main", typeof(string[])); PipedInputStream stdoutIn = new PipedInputStream(); PipedInputStream stderrIn = new PipedInputStream(); PipedOutputStream stdoutOut = new PipedOutputStream(stdoutIn); PipedOutputStream stderrOut = new PipedOutputStream(stderrIn); StreamVacuum stdoutVacuum = new StreamVacuum(stdoutIn); StreamVacuum stderrVacuum = new StreamVacuum(stderrIn); PrintStream originalOut = Console.Out; System.setOut(new PrintStream(stdoutOut)); try { PrintStream originalErr = System.err; try { System.setErr(new PrintStream(stderrOut)); stdoutVacuum.start(); stderrVacuum.start(); mainMethod.invoke(null, (Object) new string[] { new File(tmpdir, "input").getAbsolutePath() }); } finally { System.setErr(originalErr); } } finally { System.setOut(originalOut); } stdoutOut.close(); stderrOut.close(); stdoutVacuum.join(); stderrVacuum.join(); string output = stdoutVacuum.tostring(); if (stderrVacuum.tostring().length() > 0) { this.stderrDuringParse = stderrVacuum.tostring(); Console.Error.WriteLine("exec stderrVacuum: " + stderrVacuum); } return(output); } catch (MalformedURLException ex) { LOGGER.log(Level.SEVERE, null, ex); } catch (IOException ex) { LOGGER.log(Level.SEVERE, null, ex); } catch (InterruptedException ex) { LOGGER.log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { LOGGER.log(Level.SEVERE, null, ex); } catch (IllegalArgumentException ex) { LOGGER.log(Level.SEVERE, null, ex); } catch (InvocationTargetException ex) { LOGGER.log(Level.SEVERE, null, ex); } catch (NoSuchMethodException ex) { LOGGER.log(Level.SEVERE, null, ex); } catch (SecurityException ex) { LOGGER.log(Level.SEVERE, null, ex); } catch (ClassNotFoundException ex) { LOGGER.log(Level.SEVERE, null, ex); } } try { string[] args = new string[] { "java", "-classpath", tmpdir + pathSep + CLASSPATH, className, new File(tmpdir, "input").getAbsolutePath() }; //string cmdLine = "java -classpath "+CLASSPATH+pathSep+tmpdir+" Test " + new File(tmpdir, "input").getAbsolutePath(); //Console.WriteLine("execParser: "+cmdLine); Process process = Runtime.getRuntime().exec(args, null, new File(tmpdir)); StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream()); StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream()); stdoutVacuum.start(); stderrVacuum.start(); process.waitFor(); stdoutVacuum.join(); stderrVacuum.join(); string output = stdoutVacuum.tostring(); if (stderrVacuum.tostring().length() > 0) { this.stderrDuringParse = stderrVacuum.tostring(); Console.Error.WriteLine("exec stderrVacuum: " + stderrVacuum); } return(output); } catch (Exception e) { Console.Error.WriteLine("can't exec recognizer"); e.printStackTrace(System.err); } return(null); }
public JavaArchiveReflector(FileInfo jar) { this.FileName = jar; var clazzLoader = default(URLClassLoader); try { var filePath = "jar:file://" + jar.FullName + "!/"; var url = new java.io.File(filePath).toURL(); clazzLoader = new URLClassLoader(new URL[] { url }); } catch { } this.GetDynamicEnumerator = () => { var zip = default(ZipInputStream); try { zip = new ZipInputStream(new FileInputStream(jar.FullName)); } catch { } return((GetNextEntry) delegate { if (zip == null) { return null; } var e = default(ZipEntry); try { e = zip.getNextEntry(); } catch { } if (e == null) { return null; } var n = new Entry { Name = e.getName() }; if (clazzLoader != null) { if (n.Name.EndsWith(".class")) { n.TypeFullName = n.Name.Substring(0, n.Name.Length - ".class".Length).Replace("/", "."); n.InternalGetType = delegate { var c = default(java.lang.Class); try { c = clazzLoader.loadClass(n.TypeFullName.Replace(".", "/")); } catch (csharp.ThrowableException cc) { System.Console.WriteLine("error: " + cc); } return c.ToType(); }; } } return n; }); }; }