Run PHP script from Java and capture the output - java

I need to run a PHP script from a Java class and get the output of the script in order to notify the end user of the results. I will use Runtime.getRuntime.exec("php "); in order to execute the PHP script, but how to capture the console output?
Any suggestions?

The output of the process can be found by using getInputStream on the returned Process from the exec call.

Related

How to give input to a process builder

In my program, I am running a command using Processbuilder.
The command is actually to execute python script. whatever the script printing, I am just showing it into the console by printing the input stream.
the problem is, inside the python script there is an if-condition the condition excepts Yes or No to continue the script.
I don't know how to give input Yes or No in-process builder.
Please help me to solve the problem.
The easiest way is using inheritIO().
With this option, you can use the java console as stdio for your python program. Your java program will run the python code and console used for both java and python program, so you can give input via java console.
ProcessBuilder test = new ProcessBuilder("python", PATH_OF_YOUR_PY_PROGRAM);
test.inheritIO();
Process process = test.start();
process.waitFor();
notice: inheritIO() uses the console for both input and output so by activating this option the whole python output will print in java console too.

Invoking bash from Java interactively

I have a Java application which invokes (using Runtime.getRuntime().exec) a bash script like this:
read -e -p "Are you sure you want to do this? (y/n)? "
echo "$REPLY"
The problem is, I never get the prompt part from the bash script (the "Are you sure..." text).
Is there a way to invoke an interactive bash script from Java so that I can read its output? I need it in order to be able to determine which question I'm being asked by the script (in reality it's a much bigger script than described here).
If I run the process from Java through SSH channel with the -t flag (accent on the t flag, without that it won't work), it works fine. But I would like to avoid having to ssh to myself in order to run the script properly, or more precisely, to read its output properly.
From the bash man page when talking about read:
-p prompt
Display prompt on standard error, without a trailing new‐
line, before attempting to read any input. The prompt is
displayed only if input is coming from a terminal.
So you should make sure you are also capturing stderr if you want to see that message. You can perhaps do this when invoking it from java by adding 2>&1 but I'm not positive that java's invocation will honor that.
If you're using ProcessBuilder to invoke it, you should add a call to redirectErrorStream(true) on the process to get stderr visible via stdout reading.
I have not tried to verify this myself, but this page suggests redirecting stdout for the process to /dev/tty to make it think it's connected directly to the tty for the java process. So you'd add a &>/dev/tty to redirect stdout and stderr to /dev/tty, and hopefully that will get it to show up--though that may have it show up on stderr of the controlling Java process instead of the subprocess. I'm not too sure about that.
If you can't do that, another thing to consider would be to try to modify the script so it does an echo -n "<msg>"; read REPLY so the prompt will be displayed on stdout by echo instead of not at all by read
OK, here' the deal.
As Eric pointed out, prompt is displayed only if input is coming from a terminal, so I needed a pseudo terminal to fool the bash script. I managed to do it by using JPty library.

Launch and interact with a command line program from a Java program

The Java program has to launch few prorams that are launched using a command promt(one of them is nginx). How could I handle and send commands to the program from my Java application?
I found this library http://commons.apache.org/proper/commons-cli/usage.html But I'm not sure how it helps..
I do NOT need code. I need an explanation on how things like these work.
Well keep in mind its never a nice solution.
You act like you would be on a command line so you execute Commands like you would on the shell. And does always depend on your platform.
You said you don't want code, I will give it to you anyway ;)
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("service nginx start");
That is done with plan java.
I highly advise you to use a script language for that. Thats just not Java.
additional info:
One thing to remember is to use the streams on process to send input and check output (from Process class)
abstract InputStream getErrorStream()
Returns the input stream connected to the error output of the subprocess.
abstract InputStream getInputStream()
Returns the input stream connected to the normal output of the subprocess.
abstract OutputStream getOutputStream()
Returns the output stream connected to the normal input of the subprocess.
If you need to execute shell commands, this can be achieved like so (This example uses bash as the executing process)
Process p = Runtime.getRuntime().exec(new String[]{"bash","-c","my_script.sh"});
You'll have to write your own script for this, but you don't even have to put it in a file. If you wanted to give user-like input to a command, keep in mind that you can pipe in the result of an echo to a command and it will act as a user typing that command. e.g
echo 1234 | pinTaker.sh;
This will effectively "type in" 1234 to the pinTaker script. This can also be used for things like typing in a password for ssh (Though this is not a good idea, it's a good example..)

Showing the output of a shell script on the browser

This is my first question here,so pardon if somewhere i go wrong.
i am trying to call a shell script on the server from the html page using Ajax.
The Ajax function is calling the java action and this java action class is invoking a shell script.
The shell script will be writing the outputs as it runs.
I want to show that output on the browser in the same way it appears on the shell.
the way i am doing it now is writing the output of the shell in a file and then show up the content of that file on the web page,but all i want is to show the outputs on the webpage as soon as the scripts writes it .
The script takes 2 minutes to run and while running ,it keeps on writing the outputs on the shell.
Thanks
One way would be to start() the shell script via the ProcessBuilder class.
Once you have the Process instance, you can obtain its standard output and standard error streams via getOutputStream() and getErrorStream() respectively.
These InputStreams contain the output you are looking for. As you read them you can then write their output to your servlet or JSP's response output stream. You'll need to do something like waitFor() the process and create thread(s) to read its output and write to your servlet's response.

Gathering output from a Java app and shell command

I'm writing a shell script that's supposed to do the following.
- run a Java application that produces output
- run a shell command that produces output
- gather both outputs and send them out in an email
I have control of the source code of all the steps above.
Is there a best practice in gathering output from different sources? Should I redirect everything to a single temp file? Should I write different output to different files then concatenate them? What are the pros and cons of each approach?
If you don't worry about using just Java you could use the Runtime class to execute the shell command withing java (through exec) command, this will return you a Process object on which you have either getInputStream and getOutputStream so you will then be able to process the output of both the Java program and the shell command inside just one place and do whatever you want (keeping it in memory and directly send it by redirecting the outputstream to the inputstream of what you use to send the mail, with another exec) or saving it or whatever.
I'd favor using a second wrapper script which
calls the java program
calls the shell script
Captures output to a single file
reformats that output
suitable for mailing out Actually
does the mailing
Assuming you are using a unix shell, mailing/formatting and shell script calls are much simpler from the command line.

Categories