Question

Je voudrais commencer une application tierce externe de mon application Java. Cette application externe doit fonctionner tout le temps alors que mes courses d'application java.

De temps en temps (il dépend de l'interaction de l'utilisateur) mon application java devrait être capable de lire et d'écrire à cette application externe via stdin et stdout.

Comment puis-je faire?

Était-ce utile?

La solution

est ex-application du code natif, ou un autre programme Java? Si c'est le code natif, consultez http: // download.oracle.com/javase/1.5.0/docs/api/java/lang/Process.html et http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Runtime.html

Ceux-ci vous permettra d'exécuter un programme natif, garder une trace de son état, et obtenir sa sortie et l'envoyer entrée.

Autres conseils

Pour l'essentiel, vous aurez besoin de plusieurs threads en Java qui montre le processus extérieur à la fin et qui lecture aléatoire autour de son entrée / sortie / erreur flux afin que votre application principale de Java a accès.

Il y a plus de moyens « de base » de le faire avec des classes comme Process, mais je suggère Apache Commons-exec , qui fournit des outils utiles pour la gestion des valeurs de retour et d'E / S.

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+).

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).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top