Determine input source to Java program - java

I have an AutoHotKey script that sends keystrokes to a running Java program on my computer. How can the Java program tell that the keystrokes came from this specific script? I would imagine it has to do with Java Runtime and Process APIs.

There is no native way for Java to tell if the click has come from external source unless the external source notify the recipient application of itself through some hidden input.
Java runs in a virtual machine and the underlying platform is exposed in a limited way compared to native language as C / C++
If a robot/macro/script outside of the JVM is using lets say java REST API or SOAP it can write a header , or something different to report itself to the recipient program. If you have a desktop client you can do something similar by setting a hidden field or some other similar mechanism.

Related

How to package a python script with my java program?

So, I'm writing a program(mostly for practice, kind of for future convenience) that will show a java front-end when run, and depending on the input, will use a python script to crawl and interpret a webpage, then send the interpreted data back to java to format for the front end. I'm sure I can get this running on my own computer, but when I want to distribute my program for my friends to use, how can I ensure my java program, mainProgram.jar can find my python code, script_x.py. Is there a way I can assure my python script will be supported. I've read that there are ways to include the necessary runtimes and such with your program when you build it, but would it still interact the same way with my java program if it was bundled with it's dependencies? I can provide some sample code soon, but it's still a work in progress, as I don't have the python script finished yet.
here is a link https://bytes.com/topic/python/insights/949995-three-ways-run-python-programs-java
use Jython (compiles to java byte code) https://en.wikipedia.org/wiki/Jython
distributing Jython applications: https://wiki.python.org/jython/JythonFaq/DistributingJythonScripts

Interface with running applications in PHP?

I would like to know if there is any way to communicate with a running console program (preferably running on Linux / Debian) via PHP. I am currently trying to create a webinterface for a little (existing) console Java program and I have no idea if there is any way I could do this. Could I "inject" a piece of code, lets say, a remote control module, and then use this to "remote-control" the script via PHP?
(It would be great if the existing .jar file wouldn't be changed / just injection, no reprogramming)
I am grateful for every piece of advice!
If the running program has no communication interface, then you can't communicate with it. If it does however, then the answer very much depends on how the program receives external input.
If the program contains a network listening thread (daemon), then you can communicate with it on the loopback interface using CURL or raw sockets from PHP.
Other ways of communicating with the program would be to share access to a file (PHP writes the file, Java reads it) or via a database.
The database would be the best option - it is thread safe and both PHP and Java have excellent MySQL support (Java via JDBC).
If, however you do not need to actually interface with the running program only merely need to start/stop/restart it, you can do this with the system() function in PHP.
If the Java program just runs and outputs to console, then you can do it easily enough, something like this:
$output = system( "java com.yourcompany.package.RunnableClass" );
print $output;
Assuming that the user who is running PHP has access to the Java binary of course, and that you have permission to access the JAR file.
Accessing a running program is a bit more difficult. Most programs will not have this built in by default (nor should they - giving access to random external processes in many cases is not desirable). If it does, though, you are in good shape. If it doesn't, and you can change the Java code, then you're good. If not, then you may be out of luck.
If that is the case, another good approach might be to see what resources the Java code is accessing, and how it is accessing them. Then you can write something similar in PHP. Obviously this is not ideal as you'll be re-inventing the wheel, but if you need to get to the data or whatever it is, and can't use any of the approaches above, it will work.

API on a C++ soft

I would like to know how to do to get variables of a C++ running program from my Java application. I think I have to do an API, but I don't know how to start this. In fact, I want to get information about packets in the Ekiga softphone. I localized what I want in Ekiga's main.cpp :
double lost = mw->priv->current_call->get_lost_packets();
double late = mw->priv->current_call->get_late_packets();
double out_of_order = mw->priv->current_call->get_out_of_order_packets();
I think what you need is JNI: http://en.wikipedia.org/wiki/Java_Native_Interface
You have to create an dll that will have methods that return this values and have a java class with native methods that will use this dll.
Sound like you want to implement a bridge layer using technologies such JNI or JNA.
A second option would be writing a Web service or Message passing layer between the two languages. I would avoid CORBA for such a simple problem (actually, I would avoid CORBA period hehehe).
Either that or have the C++ write the data to a Database, File, etc and write Java code to read it.
You can't directly access c++ variables from a different process. I do not know if ekiga already provides a way to get the data from external programs, so here are a few ways to get at the data (all involve modifying ekiga itself in some way).
Add a socket to listen for connections within the ekiga program and make your java program connect to this socket using client sockets. This way you can send the information to the connected java program wenever these values change.
Start the java Program within ekiga or make your java program start ekiga as a native method call. Both of these involve jni and result in both parts (ekiga and java program) running as a single process.
The first is simpler and less error prone, however you need some basic knowledge about network programming in both java and c++.

remote api for java

Is there a java api similar to RAPI? I want to be able to access files on the windows mobile device using a java desktop program.
Thanks.
You could use RAPI itself, and access it from Java using JNI or a wrapper like Swig
We were also looking for a similar API in Java but unfortunately none is available. I wrote my own RAPI wrapper using JNI and used that in my program.
The main problem with JNI is that any un-handled exceptions/faults cause the calling Java program to shutdown as well. Do keep that in mind when writing your wrapper. There are different approaches to safe guard these, the simplest and common approach is to write a standalone program written in .Net/C++ that communicates with the RAPI and your Java program communicates with that program using pipes/files etc. this way you also don't have to write a JNI wrapper :-)

Run Java class file from PHP script on a website

I have a website and want to be able to allow the user to run a Java file on the server from the website.
I want the user to click a button which will run the Java file on the server AND anything printed to standard-out by the Java program will be printed out on the website for the user to see.
How can this be done (call Java program from PHP and feed the standard out from the Java file back to the PHP website in real time)?
Update:
Thanks for the answers on how to run the Java program from PHP. However I also want to be able, as the Java program is printing to stdout where it will be printing out a lot of text as it is executing, to be able to print this out on the webpage so that the user can see what stage the Java program is in its execution.
How can this be done and does it require any additional AJAX or JavaScript or anything like that?
The PHP exec() function is the way to go, but you should be very careful in what you allow to executed.. in other words don't rely on user input as it could potentially compromise your entire server.
Calling the Java application launcher using exec, you can execute any Java application from PHP, e.g.
<?php exec("java -jar file.jar arguments", $output); ?>
Since you mention real time I would suggest setting up a PHP to Java Bridge. Initializing the JVM at each request takes up a lot of resources.
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 or ECMA 335
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.
I would rather wrap the Java class in a Java applet, which can then be invoked from a javascript call on the client side : see http://www.rgagnon.com/javadetails/java-0170.html
Otherwise, if the call throws a lot of text to the standard output or the class has to be run on the server because of system dependencies, calling from php exec is the way to go, but you will probably need something like cometd to display the text on the client in real time. There are implementations for various javascript toolkits such as Dojo or jQuery.
For the server side, there seems to be a cometd implementation in php here.
I hope this helps...
Philippe
Check out exec and the other program execution functions. But do this very carefully, or it's a recipe for exploits.
Is the passthru function of any use?
http://www.php.net/manual/en/function.passthru.php

Categories

Resources