views:

322

answers:

4

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?

+5  A: 

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

jsight
+3  A: 

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.

erickson
+5  A: 

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

johnstosh
I'd upvote a large number of times if it were allowed. Nothing is ever as simple as it should be when getting Java to interoperate with anything else. I have a horror story, but there isn't room in this margin ;-)
RBerteig
johnstosh, could you be more specific about 'the TCP/IP bridge'.
Arthur Ulfeldt
A: 

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.

tomjen
the goal is to write event handlers in clojure that will be called form the C program when something happens.
Arthur Ulfeldt