I am in the midst of a Java project, part of which is calling the Windows cmd to make a directory. My code currently looks like this:
Runtime rt = Runtime.getRuntime();
String command;
command = "cmd.exe /c start mkdir \"C:\\Users\\User1\\Documents\\Folder1\\"+folderName+"\" &&exit";
rt.exec(command);
This works fine (creates the folder), but it spawns an additional instance of cmd. (I originally added the "&&exit" thinking it would eliminate the extra window, but I now realize it is unnecessary code.)
1) Can I prevent this additional instance of cmd (which begins in the new directory), or
2) Can I close this extra cmd window without causing other problems? (I have heard that killing cmd can break other things on a machine.)
You should use:
File file = new File("C:\\Users\\User1\\Documents\\Folder1\\"+folderName+"\"");
if(!file.exists())
{
file.mkdir();
}
instead. However, if you want to call the command into cmd without creating a new one, you should not call "cmd.exe /c start". You can check that if you run that very same command from outside java it will also start a new cmd. Try this:
Runtime rt = Runtime.getRuntime();
String command;
command = "mkdir \"C:\\Users\\User1\\Documents\\Folder1\\"+folderName+"\" &&exit";
rt.exec(command);
Why don't you create the dir with File?
new File("C:\\my\\path\\myDir").mkdir();
Related
I was writing a program to open notepad application from java code.
I was able to do it with below code:-
String command = "notepad.exe";
Runtime run = Runtime.getRuntime();
run.exec(command);
It opens the application but it doesn't open in maxzimized/full-screen mode.
Is there anyway in which notepad application can be opened in full-screen mode using java or in a specified size.
Yes you can with the help of Start and /Max argument,
Code
Process p = Runtime.getRuntime().exec("cmd /c START /MAX notepad.exe");
Caution
The code wont work if you dont use cmd /c before the command and it will lead you to the java.io.IOException because it cannot find the file specified.
Try something like this to give a dynamic Mode capability :-
String cmd = "notepad.exe /C START /MAX";
Runtime.getRuntime().exec(cmd + args[0]);
Runtime example :-
java -cp . StartWindowMaximized notepad.exe
I'm using a java process to open cmd to run a command, then save the output to a text file.
ArrayList<String> commands = new ArrayList<>();
commands.add("cmd.exe");
commands.add("/c");
commands.add("cd "+System.getenv("LOGSTASH_PATH")+" && start /B cmd.exe /c \"logstash --config.test_and_exit -f "+configFileName+"\""+" > testing.txt");
ProcessBuilder builder = new ProcessBuilder(commands);
Process subProcess = builder.start();
Thread.sleep(50000);
subProcess.destroy();
This piece of code works when i try this with eclipse,But when i generate a war out of this and deploy in tomcat, it doesn't work. What could be the problem?
How to solve this?
I suspect the problem may be with the CD instruction:
Check that the LOGSTASH_PATH environment variable is available in the Tomcat process.
In case the current Tomcat directory and the LOGSTASH_PATH belong to different drives, add a /d qualifier:
"cd /d "+System.getenv("LOGSTASH_PATH")+ ...
(Tough is much better to replace the cd call by a Java call to directory(File) instead, as #nitind pointed).
I want to run jar file from my java program.
ProcessBuilder pb = new ProcessBuilder("java", "-jar", "parallel/Parallel.jar", "aug/*.xml");
Process p = pb.start();
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s = "";
while((s = in.readLine()) != null){
System.out.println(s);
}
int status = p.waitFor();
System.out.println("Exited with status: " + status);
Here is the error log I get:
[33mProblem found trying to create the log file[0m
[31mCannot locate configuration source aug/*.xml[0m
[31mNo files to work with[0m
Exited with status: 1
Problem 1: I tried using a whole file name instead of the * and it works. But, I want to run the jar on ALL files under the directory.
Problem 2: When run, the jar file will ask for some input "continue" or "cancel". But the jar program just exits in the Eclipse console without giving me chance to input anything. So, I am wondering if there is a way to launch the jar file inside a terminal?
For 1.
aug/*.xml does not work that way because the unfolding of the wildcard is done by cmd/bash. If you want to use it that way your jar would need to be able to unfold that string itself or you would have to call it using cmd/bash.
It would be preferable letting your java program Parallel unfold this so that you do not loose portability.
For 2.
To run it from a terminal you could use ProcessBuilder or Runtime.exec()
Something like that on windows: (windows cmd needs the path or it will not resolve your wildcard)
Process process = Runtime.getRuntime().exec("cmd /k start java -jar <path>/Parallel.jar <path>/aug/*.xml");
"/k" keeps the terminal active. "start" opens the terminal instead of just executing the command.
With ProcessBuilder it should be something like:
ProcessBuilder pb = new ProcessBuilder("cmd", "/k", "start", "java", "-jar", "<path>/Parallel.jar", "<path>/aug/*.xml");
For bash it would be along that lines:
ProcessBuilder pb = new ProcessBuilder("sh", "-c", "java", "-jar", "<path>/Parallel.jar", "<path>/aug/*.xml");
For further reading about Processbuilder/Runtime.exec() I would suggest: http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html
I try to run an exe file from cmd that I open by Java, but nothing happens.
The cmd that oppened seems like:
C:\>file.exe
C:\>
When I open manually cmd the exe file runs. the cmd seems the same in both cases! (manually and via Java).
My code is:
File projDir = new File("C:/");
String command = "cmd /c start file.exe";
Process p = Runtime.getRuntime().exec(command, null, projDir);
Do you have an idea?
You dont need run an exe thru cmd. This should be enough:
Runtime.getRuntime().exec("file.exe", null, projDir);
and thru cmd with:
Runtime.getRuntime().exec(new String[]{"cmd","/c","start file.exe"}, null, projDir);
Thank you all for your help, I found a parallel way to run the exe file, and this way works:
List<String> args = new ArrayList<String>();
args.add("path\\of\\exe\\file");
ProcessBuilder pb = new ProcessBuilder(args);
pb.start();
anyway - thanks for trying to help me!
I would like to know how to enter commands into the cmd.exe (command prompt window) , after its been opened?
I have the code below to open cmd.exe:
Runtime rt= Runtime.getRuntime();
Process process= rt.exec("cmd.exe /c start cd c:\\ExecutionSDKTest_10.2.2");
But after it's been opened, I'd like to enter "ant compile" or any line, how do I do that??
The normal way to do this would be to put the commands in a script and execute the script.
You will need to consume the output of the child process (stdout and stderr) on separate threads, or your process will block.
you dont need to open the command line to compile a program with a running program, check this out how to compile & run java program in another java program?
Not exactly the answer to you question. But you can use ProcessBuilder to set your process current directory (so you don't need to call "cd ..." anymore)
Try to pass a List to the ProcessBuilder
final List<String> l = new ArrayList<String>();
final String cmd = "C:/Program Files/Java/jre6/bin/";
l.add("C:\\WINNT\\system32\\cmd.exe ");
l.add("cd " + cmd);
l.add("dir");
l.add("java.exe -version");