简体   繁体   中英

Java calling an external process - exe

I am building a java application, which at some point utilizes an external exe. At this point, I'm trying to simply add this exe as some sort of library, which I can use in process call, so user wouldn't have to install it..

This exe file is an command line tool which produces some output, which is further processsed by the application.

So my question is, how does one include exe file within a java application, instead of calling it as a system process. Also acceptable would be, if this exe would be for example in the final lib folder, where java app would fetch it and execute it.

I hope it is clear and thanks for any help.

Java interacts with other (native) code in a couple of ways:

  • to specifically coded libraries through Java Native Interface (JNI)
  • to external tools by forking a child process using Runtime.getRuntime().exec(...)
  • through network communications

In the first case, it is useful to include your JNI library with you Java program. It's thinkable to include it as a class resource in your JAR file, but that would be against the ideas of JAR files (holding classes and resources). More likely you should bundle the library (for all target platforms) with some sort of installation package (eg InstallAnywhere or others)

In the second case it's not useful to include the binary into your Java program (ie in the JAR file). Rather it most likely has (should have) its own installation procedure and your Java code should use an appropriate commandline (PATH) to find the executable.

I think the third case is not relevant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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