public Process createProcess() throws ExecutionException { if (LOG.isDebugEnabled()) { LOG.debug("Executing [" + getCommandLineString() + "]"); } List<String> commands; try { checkWorkingDirectory(); if (StringUtil.isEmptyOrSpaces(myExePath)) { throw new ExecutionException( IdeBundle.message("run.configuration.error.executable.not.specified")); } commands = CommandLineUtil.toCommandLine(myExePath, myProgramParams.getList()); } catch (ExecutionException e) { LOG.warn(e); throw e; } try { ProcessBuilder builder = new ProcessBuilder(commands); setupEnvironment(builder.environment()); builder.directory(myWorkDirectory); builder.redirectErrorStream(myRedirectErrorStream); return builder.start(); } catch (IOException e) { LOG.warn(e); throw new ProcessNotCreatedException(e.getMessage(), e, this); } }
public static void main(String args[]) throws InterruptedException, IOException { List<String> command = new ArrayList<String>(); command.add(System.getenv("windir") + "\\system32\\" + "tree.com"); command.add("/A"); ProcessBuilder builder = new ProcessBuilder(command); Map<String, String> environ = builder.environment(); builder.directory(new File(System.getenv("temp"))); System.out.println("Directory : " + System.getenv("temp")); final Process process = builder.start(); InputStream is = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { System.out.println(line); } System.out.println("Program terminated!"); }
public int compileJava() { try { // create new bin directory boolean createBin = new File(classPath).mkdir(); // create new javac ProcessBuilder // ProcessBuilder pb = // new ProcessBuilder("javac", "-d", classPath, "./" + studentPath + "/*.java"); ProcessBuilder pbDir = new ProcessBuilder("dir"); // Determine current working directory File srcAbsPath = new File(sourcePath); srcAbsPathName = srcAbsPath.getAbsolutePath(); System.out.println("Compiler.java line 69 source path: " + sourcePath); System.out.println("Compiler.java line 69 source absolute path: " + srcAbsPathName); File cwd = pbDir.directory(); // debug code - to confirm correct directory // TestTools.dir(cwd); // NB - ProcessBuilder default is to return a null // pointer for the abstract path to indicate that it // is using System.Properties "user.dir", i.e., the // current system working directory; hence the // critical need to handle a NullPointerException. // Also returns a null pointer if the directory // doesn't exist. // all this is doing is changing the dir, can we approach this in a different way using a // value in our Data Object? -mh File nwd = TestTools.cd(cwd, studentPath); System.out.println("(Compiler.java line 88)new working directory: " + nwd.toString()); String studentPathName = nwd.getAbsolutePath(); File nwdPath = new File(studentPath); System.out.println("(Compiler.java line 91)new working directory path: " + studentPathName); // debug code to test new working directory // TestTools.dir(nwd); FileFilter filter = new FileFilter() {}; String[] javaFileList = nwdPath.list(filter); // set up output file File outputFile = new File(outputFileName); // System.out.println(outputFileName); outputFile.delete(); for (int k = 0; k < javaFileList.length; k++) { try { if (filter.accept(nwdPath, javaFileList[k]) == true) { System.out.println("COMPILER.JAVA (line 111) Compiling: " + javaFileList[k]); String compilePath = "javac" + "-d" + classPath + ".\\" + studentPath + "\\" + javaFileList[k]; System.out.println("Compiler.java 117 compile path: " + compilePath); ProcessBuilder pb = // new ProcessBuilder("javac ", "-d", classPath, ".\\" + studentPath + "\\" + // javaFileList[k]); new ProcessBuilder(compilePath); // System.out.println(pb.environment().toString()); <-- THIS IS VERY INTERESTING // Create environment map and set environmental variables Map<String, String> env = pb.environment(); env.clear(); env.put("PATH", path); env.put("CLASSPATH", classPath); // env.put("SOURCEPATH", sourcePath); // env.remove("OTHERVAR"); pb.redirectErrorStream(true); pb.redirectOutput(Redirect.appendTo(outputFile)); // start javac process Process p = pb.start(); // need other processes to wait for compilation to finish // basically joins the thread to the javac process to force sequential // execution - need to be careful - if any process hangs, whole run hangs success = p .waitFor(); // Returns the exit value of the process. By convention, 0 indicates // normal termination. // http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html#waitFor%28%29 assert pb.redirectInput() == Redirect.PIPE; assert pb.redirectOutput().file() == outputFile; assert p.getInputStream().read() == -1; System.out.println("COMPILER.JAVA (line 138) end of loop, success = " + success); } } catch (Exception e) { System.out.println(" Compiler.java FOR LOOP Compile Exception: " + javaFileList[k]); } } } catch (Exception e) { System.out.println("Compile Exception, PROBABLY DUE TO FILE PATH"); System.out.println("source absolute path: " + srcAbsPathName); } return success; }
public static void main(String[] args) { ProcessBuilder process = new ProcessBuilder(); Integer port; if (process.environment().get("PORT") != null) { port = Integer.parseInt(process.environment().get("PORT")); } else { port = 4567; } staticFileLocation("/public"); String layout = "templates/layout.vtl"; get( "/", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("template", "templates/index.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); // get(/) type of route get this url.. get( "/tasks", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("tasks", Task.all()); model.put("template", "templates/tasks.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); // After they submit the form, this is where they will be taken /tasks.vtl // get("tasks/new", (request, response) -> { // HashMap<String, Object> model = new HashMap<String, Object>(); // model.put("template", "templates/task-form.vtl"); // return new ModelAndView(model, layout); // }, new VelocityTemplateEngine()); // //task-form is where client inputs data and hits submit post( "/tasks", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); Category category = Category.find(Integer.parseInt(request.queryParams("categoryId"))); String description = request.queryParams("description"); Task newTask = new Task(description); category.addTask(newTask); model.put("category", category); model.put("template", "templates/success.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); // grabs information and makes a new description of the information in the array // takes you to a new page get( "/tasks/:id", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); Task task = Task.find(Integer.parseInt(request.params(":id"))); model.put("task", task); model.put("template", "templates/task.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/categories", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("categories", Category.all()); model.put("template", "templates/categories.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/categories/new", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("template", "templates/category-form.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); post( "/categories", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); String name = request.queryParams("name"); Category newCategory = new Category(name); model.put("category", newCategory); model.put("template", "templates/success.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/categories/:id", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("category", Category.find(Integer.parseInt(request.params(":id")))); model.put("template", "templates/category.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); get( "/categories/:id/tasks/new", (request, response) -> { HashMap<String, Object> model = new HashMap<String, Object>(); model.put("category", Category.find(Integer.parseInt(request.params(":id")))); model.put("template", "templates/category-tasks-form.vtl"); return new ModelAndView(model, layout); }, new VelocityTemplateEngine()); } // end of main