protected void runScript(String[] cmd, JTextArea txaMsg) { String strg = ""; if (cmd == null) return; Process prcs = null; try { Messages.postDebug("Running script: " + cmd[2]); Runtime rt = Runtime.getRuntime(); prcs = rt.exec(cmd); if (prcs == null) return; InputStream istrm = prcs.getInputStream(); if (istrm == null) return; BufferedReader bfr = new BufferedReader(new InputStreamReader(istrm)); while ((strg = bfr.readLine()) != null) { // System.out.println(strg); strg = strg.trim(); // Messages.postDebug(strg); strg = strg.toLowerCase(); if (txaMsg != null) { txaMsg.append(strg); txaMsg.append("\n"); } } } catch (Exception e) { // e.printStackTrace(); Messages.writeStackTrace(e); Messages.postDebug(e.toString()); } finally { // It is my understanding that these streams are left // open sometimes depending on the garbage collector. // So, close them. try { if (prcs != null) { OutputStream os = prcs.getOutputStream(); if (os != null) os.close(); InputStream is = prcs.getInputStream(); if (is != null) is.close(); is = prcs.getErrorStream(); if (is != null) is.close(); } } catch (Exception ex) { Messages.writeStackTrace(ex); } } }
// Creates a new thread, runs the program in that thread, and reports any errors as needed. private void run(String clazz) { try { // Makes sure the JVM resets if it's already running. if (JVMrunning) kill(); // Some String constants for java path and OS-specific separators. String separator = System.getProperty("file.separator"); String path = System.getProperty("java.home") + separator + "bin" + separator + "java"; // Tries to run compiled code. ProcessBuilder builder = new ProcessBuilder(path, clazz); // Should be good now! Everything past this is on you. Don't mess it up. println( "Build succeeded on " + java.util.Calendar.getInstance().getTime().toString(), progErr); println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~", progErr); JVM = builder.start(); // Note that as of right now, there is no support for input. Only output. Reader errorReader = new InputStreamReader(JVM.getErrorStream()); Reader outReader = new InputStreamReader(JVM.getInputStream()); // Writer inReader = new OutputStreamWriter(JVM.getOutputStream()); redirectErr = redirectIOStream(errorReader, err); redirectOut = redirectIOStream(outReader, out); // redirectIn = redirectIOStream(null, inReader); } catch (Exception e) { // This catches any other errors we might get. println("Some error thrown", progErr); logError(e.toString()); displayLog(); return; } }
// Run selected test cases. private void runTests() { for (int i = 0; i < tests.size(); i++) { // If box for test is checked, run it. if (tests.get(i).isSelected()) { // Get the URLs of all of the required files. String folderURL = tests.get(i).getText(); String testURL = folderURL.concat(folderURL.substring(folderURL.lastIndexOf('/'))); String efgFile = testURL + ".EFG"; String guiFile = testURL + ".GUI"; String tstFile = testURL + ".TST"; String prgFile = testURL + ".PRG"; // attempt to read in file with program's parameters try { FileInputStream fstream = new FileInputStream(prgFile); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); HashMap<String, String> prgParams = new HashMap<String, String>(); String strLine; while ((strLine = br.readLine()) != null) { // add found parameters into prgParams as <key, value> String[] matches = strLine.split("="); prgParams.put(matches[0], matches[1]); } if (prgParams.containsKey("path") && prgParams.containsKey("main")) { programPath = prgParams.get("path"); mainClass = prgParams.get("main"); } in.close(); } catch (Exception e) { System.err.println(e.getMessage()); } System.out.println("We hit Run"); // Run the replayer using the three test files. System.out.println( "../../../dist/guitar/jfc-replayer.sh -cp " + programPath + " -c " + mainClass + " -g " + guiFile + " -e " + efgFile + " -t " + tstFile); try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec( "../../../dist/guitar/jfc-replayer.sh -cp " + programPath + " -c " + mainClass + " -g " + guiFile + " -e " + efgFile + " -t " + tstFile); // InputStream ips = proc.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); } catch (Exception e) { e.printStackTrace(); } } } }