How to execute command with parameters?
See if this works (sorry can’t test it right now) Runtime.getRuntime().exec(new String[]{“php”,”/var/www/script.php”, “-m”, “2”});
See if this works (sorry can’t test it right now) Runtime.getRuntime().exec(new String[]{“php”,”/var/www/script.php”, “-m”, “2”});
You start a new process with Runtime.exec(command). Each process has a working directory. This is normally the directory in which the parent process was started, but you can change the directory in which your process is started. I would recommend to use ProcessBuilder ProcessBuilder pb = new ProcessBuilder(“ls”); pb.inheritIO(); pb.directory(new File(“bin”)); pb.start(); If you want … Read more
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
You need to specify the arguments as separate Strings: new ProcessBuilder(“cmd”, “arg1”, “arg2”, …); The constructor accepts String, varargs, and List<String>. See ProcessBuilder documentation.
I found a workaround in this article, basically the idea is that you create a process early on in the startup of your application that you communicate with (via input streams) and then that subprocess executes your commands for you. //you would probably want to make this a singleton public class ProcessHelper { private OutputStreamWriter … Read more
By using start, you are askingcmd.exe to start the batch file in the background: Process p = Runtime.getRuntime().exec(“cmd /c start ” + path + “\\RunFromCode.bat”); So, the process which you launch from Java (cmd.exe) returns before the background process is finished. Remove the start command to run the batch file in the foreground – then, … Read more
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
You need to specify the arguments as separate Strings: new ProcessBuilder(“cmd”, “arg1”, “arg2”, …); The constructor accepts String, varargs, and List<String>. See ProcessBuilder documentation.
Why do some commands fail? This happens because the command passed to Runtime.exec(String) is not executed in a shell. The shell performs a lot of common support services for programs, and when the shell is not around to do them, the command will fail. When do commands fail? A command will fail whenever it depends … Read more
Runtime.getRuntime().exec(“netsh”); See Runtime Javadoc. EDIT: A later answer by leet suggests that this process is now deprecated. However, as per the comment by DJViking, this appears not to be the case: Java 8 documentation. The method is not deprecated.