简体   繁体   English

如何从C进程中启动Java?

[英]How to start Java from within a C process?

I want to add some Java (actually Clojure ) based event handlers to a HUGE legacy C application. 我想将一些基于Java(实际上是Clojure )的事件处理程序添加到巨大的旧C应用程序中。 What is the most straight forward and easily maintained way to do this? 什么是最直接,最容易维护的方法? I would like the Java classes to be running in the same process as the C code. 我希望Java类与C代码在同一过程中运行。 Is this even possible? 这有可能吗?

The Java Native Interface would seem to be designed for this. Java本机接口似乎是为此目的而设计的。 In particular, this section of the spec seems relevant. 特别地, 规范的这一部分似乎是相关的。

By the way, this is easy to make it "seem" correct, but much more difficult to actually do correctly. 顺便说一句,这很容易使它“看起来”正确,但是要真正正确地执行则困难得多。 Why do you want them running in the same process? 为什么要它们在同一过程中运行? Why not the same thread? 为什么没有相同的线程?

You might consider making a bridge through TCP/IP between your C code and a Java process. 您可能考虑在您的C代码和Java进程之间通过TCP / IP进行桥接。 This would be much better because it would avoid the maintenance surprises. 这样会更好,因为这样可以避免维护意外。

You're thinking "maintenance surprises, what maintenance surprises?" 您在想“维护意外,什么维护意外?” Ah! 啊! Today you want to call from C to Java, but tomorrow someone is going to want to go back the other way. 今天,您想从C调用到Java,但是明天某人想要返回另一种方式。 That's where your trouble becomes REAL painful. 那就是您的麻烦变成真正痛苦的地方。

I've been here before. 我以前来过这里 Take my advice: do the TCP/IP bridge. 征询我的意见:建立TCP / IP桥接器。 This will keep your Java from crashing. 这样可以防止Java崩溃。 -Stosh -斯托什

The source code for the java command is available. java命令的源代码可用。 It provides a good example of how to create a Sun JVM within a C program. 它提供了一个很好的示例,说明如何在C程序中创建Sun JVM。

The Java 6 (and upcoming JDK 7) version is available at OpenJDK.You can download source code for Java 5 under two different licenses. Java 6 (以及即将发布的JDK 7)版本可从OpenJDK获得。您可以在两个不同的许可下下载 Java 5的源代码

You could do it this way, but given that you really want to script a large C app, have you considered GUILE or Lua? 您可以通过这种方式进行操作,但是由于您确实想编写大型C应用程序的脚本,因此您是否考虑过GUILE或Lua? They where both made for exactly this purpose. 他们俩都是出于这个目的。

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

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