First, the 'cd' command can't be executed by Runtime.exec() in the first place (see How to use "cd" command using Java runtime?). You should be able to just set the working directory for the process when you call exec (see below).

Second, running 'cmd.exe /c' to execute your process isn't what you want here. You won't be able to get the results of your process running, because that is returned to the command window -- which eats the error and then closes without passing the error along to you.

Your exec command should look more like this:

CopyProcess p = Runtime.getRuntime().exec(
    command, null, "C:\Users\Eric\Documents\COSC\ecj");

Where 'command' looks like this:

CopyString command = "java ec.Evolve -file ec\app\tutorial5\tutorial5.params"

Edit: For reading error messages, try this:

CopyString error = "";
try (InputStream is = proc.getErrorStream()) {
    error = IOUtils.toString(is, "UTF-8");
}
int exit = proc.waitFor();
if (exit != 0) {
    System.out.println(error);
} else {
    System.out.println("Success!");
}
Answer from Dave on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Runtime.html
Runtime (Java Platform SE 7 )
Enable or disable finalization ... the Java runtime exits. By default, finalization on exit is disabled. If there is a security manager, its checkExit method is first called with 0 as its argument to ensure the exit is allowed. This could result in a SecurityException. ... SecurityException - if a security manager exists and its checkExit method doesn't allow the exit. ... Executes the specified ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-runtime-exec-method
Java Runtime exec() Method with Examples - GeeksforGeeks
October 23, 2023 - With it, you can instruct the OS to perform tasks outside the realm of Java, seamlessly integrating your application with the underlying system. So let's get into the details. Before we jump into the practical examples, let's cover some essential concepts related to the exec() method: Runtime Object: The exec() method belongs to the Runtime class, which represents the runtime environment of the application.
Discussions

Disadvantages of using Runtime.exec()?
Exception handling for one. By using exec you don't get any results, unlike the Java functions. More on reddit.com
🌐 r/java
13
8
April 13, 2020
java - Safe usage of Runtime.getRuntime.exec(String[]) - Information Security Stack Exchange
OWASP's page on command injection has a paragraph about java's exec (emphasis mine): C’s system function passes its arguments to the shell (/bin/sh) to be parsed, whereas Runtime.exec tries to split the string into an array of words, then executes the first word in the array with the rest ... More on security.stackexchange.com
🌐 security.stackexchange.com
April 5, 2020
Java: exec(): execute an app with whitesp… - Apple Community
Runtime.exec is very finicky in Java ... it doesnt actually run the command line interpreter the way one would think. You need to use a parameter array when you have spaces in the path. More on discussions.apple.com
🌐 discussions.apple.com
Problem with Runtime.exec() method and using multiple commands in Linux
Appearently my problem was trying to use a shell command using .exec() which does run programs but cd is a shell not a executed/forked program. So I had to use this line on my runtime.exec() line: runtime.exec(new String[]{"/bin/sh", "-c", "cd /mnt/; ls --group-directories-first"}); More on reddit.com
🌐 r/javahelp
4
8
September 16, 2019
🌐
Coderanch
coderanch.com › t › 419192 › java › Runtime-getRuntime-exec-String-command
Runtime getRuntime() exec(String command) - How does this work? (Java in General forum at Coderanch)
June 16, 2003 - The key thing to remember when using Runtime.exec() is you must consume everything from the child process' input stream. [ June 16, 2003: Message edited by: Michael Morris ] Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher ... Well let me rephrase my question. How do you run an external windows command line program from a java ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Runtime.html
Runtime (Java Platform SE 8 )
April 21, 2026 - If enabled, then the finalizers ... the Java runtime exits. That behavior is inherently unsafe. It may result in finalizers being called on live objects while other threads are concurrently manipulating those objects, resulting in erratic behavior or deadlock. ... Executes the specified ...
🌐
Reddit
reddit.com › r/java › disadvantages of using runtime.exec()?
r/java on Reddit: Disadvantages of using Runtime.exec()?
April 13, 2020 -

