Integrating Java into C Applications
To extend the functionality of a C application, incorporating a Java component may be desirable. While this has been achieved with Python, it seems like there hasn't been a clear solution for Java integration.
JNI and Java Class Usage from C
Java Native Interface (JNI) is a potential solution, but it typically assumes a full Java program utilizing Java classes. However, for this case, the goal is to utilize Java classes from within the C application.
Compiling and Evaluating Java Code on the Fly
The desired functionality involves compiling and executing Java code during runtime (like a scripting language) using JNI or a similar mechanism.
Example Java Code
import c4d.documents.*;
class Main {
public static void main() {
BaseDocument doc = GetActiveDocument();
BaseObject op = doc.GetActiveObject();
if (op != null) {
op.Remove();
}
}
}
Solution: Embedded JVM
The solution lies in embedding a Java Virtual Machine (JVM) within the C application. Oracle's reference book provides the necessary information. The key steps involve:
This allows for more sophisticated operations, such as custom class loaders, providing the necessary integration of Java capabilities into the C application.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3