可以将多种语言合并为一个二进制文件吗

时间:2022-08-02 21:15:47

For example there could be a bit of java byte code mixed together with some C. Jvm will execute java byte code and turns execution over to OS if a C part is hit. Is this technically possible or in practice?

例如,可能有一些Java字节代码与一些C混合在一起。如果C部分被命中,Jvm将执行java字节代码并将执行转移到OS。这在技术上是可行的还是在实践中?

1 个解决方案

#1


2  

Generally you can write C code which creates JVM, executing (execve) provided bytecode and either run them in separate threads with some IPC between or using JNA/JNI to exchange the data, or make operations and wait for completion.

通常,您可以编写C代码来创建JVM,执行(execve)提供的字节码,并在单独的线程中运行它们,在一些IPC之间或使用JNA / JNI交换数据,或者进行操作并等待完成。

I met some projects using this approach (for example part of Android system, Cloudera Impala and some others), but the code there is overcomplicated and hardly traceable. For sure it's took too much effort to make it work properly. Sometimes it's better either run 2 processes using different technologies with good IPC with data serialization (thrift, protobuf) or use only one of them.

我遇到了一些使用这种方法的项目(例如Android系统的一部分,Cloudera Impala和其他一些),但是那里的代码过于复杂,难以追溯。可以肯定,它需要付出太多努力才能使其正常工作。有时,使用不同的技术运行2个进程,使用具有良好IPC的数据序列化(thrift,protobuf)或仅使用其中一个进程更好。

If you still need to run both, I'd prefer to build a system in Java calling native functions with JNI rather than opposite.

如果你仍然需要同时运行它们,我宁愿用Java构建一个用JNI调用本机函数而不是相反的系统。

#1


2  

Generally you can write C code which creates JVM, executing (execve) provided bytecode and either run them in separate threads with some IPC between or using JNA/JNI to exchange the data, or make operations and wait for completion.

通常,您可以编写C代码来创建JVM,执行(execve)提供的字节码,并在单独的线程中运行它们,在一些IPC之间或使用JNA / JNI交换数据,或者进行操作并等待完成。

I met some projects using this approach (for example part of Android system, Cloudera Impala and some others), but the code there is overcomplicated and hardly traceable. For sure it's took too much effort to make it work properly. Sometimes it's better either run 2 processes using different technologies with good IPC with data serialization (thrift, protobuf) or use only one of them.

我遇到了一些使用这种方法的项目(例如Android系统的一部分,Cloudera Impala和其他一些),但是那里的代码过于复杂,难以追溯。可以肯定,它需要付出太多努力才能使其正常工作。有时,使用不同的技术运行2个进程,使用具有良好IPC的数据序列化(thrift,protobuf)或仅使用其中一个进程更好。

If you still need to run both, I'd prefer to build a system in Java calling native functions with JNI rather than opposite.

如果你仍然需要同时运行它们,我宁愿用Java构建一个用JNI调用本机函数而不是相反的系统。