I'm working with some legacy code and have to remake a software module. The module previously used Runtime.exec() for things such as making a directory, removing a directory, unzipping a file, and copying files. The software runs on Linux servers and most probably will always run on a Linux server.

Now that I'm making the module from scratch, I'm thinking of using native Java functions for accomplishing all this. Keeping the project requirements/dependencies aside for a moment, is there any advantage/disadvantage of doing it this way?

Find elsewhere
🌐
GitHub
gist.github.com › f3348e7843994bdd56f0
Java Runtime.getRuntime().exec() Example · GitHub
package api; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStreamReader; public class Exec { /** * @param args */ public static void main(String[] args) { String result = ""; try { // Execute command String command = "k:\\curl.exe -d login_name=mohwtest -d login_passwd=12345 --insecure https://mohw.cyberhood.net.tw/xml_import.php"; Process child = Runtime.getRuntime().exec(command); DataInputStream in = new DataInputStream( child.getInputStream()); BufferedReader br = new BufferedReader( new InputStreamReader(in)); String line; while ((line = br.readLine()) != null) { result +=line; } in.close(); } catch (IOException e) { } System.out.println(result); } }
🌐
Tutorialspoint
tutorialspoint.com › java › lang › runtime_exec.htm
Java Runtime exec() Method
package com.tutorialspoint; public class RuntimeDemo { public static void main(String[] args) { try { // print a message System.out.println("Executing explorer.exe"); // create a process and execute explorer.exe Process process = Runtime.getRuntime().exec("explorer.exe"); // print another message System.out.println("Windows Explorer should now open."); } catch (Exception ex) { ex.printStackTrace(); } } }
🌐
IBM
ibm.com › docs › he › ssw_ibm_i_74 › rzaha › javalang.htm
Using java.lang.Runtime.exec()
Use the java.lang.Runtime.exec() method to call programs or commands from within your Java program. Using java.lang.Runtime.exec() method creates one or more additional thread-enabled jobs. The additional jobs process the command string that you pass on the method.
🌐
More of Less
moreofless.co.uk › system-exec-java-not-working
How to exec external commands in Java with correct parameters
Process process = Runtime.getRuntime().exec("helloworld param1 param2"); int exitCode = process.waitFor(); System.out.println(exitCode); All good, now the problems start when we want to send a parameter of Mickey Mouse and to do that on the command line we'd put double-quotes around it to make ...
🌐
Java2s
java2s.com › example › java-api › java › lang › runtime › exec-3-0.html
Example usage for java.lang Runtime exec
From source file:org.openmrs.module.reportingsummary.api.io.util.InputOutputUtils.java · /** * Method to execute a command inside the shell. The command is usually the mysqldump or mysql command. * * @param commands the command array to be executed * @throws Exception when the task execution is interrupted *//* w ww .j av a 2s. c om*/ public static void executeCommand(final File workingDirectory, final String[] commands) throws Exception { Runtime runtime = Runtime.getRuntime(); Process process; if (OpenmrsConstants.UNIX_BASED_OPERATING_SYSTEM) process = runtime.exec(commands, null, workingDi
🌐
Blogger
codewhitesec.blogspot.com › 2015 › 03 › sh-or-getting-shell-environment-from.html
CODE WHITE | Blog: $@|sh – Or: Getting a shell environment from Runtime.exec
We call this class as shown below with single quotes around the command line to ensure that our shell passes the command line argument to Java as is: ... Your first thought on a solution to this might be: "Ok, I'll just use sh -c command to execute command in the shell." That is correct, but here is the catch: Since only the first argument following -c is interpreted as shell command, the whole shell command must be passed as a single argument. And if you take a closer look at how the string passed to Runtime.exec is processed, you'll see that Java uses a StringTokenizer that splits the command at any white-space character.
🌐
Rene Nyffenegger
renenyffenegger.ch › notes › development › languages › Java › classes › java › lang › Runtime › exec
Java class java.lang.Runtime - exec
The following example creates a cmd.exe process and executes dir /s /b ..\.. to list all files beneath the directory that is two levels higher than the current one. It then reads the process' stdout and repeats it to the console: public class exec { public static void main(String[] argv) { java.lang.Runtime rt = java.lang.Runtime.getRuntime(); try { // java.lang.Process proc = rt.exec("c:\\Program Files\\Mozilla Firefox\\firefox.exe http://renenyffenegger.ch/"); java.lang.Process proc = rt.exec("cmd.exe /c dir /s /b ..\\.."); // proc.waitFor(); // // Note: «StdOut» is returned by getInputStr
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.runtime.exec
Runtime.Exec Method (Java.Lang) | Microsoft Learn
Java.Lang · Assembly: Mono.Android.dll · Important · Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here. Executes the specified string command in a separate process. [Android.Runtime.Register("exec", "(Ljava/lang/String;)Ljava/lang/Process;", "GetExec_Ljava_lang_String_Handler")] public virtual Java.Lang.Process?
🌐
Coalfire
coalfire.com › home › coalfire articles › command injection in java: 80% proven that it is 100% impossible (sometimes)
Command Injection in Java | Coalfire
March 17, 2025 - Update: The zip file is no longer accessible, but you can still see the implications of the JVM using safe "execve" calls here. It shows that the Java String sent to Runtime.getRuntime().exec(command) gets split on ' ', the first resulting String is the executable that gets run and the remaining Strings in the array are passed along as the arguments so even if they contain control characters or other commands those are just sent to the executable as the argv parameters.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-runtime-getruntime-method
Java Runtime getRuntime() Method with Examples - GeeksforGeeks
July 23, 2025 - This object allows you to access various aspects of the application's runtime environment, such as executing system commands, managing processes, and querying memory usage. ... // Java Program to implement // Java Runtime getRuntime() import java.io.IOException; // Driver Class public class GfgRuntimeExample { // main function public static void main(String[] args) { // Getting the runtime object Runtime runtime = Runtime.getRuntime(); // Example 1: Executing System Command try { // Executing a system command to open a web page // (https://www.gfg.com/) Process process = runtime.exec( "xdg-ope
🌐
IBM
ibm.com › docs › en › i › 7.4.0
IBM i
October 7, 2025 - Note: By using the code examples, you agree to the terms of the Code license and disclaimer information. import java.io.*; public class CallHelloPgm { public static void main(String args[]) { Process theProcess = null; BufferedReader inStream = null; System.out.println("CallHelloPgm.main() invoked"); // call the Hello class try { theProcess = Runtime.getRuntime().exec("java QIBMHello"); } catch(IOException e) { System.err.println("Error on exec() method"); e.printStackTrace(); } // read from the called program's standard output stream try { inStream = new BufferedReader( new InputStreamReader( theProcess.getInputStream() )); System.out.println(inStream.readLine()); } catch(IOException e) { System.err.println("Error on inStream.readLine()"); e.printStackTrace(); } } } Copy to clipboard ·
🌐
InfoWorld
infoworld.com › home › software development › programming languages › java
When Runtime.exec() won’t | InfoWorld
December 29, 2000 - The prevalent first test of an API is to code its most obvious methods. For example, to execute a process that is external to the Java VM, we use the exec() method. To see the value that the external process returns, we use the exitValue() method on the Process class.
🌐
Apple Community
discussions.apple.com › thread › 133798
Java: exec(): execute an app with whitesp… - Apple Community
The following is an example of syntax that works for passing a file to an application as an argument: String [] cmdArray = new String[2]; cmdArray[0] = "/Applications/TextEdit.app/Contents/MacOS/TextEdit"; cmdArray[1] = "/Users/Shared/Untitled.rtf"; Runtime rt = Runtime.getRuntime(); Process ...