This is one way to run cmd.exe using java:
String command="cmd /c start cmd.exe";
Process p = Runtime.getRuntime().exec(command);
How to enforce command to run cmd.exe from the root directory C:\ ?
As suggested by others, consider using ProcessBuilder.
Code:
ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/C", "start");
processBuilder.directory(new File("C:\\"));
try {
processBuilder.start();
} catch (IOException e) {
e.printStackTrace();
}
Related
I have following java code
public static void main(String a[]) {
String location = "C:\\Users\\test\\output\\testProject";
File dir = new File("C:\\Users\\test\\cmds");
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "Start /wait","packageProject.bat",location);
pb.directory(dir);
Process p = null;
try {
p = pb.start();
p.waitFor();
}
catch (IOException e) {
e.printStackTrace();
}
catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("Folder created");
}
Batch file is
cd "C:\Users\test\output\test-master"
mvn clean install -DskipTests
exit
It is packaging file but not command prompt is not closing once process is complete.
Please suggest.
You should remove wrapper CMD.EXE and start, just call the batch file directly as:
String bat = new File(dir, "packageProject.bat").getAbsolutePath();
ProcessBuilder pb = new ProcessBuilder(bat , location);
pb.directory(dir);
Process p = pb.start();
p.waitFor();
If this process generates a lot of output you may run into a second problem if you don't consume error and output streams. You can do this in background threads, or simply send stdout/err to files by adding these calls before pb.start():
pb.redirectOutput(new File(location, "std.out"));
pb.redirectError(new File(location, "std.err"));
In Java, I've tried to run:
Process p = Runtime.getRuntime().exec("C:\\Windows\\System32\\chgport.exe");
as well as
Process p = Runtime.getRuntime().exec("chgport.exe");
but getting the following Exception:
java.io.IOException: Cannot run program "C:\Windows\System32\chgport.exe": CreateProcess error=2, The system cannot find the file specified
I am using the NetBeans IDE and it is running with admin credentials.
I tried your code and its working fine, Try it like so:
String[] command = {"chgport"};
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(new File("C:/Windows/System32/"));
pb.redirectErrorStream(true);
Process p = pb.start();
I tried both methods from Eclipse and both are working fine
Is it possible that you are not running your IDE with Administrator rights?
Can you try close the IDE and right click run as administrator?
try {
Process p = Runtime.getRuntime().exec("C:\\Windows\\System32\\mspaint.exe");
p.waitFor();
String[] command = {"mspaint"};
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(new File("C:/Windows/System32/"));
pb.redirectErrorStream(true);
Process p2 = pb.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You could run it with CMD /C, which "carries out the command specified by string and then terminates".
Process p = Runtime.getRuntime().exec("CMD /C chgport.exe");
How can I get the process id of the shell script started with ProcessBuilder?
String cmd[] = { "sh", "-c", "ls -l" };
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectOutput(new File(request.getParameter("output_file_name")));
Process p = pb.start();
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
p.destroy();
See: How can a Java program get its own process ID?
Java 9 now has support for this. Otherwise call a script out of your application to save the PID which you will read somewhere
I am running the following code in Java for running a shell script in Ubuntu.
But when it is running, y=the putty terminal will be displayed. But i dont want this. How will I hide this terminal.
ProcessBuilder pb = new ProcessBuilder(winBasePath + "putty.exe", "-ssh", "-m", winBasePath + "runHiveCmd.txt", linuxSystem, "-pw", linuxPwd);
Process p = pb.start();
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
Try this
ProcessBuilder pb =
new ProcessBuilder("cmd.exe", "/C", "START", "/MIN", winBasePath + "putty.exe", ...);
If performing a remote command via ssh, consider jsch. This provides cleaner integration does not require ProcessBuilder. Here is an example you could start with
How to set argv[0] name for ps when spawning program from java getRuntime().exec()?
Runtime rt = Runtime.getRuntime();
String[] cmd = {"/bin/sh", "-c", "ls > hello"};
rt.exec(cmd);
I want the process to have another name in ps ef output.
In C you can just overwrite argv[0] pointer, how to do it in java?
In Java you would need to create a symbolic link to the executable and call that instead. I don't believe you have access to the argv[0]
#user1335897: ProcessBuilder provides more flexibility than Runtime, try using following code:
ProcessBuilder processBuilder = new ProcessBuilder(args);
if (envMap != null) {
processBuilder.environment().putAll(envMap);
}
try {
Process process = processBuilder.start();
process.waitFor();
} catch (IOException ioe) {
} catch (InterruptedException ie) {
}