Live output from .jar executed by PHP - java

I have a php-script which executes a .jar-file:
<?php
passthru("java -jar nlp-server.jar 9000");
?>
I want to display the output generated by this .jar-file on a website. The problematic part is the fact that the .jar-file doesnt finish executing because its a server-application.
Thus navigating to the php-file wont help and I didnt manage to make AJAX work either.
Is there a way to display the "live" output of the .jar-file on a website?

You have to use
system();
system() is just like the C version of the function in that it executes the given command and outputs the result.
http://php.net/manual/en/function.system.php

Use it directly via (if you can) : <applet code="test.class" archive="nlp-server.jar" width=120 height=120>
This is the only way I know because the (java) script must finish in order to display results via PHP else you will get execution timeout...

Related

JavaFX show looping Python print output

I built a GUI in JavaFX with FXML for running a bunch of different Python scripts. The Python scripts continuously collect data from a device and print it to the console as it's collected in a loop at anywhere from around 10 to 70 Hz depending on which script was being run, and they don't stop on their own.
I want the end-user to be able to click a button on my GUI which launches the scripts and lets them see the output. Currently, the best I have done was using Runtime.exec() with the command "cmd /c start cmd /k python some_script.py" which opens the windows command prompt, runs python some_script.py in it, and keeps the command prompt open so that you can see the output. The problem with this is that it only works on Windows (my OS) but I need to have universal OS support and that it relies on Java starting an external program which I hear is not very elegant.
I then tried to remedy this by executing the python some_script.py command in Java, capturing the process output with BufferedReader, creating a new JavaFX scene with just a TextArea in an AnchorPane to be a psuedo-Java-console and then calling .setText() on that TextArea to put the script output in it.
This kinda worked, but I ran into many problems in that the writing to the JavaFX console would jump in big chunks of several dozens of lines instead of writing to it line by line as the Python code was making Print() calls. Also, I got a bunch of NullPointerException and ArrayIndexOutOfBoundsException somewhat randomly in that Java would write a couple of hundred lines correctly but then throw those errors and freeze the program. I'm pretty sure both of these issues were due to having so much data at such high data rates which overflowed the BufferedReader buffer and/or the TextArea.setText() cache or something similar.
What I want to know is what approach I should take at this. I cannot migrate the Python code to Java since it relies on someone else's Python library to collect its data. Should I try to keep with the pseudo-Java-console idea and see if I can make that work? Should I go back to opening a command prompt window from Java and running the Python scripts and then add support for doing the same with Terminal in Mac and Linux? Is there a better approach I haven't thought of? Is the idea of having Java code call Python code and handle its output just disgusting and a horrible idea?
Please let me know if you would like to see any code (there is quite a lot) or if I can clarify anything, and I will try my best to respond quickly. Thank you!
My solution was to still call the Python code from the Java Processbuilder, but use the -u option like python -u scriptname.py to specify unbuffered Python output.

Java - create a php file, execute it and get output

In the JAVA application I'm developping, I need to execute on the server side a piece of PHP code sent by the client.
I found some intresting libraries but they don't return the output when there are syntax errors.
As an alternative, I thought of creating a php file, adding the content to it, then execute it using ubuntu command lines.
What I don't really know, is how to get the output of the execution, and when executing the code using terminal, what's the path of the php file that I created in java.
If you have any other ideas that would be great
I don't know much about Java but if you're going to execute php using terminal then you can use standard output redirection
php fileThatYouUploaded.php > someTemporaryFileToGetOutput.txt 2> tempFileWithErrorsIfAny.txt
or if you want the output and errors in the same file:
php fileThatYouUploaded.php > someTemporaryFileWithOutputAndErrors.txt 2>&1
As a side note: trusting a code from a user is a very bad idea. You would be better with some kind of library that will allow you to whitelist/blacklist functions and classes or even run it in closed/sandboxed environment.

Run PHP script from Java and capture the output

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.

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.

Download web page using Java after javascript executes

I have a problem that doesn't seem to be answered clearly in StackOverflow. I want to download a page using Java and retrieve some data from it in order to give some values to an application that I develop. This page is a betting site so it contains javascrit methods to change the betting values.
In order to do some tests I downloaded the page manually using Ctrl-S and then I made a programm (with FileReader, BufferedReader, etc...) which retrieves the data. This worked perfectly. So I would make a bash script to be executed in order to download the page every time when the user opens my application.
After that I searched for methods who download the page programmatically (I used Jsoup, URL, ...). What I noticed is that the javascript variable values couldn't be printed because the javascript code wasn't executed.
What i want to know is that if there is some way to download programmatically the executed website (download the instance of the javascript values) without having to make some bash script to do it every time before someone opens my app.
Try HtmlUnit. It is used for automatic testing but should fit your purpose well too!

Categories