Please tell me how to retrieve the status of autosys command from java program.
we can execute the commands using Runtime.getRuntime().exec(command) but how to get autosys status.
I am not sure about the autosys commend but if you can execute using the Runtime then you need to see the output of execution of command. If autosys will return something upon execution you will get it in out InputStream or error in ErrorStream in case of error.
Try something like this:
public static void printStream(InputStream is, String type){
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(type + ">" + line);
} catch (IOException ioe){
ioe.printStackTrace();
}
}
public static void main(String args[])
{
String cmd = "command to execute";
Process proc = Runtime.getRuntime().exec(cmd);
printStream(proc.getInputStream(), "OUTPUT");
printStream(proc.getErrorStream(), "ERROR");
}
Process has a method exitValue() to get the exit value for the subprocess. exitValue
Example:
Runtime rt = Runtime.getRuntime();
Process process = rt.exec("ls -al");
process.waitFor();
System.out.println(process.exitValue());
Related
I've been trying to read lines of console output from a command in realtime, but I've only been able to get it after the process has ended. How could I run a command and get the output as it runs?
Here's the code I'm currently using
public OutputStream executeCommand(String command) {
try {
ProcessBuilder pb = new ProcessBuilder(command);
Process p = pb.start();
OutputStream ops = p.getOutputStream();
return ops;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Be aware that the code below can throw an IOException.
Runtime runtime = Runtime.getRuntime() ;
Process process = runtime.exec("Your command") ;
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())) ;
String line = null ;
while ((line = reader.readLine())!= null){
System.out.println(line);
}
I'm trying to execute batch file in java.
My source is below:
List<String> comm = new ArrayList<String>();
comm.add("cmd");
comm.add("c:/Users/cointreau/workspace/pmd-bin-5.3.2/pmd-bin-5.3.2/bin/pmd.bat");
comm.add("-d");
comm.add("C:\\Users\\cointreau\\workspace\\counter\\src\\Counter.java");
comm.add("-f");
comm.add("xml");
comm.add("-R");
comm.add("java-codesize");
comm.add("-r");
comm.add("C:\\Users\\cointreau\\workspace\\counter\\report.xml");
ProcessBuilder probuilder = new ProcessBuilder( comm );
Process process = probuilder.start();
//Read out dir output
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
//Wait to get exit value
try {
int exitValue = process.waitFor();
System.out.println("\n\nExit Value is " + exitValue);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
the original command line is this,
c:/Users/cointreau/workspace/pmd-bin-5.3.2/pmd-bin-5.3.2/bin/pmd.bat -d C:\\Users\\cointreau\\workspace\\counter\\src\\Counter.java -f xml -R java-codesize -r C:\\Users\\cointreau\\workspace\\counter\\report.xml`
pmd.bat is the batch file what i want to execute and the remainders are just parameters for the bat file.
The only output I can see is just exit Value is 1.
When I execute this command line in cmd, it runs properly but not in my java source.
What should I do?
Thanks for your help in advance.
Try adding the /C option to carry out the batch command
comm.add("cmd");
comm.add("/c");
comm.add("c:/Users/cointreau/workspace/pmd-bin-5.3.2/pmd-bin-5.3.2/bin/pmd.bat");
...
I am using correct practices mentioned in this article:
http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2
I read the Output and Error streams in concurrently. However, when reading the Output stream of the process, it hangs on readLine() after the last line. I have no idea how to work around this. It is the line of the process and it just hangs there.
The command that hangs this is paexec \192.168.1.92 -c -f C:\Windows\ITBBsync0.bat.
Inside the batch file there are several line such as the following: devcon.exe status =USB > C:\Windows\output.txt
When I execute it on command line, process exits with code 0. When I execute it in Java it hangs after reading the last line of output (which is basically the last line of the batch file). I believe the process is not exiting which is why the issue occurs.
import java.io.*;
class StreamGobbler extends Thread
{
InputStream is;
String type;
StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ((line = br.readLine()) != null){
System.out.println(type + ">" + line);
//hangs after reading last line.
}
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
Also the method that uses the StreamGobbler is the following. There is obviously a parent class which executes this method.
public static boolean cmd(String command){
try{
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + command);
Process p = rt.exec(command);
// any error message?
StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(),"ERROR");
StreamGobbler outputGobbler = new StreamGobbler(p.getInputStream(), "OUTPUT");
errorGobbler.start();
outputGobbler.start();
int exitVal = p.waitFor();
if (exitVal != 0){
System.out.println("ExitValue: " + exitVal);
System.out.println(command + "EXIT CODE:" + exitVal);
return false;
}
return true;
}catch(IOException ioe){
ioe.printStackTrace();
return false;
}catch(InterruptedException ie){
ie.printStackTrace();
return false;
}
}
I have visited many threads on StackOverflow and as far as I know I am using best practices for this. Please let me know how I can fix this or why this may be happening.
I can't give you exactly the application I am executing because it is a bit complex, but the application performs some network operations and also executes a batch file which in turns contains more operations.
I have this program.
try {
Runtime rt = Runtime.getRuntime();
//Process pr = rt.exec("cmd /c dir");
Process pr = rt.exec("java -jar C:/sample/sample.jar D:/pdftest.pdf");
BufferedReader input1 = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=null;
while((line=input1.readLine()) != null) {
System.out.println(line);
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);
} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
Here , process pr defines a command. Now my question, I want to replace the part "D:/pdftest.pdf" to a variable like
String pdfvariable="D:/pdftest.pdf";
So I should be able to replace hard coded value of "D:/pdftest.pdf" to pdfvariable.
Is it possible? Can anyone please explain it to me?
Thanks
String pdfvariable = "D:/pdftest.pdf";
Process pr = rt.exec("java -jar C:/sample/sample.jar " + pdfVariable);
I'd like to get the output from a long running shell command as it is available instead of waiting for the command to complete. My code is run in a new thread
Process proc = Runtime.getRuntime().exec("/opt/bin/longRunning");
InputStream in = proc.getInputStream();
int c;
while((c = in.read()) != -1) {
MyStaticClass.stringBuilder.append(c);
}
The problem with this is that my program in /opt/bin/longRunning has to complete before the InputStream gets assigned and read. Is there any good way to do this asynchronously? My goal is that an ajax request will return the current value MyStaticClass.stringBuilder.toString()
every second or so.
I'm stuck on Java 5, fyi.
Thanks!
W
Try with Apache Common Exec. It has the ability to asynchronously execute a process and then "pump" the output to a thread. Check the Javadoc for more info
Runtime.getRuntime().exec does not wait for the command to terminate, so you should be getting the output straight away. Maybe the output is being buffered because the command knows it is writing to a pipe rather than a terminal?
Put the reading in a new thread:
new Thread() {
public void run() {
InputStream in = proc.getInputStream();
int c;
while((c = in.read()) != -1) {
MyStaticClass.stringBuilder.append(c);
}
}
}.start();
Did you write the program you're calling? If so try flushing your output after writing. The text could be stuck in a buffer and not getting to your java program.
I use this code to do this and it works:
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while (true) {
// enter a loop where we read what the program has to say and wait for it to finish
// read all the program has to say
while (br.ready()) {
String line = br.readLine();
System.out.println("CMD: " + line);
}
try {
int exitCode = proc.exitValue();
System.out.println("exit code: " + exitCode);
// if we get here then the process finished executing
break;
} catch (IllegalThreadStateException ex) {
// ignore
}
// wait 200ms and try again
Thread.sleep(200);
}
Try :
try {
Process p = Runtime.getRuntime().exec("/opt/bin/longRunning");
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line); }