I have a problem relating my web project.
Previously when my professor ask me on doing a online judge application, I chose to implement it using php rather than Java servlet, as I thought java servlet quite complicated.
Then problem comes. For the online judge, I have to use a backend java program on the server to do the processing of user submitted codes. That is, each time a user submits something, php would call the java virtual machine and invokes the java application. However, to invoke the java application, my own way now is to use command line
popen("start java -jar \"$FILE_ROOT/OnlineJudge.jar\"", "r");
This works fine, but considering loading of java virtual machine, it is actually very slow and error prone. So I was wondering if there is any better ways for PHP to invoke local java programs on the server. Because later I found I still need to invoke more java from php.
Any idea would be appreciated. Thanks.
have you considered php-java bridge, this is quite useful, it uses XML to communicate between php and java.
You'll need to start the JVM once and the bridge component will help with communication.
here are some examples, here you can find a simple example to connect php and java
Depending on how the java application is written, it likely wouldn't be too hard to convert it to a servlet. Once it is a servlet, run it under jetty or tomcat, and then just have your php connect to it via CURL and put/get data from the servlet.
Java servlets are fairly simple. If you are using an external framework like Spring it becomes even easier, but if not, you just need to extend javax.servlet.http.HttpServlet, and then configure it in your web.xml.
It's a bit of a hack, but shouldn't take you too long to accomplish.
Also take a look at the exec for command line executions. I used it to shut down my computer after doing an extensively long task well into the night.
Related
As node.js still lacks important functionality which exists in Java, I would like to use Java instead of node.js, and create the client using a web language (html, js, css..).
Electron is cross platform and so does java so it seems fit to have a solution getting the best of both worlds.
Does someone know of a way to integrate electron with java or have a different solution to the problem?
I made something similar, Java back-end with Electron GUI.
You can do it in more ways, it depends on what you need.
You can create a jar file and then execute it like terminal:
https://nodejs.org/api/child_process.html
Or you can open a socket communication and talk on a Port. (A lot of documentation:
Java (web)socket - Node.js client.io)
In this second way, you can do everything you want, but you have to create your communication protocol.
Your path is not foolish, I am very satisfied of the communication and usage in my work with Java + Electron .
I've created a small PoC where Java process is integrated with Electron front-end: https://github.com/jreznot/electron-java-app There you will find a simple TODO List application built with Vaadin/Jetty and Electron.
Personally i made my back-end java communicate with the front-end by creating a file with te data then sending it to the main.js to be processed.
DBus and winDbus seems to be an option here.
It creates nice abstraction and separation between "frontend" and backend
https://sourceforge.net/projects/windbus/
I'm going to test it on my own soon
Is there a way to run a Java Applet in node.js, at server side?
It may sound crazy, but I am looking for a way to run a third party java applet on my node.js app server.
It is used to do a weird calculation that I am not willing to migrate to JavaScript code. Is this possible in any way? Any suggestions? I am in need to send the result of this calculation as a web page to the user, based on a request param.
I was initially thinking of how can I communicate between node.js and an Java Applet running standalone. Googled a bit but no success at all. Can someone suggest how to better google for it or point some good resources???
Using JNI will not help you as applets will not run in a headless environment. Applets are directly dependant on AWT so require a GUI environment to run.
Presuming that the license issues of the applet allow you to reuse the code, then my solution is to decompile the classes for the applet and use the generated java to create a normal java class with main that will run the desired calculations and return the result.
Unless the code is obfuscated, it's relatively simple to understand the decompiled code and should be relatively painless to adapt for what you wish to do.
Once you have your java code working from the command line you can then use the node-java project to call the java code from node.js and get your result.
I have a situation. A device connected to PC(client side) via COM. The vendor provide me a dll to exchange data with the device. I would like to create a java web-app to collect data from many devices(connected to backend through PCs). So how can I call dll from a servlet? Thanks in advanced!
The straight-forward solution is using JNI or JNA. You should learn appropriate tutorial from Oracle to learn how to do this.
But probably you can do it easier. If for example this DLL is ActiveX you can create script (VBScript or JScript) and then run it from java using utility named cscript. Other possibility if this DLL already knows to run as a stand alone application (or you have separate command line app that runs this DLL and provides you CLI.). In this case I'd recommend you to use it unless you have serious performance constraints. It is much easier to run command line application from java than coding JNI.
I'm looking for a way to call a COM port from a webpage.
I was thinking abut running a Java WebStart (or Flash?) program that opens a local web server that allows to interact with the COM port using JSONP.
Are there any show stopping security restrictions on the way that I don't know of? This should be possible:
use native libraries (Java COM bridge) from Java WS application
open a local port
access local port from javascript, likely using <script> tags
do all this without scaring users with "This website is trying to do something really nasty, get off as fast as you can" kind of messages :)
I've used a Java COM bridge before, so this shouldn't be the problem - at least as soon as I'm able to run native code.
So how would my JNLP file have to look like to get this working? Any alternatives to Java WS? Better install it as a daemon?
Impossible. This violates all sorts of very basic security principles, especially the part about do all this without scaring users with "This website is trying to do something really nasty, get off as fast as you can" kind of messages
This is EXACTLY what those messages are intended to prevent.
This article answers the first part of my question, the WS to COM approach that is: Juggling with DLLs, WebStart and Maven
Accessing an HTTP server on a local port should also be feasible as that's what Playdar does for instance.
What are you trying to achieve?
You need a standalone software that does the job. The user will have to install it. Like the security pathches.
I want to call Java from PHP 5.2, running either on a webserver or from a command line script.
From PHP 4, this seems to be simple, and just involves installing the PECL Java extension.
Example code from the PHP 4 extension:
<?php
// get instance of Java class java.lang.System in PHP
$system = new Java('java.lang.System');
// demonstrate property access
echo 'Java version=' . $system->getProperty('java.version') . '<br />';
?>
However, this extension doesn't exist on PHP 5.
What is the closest alternative for PHP 5?
edit:
Really I'm looking for an interface similar to either a C PHP extension or to that provided by the PHP 4 Java extension. The Java program is fairly small and only needs to retain a small amount of state between calls and doesn't need to run asynchronously. The PHP script would only be running a small number of instances simultaneously.
This would also need to be deployed to multiple machines (running Ubuntu 9.x and Debian Lenny), so it should be simple to install.
This project seems to be a good bet: http://php-java-bridge.sourceforge.net/pjb/
Since you want to keep state at the java site I'd either use a java process that listens on a plain socket or use a simple embedded webserver (winstone or jetty) if you are more fluent writing servlets. Some other possibilities are listed at this related question: What is the best approach for IPC between Java and C++?
Now quite what you asked for, but you could have a look at Caucho's Resin, and in particular their Quercus which is a 100% Java implementation of PHP, it allows integration of Java and PHP.
I have a project that uses a Java program to fetch some data. It's quite simple, but I found that
$java_command="cd /var/java_dir && java my_java_program $myparam1 $myparam2";
$result=exec($java_command,$output,$return_code);
works just fine.
Zend Server also has a built-in daemon allowing you to access Java functionality from within PHP. It works right out of the box.