If i am using following command in java:
Process ps = Runtime.getRuntime().exec("some .exe file of VB");
How do I know that the particular .exe has done its job eg: it executed successfully.
How do i know that it has some error or just completed half task in java.
How should I design my program in java to know or Is there any way to tell java from VB.
Any help is appreciated.
I would assume that you could look at the exit status of the program: ps.exitValue() or you could read the stdout/stderr ps.getInputStream() / ps.getErrorStream() respectively.
You get back a Process
http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/java/lang/Process.html
Which has such methods as:
exitValue()
getErrorStream()
waitFor()
Which will get you what you need
Related
Can any one tell me how to get status report from shell script to java code.
Use case : I run a shell script from java code and if I get any error in shell script(while running) then I need to send some message from shell script to my java code.
So how I can acheive that.
I'm sure there is a better way, but an easy way to do this is to have the shell script pipe error output to a file, which you then read by the Java application.
I'm assuming you are using a ProcessBuilder launch your shell script.
Once you build the process you can use getInputStream() which will give you the output stream from the Shell Script, then just copy the stream to where you want to go.
There also is a redirectOutput but I have limited experiences using it. I'm assuming it does what I'm describing above.
You can use something like this:
Process proc = Runtime.getRuntime().exec("ls -la")
// To get the error code (0=success)
int outCode = proc.exitValue()
If you need to send something from the script to the application executing it, you may be able to do that writing to stdErr in the script and using proc.getErrorStream in your java program.
I want to run linux script from Java program and continue to execute program only when script stop. I am not interested to read script output ... Can anybody help me?
Thanks a lot,
and excuse me for my bad English
Assuming all other threads are idle:
// run the script.
Process proc = Runtime.getRuntime().exec("/path/to/myscript");
// wait for the return code.
int ecode = proc.waitFor();
If you have more complex arguments to your script, or it needs to monitor STDOUT, STDERR, or needs other modifications (like feeding data to STDIN, or changing execution directory, environment variables, etc.) then you should do the same effective procedure, but instead of using Runtime.exec(...) you should build and start the Process manually. Read the Process javadoc and ProcessBuilder javadoc on how to set it up, and start it.
You can also launch the bash interpreter instead
Process proc = Runtime.getRuntime().exec("/bin/bash /path/to/myscript");
int ecode = proc.waitFor();
This may work in some generally broken cases when #rolfl solution may not work (non executable script file, #!/ header missing, etc)
I have a shell script test.sh that does:
cat /home/tomcat/temp/tempLogFile.log > /home/tomcat/temp/logFile_test.log
I call test.sh from java by Runtime.getRuntime().exec("test.sh")
It creates the logFile_test.log but nothing written to it.
If I run the script directly from shell, it works fine.
What could be going wrong?
Thanks,
UPDATE: Interstingly, it works fine if I do
echo 'cat /home/tomcat/temp/tempLogFile.log > /home/tomcat/temp/logFile_test.log' | at now
But I cant use at now since I need to wait for cat to finish
Maybe your command path is messed up? That command will create logFile_test.log even if it can't run the cat program. Try adding something like:
set > /tmp/dump.txt
to the shell script. This will write a copy of the process's environment variables to /tmp/dump.txt. Inspecting those might help you figure out the problem.
Another possibility is that the disk is full. In that case, you'd be able to create empty files, but not write any data to them.
Once you invoke the getRuntime().exec("ANY_COMMAND"); , it returns you an instance of Process, using process object you can get Input and Output Streams and then pass any other command to the same process or read the output , for that purpose you have three streams connected
Output Stream :- Gives you Control to send more arguments or commands to the same process
Input Stream :- Gives you Control to read the values from the stdout of the process
Error Stream :- Gives you all the errors that were occured during execution of any command.
even if you have any errors, you can check the error stream and see whats going wrong.
you may refer Java process.getInputStream() has nothing to read, deadlocks child
I have a java project built that I want to run from another java program. But when I call it using the exec command it does not execute immediately but waits for the current program to end. If I add a waitFor statement then the Program hangs as the main program waits for the process and the Process is waiting for the Main program. Does anyone know how I can solve this? Or why it is behaving in such a manner? I need this jar file to execute before a second one can.
Runtime.getRuntime().exec("java -jar \"JavaProject1/dist/JavaProject1.jar\"");
System.out.println("Hello");
p.waitFor();
The location of the jar file is fine and it prints the hello. The main class for the jar file I want to run is in this thread at DaniWeb
Amongst other things, you have to keep reading from the processes STDOUT and STDERR, otherwise it will block.
See, for example, http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
why it is behaving in such a manner?
Because You are creating deadlock in your own program.
When you add waitFor() your program main() in this case waits for the external process to get finished.
It returns Zero when the external process gets fnished normally.
May be the external program has some issues
I am trying to write a simple application that takes in a command line arguement (which will be a Powershell ps1 file) and then run it. So I have experemented with a number of different approaches and seem to be running into a problem. If I attempt to invoke powershell from within java, the windows process is started and is visible via process explorer, however powershell never returns, it hangs in some sort of loop by the looks of it. The command I am using is:
String command = "powershell -noprofile -noninteractive \"&C:\\new\\tst.ps1\"";
The command is then executed using:
Runtime systemRuntime = Runtime.getRuntime();
Process proc = systemRuntime.exec(command);
At the moment I am hard coding the location to the ps1 file as I was trying to rule this out as an issue. Using a process explorer I can see the hanging powershell process and the command that was passed to it was :
powershell -noprofile -noninteractive "&C:\new\tst.ps1"
which when copied into a cmd window, works to launch the tst.ps1 file. The file itself is incredibly simple in this example and I think I can rule it out being the cause of the freeze as I have tried to launch other ps1 files the same behaviour can be seen.
To further add to the confusion, if I use the java code posted above and pass in powershell commands instead of a file name then it successfully runs.
I've scoured the web and see lots of people experiencing the same issue but no one seems to have posted there solution, I hope its a simple oversight on my part and can be easily fixed.
Any hints/tips are appreciated :D
Alan
You have to close OutputStream in order for Powershell to exit.
Runtime systemRuntime = Runtime.getRuntime();
Process proc = systemRuntime.exec(command);
proc.getOutputStream().close();
Is your external program writing to the standard outputs (err and out)?
If yes, it can hang waiting for you to consume them from the java parent process.
You can get those as InputStreams by calling
Process.getInputStream()
and
Process.getErrorStream()
There's more details here:
Javadoc for Process