java 是否可以在java中执行二进制文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2765888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to execute binary files in java?
提问by Dimitri
I have a list of binaries written in Java, Ada, C, and Python and I want to execute them. How can I do that? Are there any JVM binding to those languages?
我有一个用 Java、Ada、C 和 Python 编写的二进制文件列表,我想执行它们。我怎样才能做到这一点?是否有任何 JVM 绑定到这些语言?
回答by Oded
If all you want to do is execute existing applictions, you can use the execmethods from the java.io.runtimenamespace.
如果您只想执行现有应用程序,则可以使用命名空间中的exec方法java.io.runtime。
Runtime rt = Runtime.getRuntime();
Process ps = rt.exec("path to my executable.exe");
回答by Chris J
Yes. Here is a link to a good blog article on how to do it: Running system commands in Java.
是的。这是一篇关于如何做到这一点的优秀博客文章的链接:在 Java 中运行系统命令。
The gist of it is that you need to do the following:
其要点是您需要执行以下操作:
// run the Unix "ps -ef" command
// using the Runtime exec method:
Process p = Runtime.getRuntime().exec("ps -ef");
You can pretty much put any command in there but the only gotcha that I have encountered in be aware of system environment variables like the PATH that you are running your JVM in.
您几乎可以在其中放置任何命令,但我遇到的唯一问题是要注意系统环境变量,例如您正在运行 JVM 的 PATH。
回答by Frederik
If you want to interact with the binary API's, use:
如果要与二进制 API 交互,请使用:
- Java Native Access (JNA): for loading and calling DLLs.
- Java Native Interface (JNI): for wrapping a C library in Java.
- Java Native Access (JNA):用于加载和调用 DLL。
- Java Native Interface (JNI):用于在 Java 中包装 C 库。

