简体   繁体   English

Java 外部程序

[英]Java external program

I'd like to start external third party application from my Java application.我想从我的 Java 应用程序启动外部第三方应用程序。 This external application should run all the time whilst my java application runs.这个外部应用程序应该在我的 java 应用程序运行时一直运行。

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 .有时(取决于用户交互)我的 java 应用程序应该能够通过stdinstdout读写这个外部应用程序。

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.从本质上讲,您将需要 Java 中的多个线程来监视外部进程的结束并围绕其输入/输出/错误流进行洗牌,以便您的主 Java 应用程序可以访问它。

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.有更多“基本”方法可以使用Process之类的类,但我建议Apache Commons-exec ,它提供了一些有用的工具来处理返回值和 I/O。

As you are implementing the suggestion of starting a Process , read and implement all the recommendations of When Runtime.exec() won't .在实施启动Process的建议时,请阅读并实施When Runtime.exec() won't所有建议。

Also consider using aProcessBuilder in place of Runtime.exec() (if coding for 1.5+).还可以考虑使用ProcessBuilder代替Runtime.exec() (如果编码为 1.5+)。

Is ex-app native code, or another Java program?是前应用程序本机代码,还是另一个 Java 程序? 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 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.这些将允许您执行本机程序,跟踪其状态,并获取其 output 并将其发送输入。

It depends on the specifics of the external application, mainly: 3rd party or is it something you have control over?这取决于外部应用程序的具体情况,主要是:第 3 方还是您可以控制的东西? ... 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).一种“笨拙”的方法是简单地使用文件系统并让 Java 监视放置在特定位置的文件(注意适当地处理锁定的文件)。 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.更复杂的方法是通过 sockets 进行通信,或写入本地/内部托管数据库(如 hsqldb)中的数据库表。 Using in/out streams via java.lang.Process might also do the trick, depending on the 3rd party app of course.通过 java.lang.Process 使用输入/输出流也可以解决问题,当然这取决于第 3 方应用程序。

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).如果第 3 方应用程序是 Excel,Java 的进程 class 将无济于事(在这种情况下,您可能必须按照我提到的第一种方法查看 xls 文件的保存目录)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM