I am using PHP 5.5 with Apache 2.4 and Java 8. Now I want to call the function within a Java JAR file. I search the internet about the PHP Java bridge. I have found a JavaBridge.Jar file for the SourceForge, but I don't know how to make it works. So how can I use PHP Java Bridge. I found by Google saying it may use PHP exec() to execute the Java function, however, this method seems need to open the JVM each time. So I am wondering whether it has a better method to call function in JAR.
You have to have the jvm running at some point if you want to execute your java function - you are correct that running the jvm many times is not ideal.
PHP's exec call simply runs an external program (resident on the server). If you write a complete Java application, your program could then call whatever function you want. This, of course, will result in the jvm having to start for each instance of the program (someone can correct me if there will be pooling, but I suspect not).
A better solution would be to work out a way to keep a single java program running indefinitely, and communicate between your PHP code and that one instance of your java program (which does the function calling). A pretty straight-forward way to do this would be with a Socket (PHP) that connects to another Socket (Java "ServerSocket"). Build a java application as a 'server' that accepts requests from your PHP 'client'.
Google has been very helpful to me in the past in getting started with network communication with java/etc.
The library that you mentioned looks like it does something like this for you, but it looks like you will need to have some sort of java application available to connect to - not just a raw jar.
Related
I'm writing a web server by Java. This server is just to use socket to get the connection from client, parse the request and call the corresponding handler to handle this request. But I want to make a page using Django. However, I don't know how to run python script when the server get the request, and Django site tell me that it can be used in Apache by wsgi, so I guess I can do it as well, but how to implement it?
To get a good idea of what is WSGI you can read the official documentation (PEP-333). But basically WSGI is a big hash table that you pass to your WSGI application.
Since you are writing your server with Java an important part of your project will be to figure out how to call python code from inside your Java code.
But if you avoid writing this Java web server, you can tale a look at other tools that will give give your a better support to run your python WSGI app.
Apache with mod_wsgi
Mongrel2 with wsgid
Nginx with gunicorn
Good luck!
It is a bit confusing as to what you are really after. If you want to run Python WSGI applications, including Django on a Java server, then look at Jython and modjy.
http://opensource.xhaus.com/projects/show/modjy
I have a matlab function written in an m-file. (it's not written by me and seems a complex algorithm) Now I want to use it with a java application. I searched in internet and learned how to run matlab codes inside java.
For example here it says how to do that. But I can't understand how to use the exact matlab function as it is in java without knowing what function does. Can someone help me to figure this out?
I am not sure that MatlabControl is what you need. It merely runs Matlab as server and sends it commands. You will not be able to give it to your users, unless they are all willing to pay for a Matlab installation.
If you want to deploy your application, consider using Matlab Builder JA.
From the website:
MATLAB Builder™ JA enables you to create Java™ classes from your MATLAB® programs. These Java classes can be integrated into Java programs and deployed royalty-free to desktop computers or Web servers that do not have MATLAB installed.
Also, if you use MatlabControl, there might be a compatibility problem in the future - check this out:
This API relies upon the Java MATLAB Interface distributed with all recent copies of MATLAB. This interface is entirely undocumented and there is no guarantee matlabcontrol will work with all versions of MATLAB and operating systems. Efforts are made to ensure compatibility with MATLAB R2007b and greater, see here for specifics.
I have a java web application running under tomcat in a Sun java 6 JVM. Is there a way to attach a scala or clojure REPL to the running JVM ?
Once the webapp is up and running, the context in which the calls are to be made is already setup in the running VM. Hence, this can be really helpful in invoking arbitrary java method calls for incremental, exploratory development and for debugging.
Copied over the answer from the other question as per your request:
liverepl: Connect a Clojure REPL to running Java or Clojure processes without any special setup
From the website:
Start a Clojure REPL connected to any running Java or Clojure process
without needing the process to be setup in any special way beforehand.
Now supports connecting to Tomcat web applications.
You can use the repl to run code, inspect variables, and
redefine Clojure functions.
https://github.com/djpowell/liverepl
There's no way to attach a REPL to an already-running JVM, so you need to think about the problem backwards...
Start the REPL on the server, either when the program is launched or in response to a inbound client connection (using e.g. Telnet).
There's a good example here: http://blog.homecloud.ca/2010/03/scripster-interactive-scala-repl-using.html
You could also host an Interpreter instance in your server, then expose a web interface. This is the approach taken by http://www.simplyscala.com/
If you're thinking of debugging, then you'd be better served by this answer: Drop into interpreter during arbitrary scala code location
You could use this:
https://github.com/wirde/swank-inject
Allows you attach a Clojure repl to a running java process (with remote debugging enabled).
You are also able to specify a number of classes (singletons) for which instances will be found and bound to a symbol available in the repl.
For Scala REPL connecting to other JVM, take a look at Scalive.
Have you experience of integration of php and Java? I've written a class in Java "Hello". Now I want to call its method from php when I create an instance of this class from php with
$obj = new Java(Hello);
Then it gives me fatal error. I've tried many times to setup php ini variables etc but I couldn't find anything. Please advice me how can I set it up.
Thanks in advance
Reference: http://php.net/manual/en/book.java.php
Have a look at:
PHP-Java Bridge
The PHP/Java Bridge is an
implementation of a streaming,
XML-based network protocol, which can
be used to connect a native script
engine, for example PHP, Scheme or
Python, with a Java virtual machine.
It is up to 50 times faster than local
RPC via SOAP, requires less resources
on the web-server side. It is faster
and more reliable than direct
communication via the Java Native
Interface, and it requires no
additional components to invoke Java
procedures from PHP or PHP procedures
from Java.
Could it be you are just missing the quotes around the Java Class? Also make sure you actually do have a Java VM on the machine you are trying to run the code on and make sure you have properly set up the config options. Also, the Java extension is a PECL extension and has to be installed. It is not part of the native distribution. Furthermore, the extension is considered unmaintained and dead, so you might want to have a look at the JavaBridge suggested elsewhere. Zend Server also brings a JavaBridge.
Check out this article: http://www.devshed.com/c/a/PHP/Using-PHP-with-Java/
They say it's possible (and how to do it). I've never done it, so I can't vouch for it...
In my experience the best way to interact between Java and PHP is through a WebService layer. NuSoap for PHP is very good, and you can consume the webservice in Java to create a stub that will build the request/response object.
There are many ways few are:
PHP Java Bridge
Java Integration in PHP
Using web services to pass data in between like SOAP
Using simple HTTP data passage between Java and PHP see (facejar uses the same technique)
Client site script (AJAX)
If you are after security, use option 1 otherwise I prefer using option 4 which is a bit faster when it comes to performance.
I'd like to log or record every time I start an application to gain insight into which applications I use most on my Windows system. I was thinking I could create an event in the event log and listen for it in a .Net program.
Questions:
Is this the best way to solve this problem?
If so, which .Net library should I use?
I am also open to using Java to solve this problem. Thanks!
In .NET, you could probably create a shell extension which you would register for EXE programs which would really just be a filter for the EXE extension. When your shell extension is called, you would execute the program (by invoking the old functionality) after you logged your information.
Note, however, that you can ONLY do this with .NET 4.0 or above, which is currently in beta. Because of the way that previous versions of the CLR worked, only one version was allowed to run in a process at a time (including explorer, the OS process).
.NET 4.0 introduces Side-by-Side (SxS) CLR instances within the same process, so it is safe to use it from .NET 4.0 on as a mechanism for shell extensions.
It will also require a good deal of COM interop, but it can be done.
In regards to LWoodyiii's comment asking if this can be done in older versions of .NET: Could it? Yes, it can be done, but the official decree from MS is that you shouldn't. The reason for this is because if someone else decides to run a shell extension, or interface with the OS in some way using .NET, and the version is different from the one that you are using, you run the risk of hosing the OS process.
maybe you can try to hook the CreateProcess API in system wide using unmanaged c++.
and in C# use .NET interop to handle events/notifies from you unmanaged hook module.
related links:
http://www.codeproject.com/KB/system/hooksys.aspx?msg=1322916
http://www.madshi.net/madCodeHookDescription.htm