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?
The Java Native Interface would seem to be designed for this. In particular, this section of the spec seems relevant.
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.
The Java 6 (and upcoming JDK 7) version is available at OpenJDK.You can download source code for Java 5 under two different licenses.
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.
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.
I've been here before. Take my advice: do the TCP/IP bridge. This will keep your Java from crashing. -Stosh
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.