How to use “cd” command using Java runtime?

There is no executable called cd, because it can’t be implemented in a separate process. The problem is that each process has its own current working directory and implementing cd as a separate process would only ever change that processes current working directory. In a Java program you can’t change your current working directory and … Read more

how to compile & run java program in another java program?

I have modified the code to include some checks: public class Laj { private static void printLines(String name, InputStream ins) throws Exception { String line = null; BufferedReader in = new BufferedReader( new InputStreamReader(ins)); while ((line = in.readLine()) != null) { System.out.println(name + ” ” + line); } } private static void runProcess(String command) throws … Read more

read the output from java exec

Use getErrorStream(). BufferedReader in = new BufferedReader(new InputStreamReader(pr.getErrorStream())); EDIT: You can use ProcessBuilder (and also read the documentation) ProcessBuilder ps=new ProcessBuilder(“java.exe”,”-version”); //From the DOC: Initially, this property is false, meaning that the //standard output and error output of a subprocess are sent to two //separate streams ps.redirectErrorStream(true); Process pr = ps.start(); BufferedReader in = new … Read more

How to run Linux commands in Java?

You can use java.lang.Runtime.exec to run simple code. This gives you back a Process and you can read its standard output directly without having to temporarily store the output on disk. For example, here’s a complete program that will showcase how to do it: import java.io.BufferedReader; import java.io.InputStreamReader; public class testprog { public static void … Read more

Execute external program

borrowed this shamely from here Process process = new ProcessBuilder(“C:\\PathToExe\\MyExe.exe”,”param1″,”param2″).start(); InputStream is = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; System.out.printf(“Output of running %s is:”, Arrays.toString(args)); while ((line = br.readLine()) != null) { System.out.println(line); } More information here Other issues on how to pass commands here and here

Difference between ProcessBuilder and Runtime.exec()

The various overloads of Runtime.getRuntime().exec(…) take either an array of strings or a single string. The single-string overloads of exec() will tokenise the string into an array of arguments, before passing the string array onto one of the exec() overloads that takes a string array. The ProcessBuilder constructors, on the other hand, only take a … Read more

tech