Java external program - java

I'd like to start external third party application from my Java application. This external application should run all the time whilst my java application runs.
From time to time (it depends on user interaction) my java app should be able to read and write to this external application via stdin and stdout.
How can I do that?

Essentially, you will need multiple threads in Java that watch for the outside process to end and which shuffle around its input/output/error streams so that your main Java app has access to it.
There are more "basic" ways to do it with classes like Process, but I would suggest Apache Commons-exec, which provides some useful tools for handling return values and I/O.

As you are implementing the suggestion of starting a Process, read and implement all the recommendations of When Runtime.exec() won't.
Also consider using a ProcessBuilder in place of Runtime.exec() (if coding for 1.5+).

Is ex-app native code, or another Java program? If it's native code, look at http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Process.html and http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Runtime.html
Those will allow you to execute a native program, keep track of its status, and get its output and send it input.

It depends on the specifics of the external application, mainly: 3rd party or is it something you have control over?... what it's built with, what it's capabilities are, etc.
A 'kludgy' method would be to simply use the file system and have Java watch for files dropped in a specific location (taking care to handle locked files appropriately). A more sophisticated method would be to communicate via sockets, or writing to a database table within a local/internally hosted db such as hsqldb. Using in/out streams via java.lang.Process might also do the trick, depending on the 3rd party app of course.
But again all of this depends on the specifics of the application you're communicating with. Java's Process class isn't going to help if the 3rd party app is Excel (in which case you'd probably have to watch a save directory for xls files per the first method I mentioned).

Related

Fetching data outside of JVM

I'm curious to know if it's possible to fetch data from other applications to my Java program. I know java is running in a virtual environment, hence JVM and therefore have problems communicating with other applications unless you were to use the Robot class or so forth.
What I would like to do, for starters as educational purposes is to take let's say a music application like Spotify/iTunes, fetch the playlist (text data) and send/display it in a text file. I've tried a few things so far, and the only thing I've come close to is by using the Robot class, opening the application, doing Ctrl+A, pasting it in to a text document and so forth but that's more like a macro. I would like to make a java application that would do this automatically. Is that possible in any sense with Java or are you just better off changing languages? I wish to do it with Java though because it's the language I've studied for the past year and what I'm trying to master. (Sorry for the long explanation.)
It's nothing to do with Java and the JVM. Any language has the same problems and solutions for this sort of situation.
The thing you need to talk to has to provide you a way to talk to it. You need to talk to it using that way.
Methods include pipes, custom network protocols, SOAP and Restful web services, etc.
Just because an application runs in a virtual machine doesn't mean it cannot access external data through an API provided by an external program. For example, iTunes has a COM-based API for accessing playlists, and here is an example of using it from C#. You'll need something which allows accessing COM objects from Java.
(Please note I know nothing about this topic, this is just what I found with a little searching...)
You need to know which data interfaces provides the application you want to connect to.
As an example, maybe the application writes files to disk or stores some info in a database.
Then with Java you can read files, query the database, use an API (web services, REST, etc..) and so on...

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.

how to control a running java service

I intend to create a java program/service that continuously polls rss-feeds using the informa library 'poller' functionality. I want to be able to add,delete,update the rss-url's realtime, while the program is running. I have no prior experience with the informa library but I need it to potentially scale to a lot of rss-feeds.
Does anyone have have experience with the informa library for polling rss-feeds? What other method/libraries would you consider to poll a lot of rss-feeds (10.000+)?
What do you consider to be an accepted solution to control a running (console) java program. I was thinking about using a control port for sending commands. Are there other mechanisms more commonly used to achieve this functionality?
Please let me know if you need more specific information.
Kind regards,
Ivo
What do you consider to be an accepted solution to control a running
(console) java program. I was thinking about using a control port for
sending commands. Are there other mechanisms more commonly used to
achieve this functionality?
You can read the parameter from a .properties file. The only disadvantage with this is that the properties file will have to be read in each time you want to use that property, irrespective of whether the value has changed.
You can make use of JMX. This is a fairly nice concept in which you expose a bean to be manageable using the jconsole command (Java Management Extensions Console). Once done, you can then remotely inject values into a running JVM.
There is a nice example on Sun Oracle website that shows you how to do it.
Yes, a normal way to interact with a remote service would be a control port as you described it.
You can also control it via Database settings and create a thread which will poll for these DBs settings. The DB settings will be set via some web? UI.
If you plan on running one service with polling on one single machine I would rather recommend against it and set your service either on virtual machines or setup multiple instances of the service on one big machine with a big amount of memory. I have been using a com.sun.syndication library for feeds parsing/retrieving.
I don't want to be a captain obvious but I think it's easily achievable with a usual multi-threading application and Concurrent Queueing. If I got you correctly.
Thanks.

Save and load a (Java) program's state

Suppose you're short on time and you're looking for a program with certain features, and you find one, except it lacks one feature - it cannot save and load its state. Is it possible to achieve this on OS level, or with another program, that can take the whole thing, write it to a file, and then at a later time, load it back into memory? How?
Specifically for me, this is about a Java program, but any more information on this topic is welcome.
One (heavy and easy) solution could be to use VirtualPC and install the program on a virtual OS.
Check these library's:-
Brakes
ACTC(Asynchronous Transfer of Control Threading) [Article]
Apache JavaFlow
You want to do something like the Hibernate function of Windows right?
This will be extremely difficult to implement in Java as you will also have to write the state of the JavaVM. If you had open files when you closed the program end so on.
I think the best you can do is writing the objects you need to recover to disk using Java serialization.
Have a look at the CRIU project at https://criu.org/Main_Page
It offers exactly this possibility within linux systems. Docker integrates it and offers a docker checkpoint command, which if you run your program in a container, will allow you to do this on any OS.

How to know start and kill processes within Java code (or C or Python) on *nix

I need to write a process controller module on Linux that handles tasks, which are each made up of multiple executables. The input to the controller is an XML file that contains the path to each executable and list of command line parameters to be passed to each. I need to implement the following functionality:
Start each executable as an independent process
Be able to kill any of the child processes created, independent of the others
In order to do (2), I think I need to capture the pid when I create a process, to issue a system kill command. I tried to get access to pid in Java using ProcessBuilder but saw no easy way to do it.
All my other logic (putting info about the tasks in DB, etc) is done in Java so I'd like to stick with that, but if there are solutions you can suggest in C, C++, or Python I'd appreciate those, too.
For a Java solution, you should take a look at the apache commons exec library. They've done a lot of work to make it platform independant and they have a great tutorial.
In python, you can use the included subprocess library.
You really really need to look up "shell scripting" on google. Specially if your employer/instructor wants you to work on linux and deal with processes etc.
Maybe start here:
http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/unixscripting/unixscripting.html
I'm not sure, but if you start executables from Java, you may start them in seperate threads, and then you can map them however you want - by name, by line number or something - and stop that enclosing thread regularly as java-thread, which doesn't seem to be an elegant solution (not closing files, etc.), but could work to some extend (as long as the linux-program doesn't start a process, which is freeing itself from its parent).
Specific commands for closing each process, send via stdin to the programs, might be another option. How to handle stdin and stdout and other pitfalls are mentioned here in some length:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?
Visible programs can even be controlled by java.awt.Robot (keyboard, mouse).
As a last idea I would consider using a new command "kill pidof program" which working on a name-basis, so you can't distinguish two instances of the same progam.
I don't know the apache-lib, mentioned by Steen, but there is normally very useful stuff, I would recommend to look there too - maybe in the first place.

Categories

Resources