如何从C进程中启动Java?

时间:2021-10-18 01:35:05

I want to add some Java (actually Clojure) based event handlers to a HUGE legacy C application. 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. Is this even possible?

我想将一些基于Java(实际上是Clojure)的事件处理程序添加到一个巨大的遗留C应用程序中。什么是最直接,最容易维护的方法?我希望Java类在与C代码相同的过程中运行。这有可能吗?

4 个解决方案

#1


The Java Native Interface would seem to be designed for this. In particular, this section of the spec seems relevant.

Java Native Interface似乎是为此而设计的。特别是,规范的这一部分似乎是相关的。

#2


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. This would be much better because it would avoid the maintenance surprises.

您可以考虑在C代码和Java进程之间通过TCP / IP建立桥接。这会好得多,因为它可以避免维护意外。

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. That's where your trouble becomes REAL painful.

你在想“维护意外,维护意外吗?”啊!今天你想从C调用Java,但明天有人会想要回到另一个方向。这就是你的麻烦变得真实痛苦的地方。

I've been here before. Take my advice: do the TCP/IP bridge. This will keep your Java from crashing. -Stosh

我以前来过这里。听从我的建议:做TCP / IP桥。这将使您的Java崩溃。 -Stosh

#3


The source code for the java command is available. It provides a good example of how to create a Sun JVM within a C program.

可以使用java命令的源代码。它提供了如何在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.

OpenJDK提供了Java 6(以及即将推出的JDK 7)版本。您可以在两个不同的许可下下载Java 5的源代码。

#4


You could do it this way, but given that you really want to script a large C app, have you considered GUILE or Lua? They where both made for exactly this purpose.

你可以这样做,但鉴于你真的想编写一个大型的C应用程序脚本,你考虑过GUILE还是Lua?它们都是为了这个目的而制造的。

#1


The Java Native Interface would seem to be designed for this. In particular, this section of the spec seems relevant.

Java Native Interface似乎是为此而设计的。特别是,规范的这一部分似乎是相关的。

#2


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. This would be much better because it would avoid the maintenance surprises.

您可以考虑在C代码和Java进程之间通过TCP / IP建立桥接。这会好得多,因为它可以避免维护意外。

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. That's where your trouble becomes REAL painful.

你在想“维护意外,维护意外吗?”啊!今天你想从C调用Java,但明天有人会想要回到另一个方向。这就是你的麻烦变得真实痛苦的地方。

I've been here before. Take my advice: do the TCP/IP bridge. This will keep your Java from crashing. -Stosh

我以前来过这里。听从我的建议:做TCP / IP桥。这将使您的Java崩溃。 -Stosh

#3


The source code for the java command is available. It provides a good example of how to create a Sun JVM within a C program.

可以使用java命令的源代码。它提供了如何在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.

OpenJDK提供了Java 6(以及即将推出的JDK 7)版本。您可以在两个不同的许可下下载Java 5的源代码。

#4


You could do it this way, but given that you really want to script a large C app, have you considered GUILE or Lua? They where both made for exactly this purpose.

你可以这样做,但鉴于你真的想编写一个大型的C应用程序脚本,你考虑过GUILE还是Lua?它们都是为了这个目的而制造的。