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 you shouldn't need to. Simply use absolute file paths.
The one case where the current working directory matters is executing an external process (using ProcessBuilder
or Runtime.exec()
). In those cases you can specify the working directory to use for the newly started process explicitly (ProcessBuilder.directory()
and the three-argument Runtime.exec()
respectively).
Note: the current working directory can be read from the system property user.dir
. You might feel tempted to set that system property. Note that doing so will lead to very bad inconsistencies, because it's not meant to be writable.
Java Runtime Command to Change Directory & Compile LaTeX Source Not Working
The operator &&
is expanded by the shell. Runtime.exec
does not expand this operator and just attempts to executes the cd
command with the remainder of the command line as arguments. Thus, your pdflatex
command is never run.
If you want to run pdflatex
with a particular working directory, use the three-argument version of Runtime.exec
.
How to run windows commands (cd programfiles..etc) through java program
Please see my answer to a similar question which some people found useful. Here is it:
You can use Runtime.exec(java.lang.String, java.lang.String[], java.io.File) where you can set the working directory.
Or else you can use ProcessBuilder as follows:
ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
pb.directory(new File("myDir"));
Process p = pb.start();
executing cmd command using a java program
Try this:
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("java -jar Demo.jar readExcelDemo.Hvd");
If above code is not okay, try this:
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("cmd.exe /c start java -jar Demo.jar readExcelDemo.Hvd");
If you want to watch cmd, you can use below code.
It will immediately close because of the /c flag.
Process p = runTime.exec("cmd.exe /c start cmd /k java -jar Demo.jar readExcelDemo.Hvd");
Executing commands using Java Runtime
A few things.
- You should use
Process
andProcessBuilder
instead. - The commands have to be split up and tokenized according to arguments.
- The way you have it written, those two commands will not be executed in the same process.
- Fortunately for you,
ProcessBuilder
supports changing the working directory of the command anyway.
As an example:
sendCommand("homepath/plugins", "mvn", "archetype:generate", "-DarchetypeCatalog=file://homepath/.m2/repository");
private static void sendCommand(String workingDirectory, String... command) throws IOException {
Process proc = new ProcessBuilder(command).directory(new File(workingDirectory)).start();
int status = proc.waitFor();
if (status != 0) {
// Handle non-zero exit code, which means the command failed
}
}
Notice how a) the command has been split up, and b) that the working directory is passed in and set using ProcessBuilder.directory(File)
. This will get your desired behavior, but note that each command will still be a separate process, and there's no way to combine them with Java. You'd have to use Maven's features to get them all to run at once by specifying multiple build targets.
Java Runtime.exec() to run a java program
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:
Process p = Runtime.getRuntime().exec(
command, null, "C:\Users\Eric\Documents\COSC\ecj");
Where 'command' looks like this:
String command = "java ec.Evolve -file ec\app\tutorial5\tutorial5.params"
Edit: For reading error messages, try this:
String 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!");
}
Related Topics
Jfreechart with Selectable Domain Axis and Zoom
How to Pass Parameters to Anonymous Class
Why Do I Get "Non-Static Variable This Cannot Be Referenced from a Static Context"
Is the Java Hashmap Keyset() Iteration Order Consistent
Repaint Swing Button with Different Gradient
Multiple Input in Joptionpane.Showinputdialog
Why Do == Comparisons with Integer.Valueof(String) Give Different Results for 127 and 128
Differencebetween 'Super' and 'Extends' in Java Generics
How to Make Jscrollpane (In Borderlayout, Containing JPAnel) Smoothly Autoscroll
Java, List Only Subdirectories from a Directory, Not Files
Rounding a Double to Turn It into an Int (Java)
Java Ee Specification and Multi Threading
Refreshing Gui by Another Thread in Java (Swing)
How to Properly Configure Implicit/Explicit Waits and Pageloadtimeout Through Selenium
Passing Directly an Array Initializer to a Method Parameter Doesn't Work