Not getting input data stream when execute another process from ProcessBuilder - java

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.

Related

Buffer reader adding "[?1034h" as last line when executing Linux commands in Java

I'm executing a linux command in java using processBuilder but it adds [?1034h as the last line.
It is printing all lines but after my expected last line, it adds another line with those characters.
My code:
ProcessBuilder processBuilder = new ProcessBuilder();
// -- Linux --
// Run a shell command
processBuilder.command("bash", "-c", "sudo /usr/sbin/ilorest load -f "+biossource);
try {
Process process = processBuilder.start();
output = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line);
}
BufferedReader stdError = new BufferedReader(new
InputStreamReader(process.getErrorStream()));
System.out.println("Here is the standard error of the command (if any):\n");
while ((line = stdError.readLine()) != null) {
output.append(line);
System.out.println(line);
}
int exitVal = process.waitFor();
if (exitVal == 0) {
System.out.println("Success!");
System.out.println(output);
System.exit(0);
} else {
//abnormal...
}
I also wrote the output to file and even there it is showing the same.

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

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

How to run Windows commands in JAVA and return the result text as a string [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Get output from a process
Executing DOS commands from Java
I am trying to run a cmd command from within a JAVA console program e.g.:
ver
and then return the output of the command into a string in JAVA e.g. output:
string result = "Windows NT 5.1"
You can use the following code for this
import java.io.*;
public class doscmd
{
public static void main(String args[])
{
try
{
Process p=Runtime.getRuntime().exec("cmd /c dir");
p.waitFor();
BufferedReader reader=new BufferedReader(
new InputStreamReader(p.getInputStream())
);
String line;
while((line = reader.readLine()) != null)
{
System.out.println(line);
}
}
catch(IOException e1) {e1.printStackTrace();}
catch(InterruptedException e2) {e2.printStackTrace();}
System.out.println("Done");
}
}
You can use Runtime exec in java to execute dos commands from java code.
Based on Senthil's answer here:
Process p = Runtime.getRuntime().exec("cmd /C ver");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()),8*1024);
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
String s = null;
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null)
System.out.println(s.replace("[","").replace("]",""));
Output = Microsoft Windows Version 6.1.7600
You can do something like:
String line;
Process p = Runtime.getRuntime().exec ("ver");
BufferedReader input =new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader error =new BufferedReader(new InputStreamReader(p.getErrorStream()));
System.out.println("OUTPUT");
while ((line = input.readLine()) != null)
System.out.println(line);
input.close();
System.out.println("ERROR");
while ((line = error.readLine()) != null)
System.out.println(line);
error.close();
On comment of #RanRag, the main issue is Windows versus Unix/Mac.
WINDOWS: exec("cmd /c ver");
UNIX FLAVOUR: exec("ver");
Have a look at java.lang.Runtime or, better yet, java.lang.Process
This might help you get started.

Categories

Resources