ProcessBuilder to execute custom executable - java

Okay, I have tried a dozen different ways and no success. I want to execute a custom exe and grab the output. It runs fine from the command prompt. I get the "dir" to work fine, but not custom.exe. Here is the code:
List<String> command = new ArrayList<String>();
command.add("cmd"); // Even removed these two lines
command.add("/c"); // aka hail mary coding.
//command.add("dir");
command.add("custom.exe"); // even tried "c://custom.exe"
String line;
Process p = new ProcessBuilder(command).start();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
I get no output at all. If I place it in a batch file, i get output. I have a feeling it has something to do with %PATH%. Back at it...
EDIT--> So turns out that the output from this custom exe goes to error, so to see what is happening i have the code:
List<String> command = new ArrayList<String>();
command.add(System.getenv("ProgramFiles(x86)") + "\\mydir\\custom.exe";
String line;
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
And it works like a hot damn. :)

You don't need the lines
command.add("cmd");
command.add("/c");
That would only be required for a batch file. I would rather specify the full path to the executable.
Maybe the output is on stderr? Try replacing p.getInputStream() with p.getErrorStream().

Related

Java can't run cmd-command's properly

I am attempting to generate a list of all installed Programs on my Windows machine.
This is the command I am using:
WMIC /output:D:\miep product get name && type D:\miep > D:\miep_
You might have realized that I'm also trying to make a type-Command as I need the output in UTF-8.
I made a Whitelist for this with a simple loop where I will look later where in my file certain Names will appear and keep them while I remove everything else.
The command works in the command prompt, but when I try to do the same inside my Java Program it keeps telling me I've got an Invalid GET-Expression ...
Here is my function:
void createLists() throws IOException {
//String cmd = "WMIC /output:D:\\miep.csv product get name /format:\"%WINDIR%\\System32\\wbem\\de-DE\\csv.xsl\"";
String cmd = "WMIC /output:D:\\miep product get name && type D:\\miep > D:\\miep_";
System.out.println(cmd);
Process p;
p = Runtime.getRuntime().exec(cmd);
p.getOutputStream().close();
String line;
BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = stdout.readLine()) != null) {
System.out.println(line);
}
stdout.close();
BufferedReader stderr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = stderr.readLine()) != null) {
System.out.println(line);
}
stderr.close();
System.out.println("Done");
}
I also tried the converting stuff with the .csv files as you might have seen in the second line of my code and the same:
Works in CMD, but not in my Java-Program..!
Here it keeps telling me that it's an Invalid XSL-Format
Can someone help?

java exec attempting to untar a file

I have been trying to untar a .tbz file without a lot of success in a java app. I have now decided to try and hit the command line to get the job done, and it currently doesn't through any errors but it doesn't untar the file, either. Can anyone see an issue with this?
String[] cmd = { "tar", "-xjf", "/var/tmp/filename.tbz"};
Process p =Runtime.getRuntime().exec(cmd, null);
EDIT, this works:
List<String> commands = new ArrayList<String>();
commands.add("tar");
commands.add("-xvjf");
commands.add("/var/tmp/filename.tbz");
ProcessBuilder pb = new ProcessBuilder(commands);
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String readline;
while ((readline = reader.readLine()) != null) {
System.out.println(readline);
}
What is a cd command doing there in the middle? Perhaps you meant this:
String[] cmd = { "tar", "-xjf", "/var/tmp/filename.tbz"};
If instead of the above, you really want to do this:
cd /var/tmp
tar -xjf filename.tbz
In this case you can use ProcessBuilder.

BufferedReader seems to infinitely read the first line

I'm trying to run Handbrake through a Java app I'm writing, and am having trouble waiting for Handbrake to finish.
When I try this :
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", command);
Process p = builder.start();
BufferedReader inputreader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while((line = inputreader.readLine()) != null)
{
System.out.println(line);
}
The output I get is :
Encoding: task 1 of 1, 0.00 %
Over and over, and the file never gets converted.
When I change it to the following:
BufferedReader inputreader = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader errorreader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line = null;
String line2 = null;
while((line = inputreader.readLine()) != null && (line2 = errorreader.readLine()) != null)
{
System.out.println(line);
System.out.println(line2);
}
It works on my test files, however it gets hung-up when the errorreader runs out of lines to read and the readLine() locks the thread infinitely. On full length files the file gets converted but this portion of code gets locked so it never continues with the application.
Any suggestions?
Call builder.redirectErrorStream(true); before creating the process (this will merge the input and the error stream into one: the input stream), and only read from the InputStream.
That should solve the problem of the error stream running out of data before the input stream.
If you do want to keep them separate, then you can start two threads, on to read from the input stream and one from the error stream.

How to execute an exe in java on eclipse and pass intput through a variable and get output on console?

I have an exe file and i want to execute it for a large number of times passing a variable as an input and print the output for each case..
Runtime runtime = Runtime.getRuntime();
for(int i=0;i<1000;i++)
{
Process p = runtime.exec("cmd /c start C:/Users/sbm/workspace/Codex/a.exe",i);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null)
{
System.out.println(line);
}
}
Even if i get the output in a file it will be helpful.
You can do something like this
for(int i=0;i<1000;i++) {
ProcessBuilder builder = new ProcessBuilder("urcmd","urarg");
builder.redirectOutput(new File("C:\\output\\process"+i+".txt"));
builder.start();
}

Get output from BAT file using Java

I'm trying to run a .bat file and get the output. I can run it but I can't get the results in Java:
String cmd = "cmd /c start C:\\workspace\\temp.bat";
Runtime r = Runtime.getRuntime();
Process pr = r.exec(cmd);
BufferedReader stdInput = new BufferedReader(
new InputStreamReader( pr.getInputStream() ));
String s ;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
The result is null. No idea why I get this. Note that I'm using Windows 7.
Using "cmd /c start [...]" to run a batch file will create a sub process instead of running your batch file directly.
Thus, you won't have access to its output.
To make it work, you should use:
String cmd = "C:\\workspace\\temp.bat";
It works under Windows XP.
You need to start a new thread that would read terminal output stream and copy it to the console, after you call process.waitFor().
Do something like:
String line;
Process p = Runtime.getRuntime().exec(...);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
Better approach will be to use the ProcessBuilder class, and try writing something like:
ProcessBuilder builder = new ProcessBuilder("/bin/bash");
builder.redirectInput();
Process process = builder.start();
while ((line = reader.readLine ()) != null) {
System.out.println ("Stdout: " + line);
}
BufferedReader stdInput = new BufferedReader(new
InputStreamReader( pr.getErrorStream() ));
instead use
BufferedReader stdInput = new BufferedReader(new
InputStreamReader( pr.getInputStream ));

Categories

Resources