Can't run exe file from cmd in Java - java

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!

Related

Writing cmd output to a file is not working with Tomcat :(

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).

How to run a jar file and launch terminal from java program?

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

start a new process in java,invoke an exe, but can find the .txt file this exe output in nowhere

I invoke this exe command in prompt line, it works fine, just output the txt file to the path I told it.
But I cannot find this file when I invoke this exe from java
the function I use is
Process pro = Runtime.getRuntime().exec(cmds);
any clue will be appreciate!
You need to set its working directory(where the directory which contains the file you want to execute).
Do do this:
Process p;
ProcessBuilder pb = new ProcessBuilder("your command", "arg", "arg", "etc");
pb.directory("/thepath/to/thefile");
p = pb.start();

Launching batch file from JAVA not working for directory path having spaces

I have created a batch file with the following contents
xcopy "C:\Documents\javascript\src\*" "C:\Program Files (x86)\Apache Group\Apache2\htdocs\docs\8.1\version1\" /s /y
I am running the batch file using Java. Running the script from command line or directly executing it(double clicking) doesn't seem to have any problem. However when I run using java the copy operation doesn't succeed. In the console I see a message More? when the script is executed.
Also running the copy operation for directory path without spaces seems to work fine.
Here is the java method which does the batch file running.
public void run(String input)
{
File dir = new File(input);
ProcessBuilder processBuilder = new ProcessBuilder("cmd");
processBuilder.redirectInput(dir);
Process process = processBuilder.start();
int exitStatus = process.waitFor();
process.destroy();
}
Any suggestions? Thanks in advance.
It depends on the way you are launching the bat file
for me this works fine:
Runtime.getRuntime().exec("cmd /c start xcopy-file.bat"

Does cmd always open a new window when you md

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();

Categories

Resources