Get output from BAT file using Java - 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 ));

Related

Not getting input data stream when execute another process from ProcessBuilder

I am new to java and i am calling a Python script from java using processbuilder and trying read python output in java.
ProcessBuilder pb = new ProcessBuilder(Arrays.asList("python","PyScript.py",""+path));
Process p = pb.start();
String line;
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = in.readLine()) != null)
{
System.out.println(line);
logger.debug("Value of python output is"+line);
System.out.println("in while loop");
}
readline is getting null. when i run through command prompt its running fine.

How to run a terminal command and print the output through java in Mac

I tried below code to get the output of the ran command. But it printing empty.
String cmd = "/bin/bash device_id -l";
Process process = Runtime.getRuntime.exec(cmd);
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
Could anyone please help me. I have to get the connected iDevice UDID.
With the below code I could get the connected device udid.
public String deviceUDID() throws IOException{
ProcessBuilder builder = new ProcessBuilder("/bin/bash", "-c", "/usr/local/bin/idevice_id -l");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
line = r.readLine();
System.out.println(line);
return line;
}

Java Runtime.getRuntime().exec() disable network on win 7

how i can enable/disable network with java program .jar
how i can use :
Runtime.getRuntime().exec()
is this can do
Process result = Runtime.getRuntime().exec("ipconfig/release");
thank you
you can help me to fix this
ProcessBuilder pb = new ProcessBuilder("netsh", "Lan", "disabled","name=\"lan\"");
pb.redirectErrorStream(true);
Process process = pb.start();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}

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

java input.readLine hangs after starting a powershell script

We are trying to call a Powershell script via Java but it hangs when we try to read the output of that script. It hangs on "input.readLine()"
Here is a code we have tried:
String command = "cmd /c powershell C:\\_checkouts\\TestPowerShell\\passwordExpirationTime.ps1";
Process p = Runtime.getRuntime().exec(command);
new InputStreamReader(p.getInputStream());
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();;
for (String line = input.readLine(); line != null; line = input.readLine()) {
stringBuilder.append(line);
}
input.close();
String msg = stringBuilder.toString();
System.out.println("msg: " + msg);
We tried looking at this solution Java program hangs when trying to invoke powershell script
but none of those suggestions worked for us.
We also tried it without cmd /c and with cmd /k. We really want this as a generic class where any script could be called. i.e. BAT, BASH, SH, Powershell, etc.
Any ideas?
Thanks to the answer below here is the code that worked:
try{
final ProcessBuilder pb = new ProcessBuilder("powershell","C:\\psFile.ps1");
pb.redirectInput(Redirect.from(new File("NUL")));
final Process p = pb.start();
final int retcode = p.waitFor();
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}catch (Exception e){
e.printStackTrace();
}
Here's some additional information. Our powershell was returning an error because it was restricted. That is why we needed all the Redirect to NUL. Once we fixed that and it wasn't returning an error we were able to simplify our code to this:
try{
final ProcessBuilder pb;
pb = new ProcessBuilder(args);
final Process p = pb.start();
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}catch (Exception e){
e.printStackTrace();
}
Use a ProcessBuilder. Any process implying a modicum of I/O should outright refuse to run via Runtime.exec(), alas, it cannot detect that. And Oracle hasn't flagged it as #Deprecated which is a pity.
Anyway:
final File logFile = new File("/path/to/logfile");
final ProcessBuilder pb = new ProcessBuilder("powershell", "/path/to/the/ps1");
pb.redirectOutput(Redirect.to(logFile));
pb.redirectErrorStream(true);
final Process p = pb.start();
final int retcode = p.waitFor();
// deal with retcode
// read result from the log file

Categories

Resources