开发者

calling java from c code

Our programming model is like this:

The c code is to capture the real-time events(very frequent) of the system and java code runs as a server to receive the request. When there is an event captured by the c code, we need to send request to java code a开发者_C百科nd wait for the reply. I have tried JNI to call java from c, but it seems that it will stuck after running for several seconds. JNI needs to initialize jvm and search the handler in the class file. Is there any better way to communicate between c and java? Can I compile java code into binary and link it with the c code? Thanks


You may want to look into using sockets if there is a nice producer-client/consumer-server paradigm involved. That way the C-code is running in the background and pushes info to the Java service over a socket connection, and the Java services in-turn sends replies back to the C-code. You could also look into using FIFO's or named pipes for communication as well, but sockets, at least locally on a machine, tend to be pretty fast and efficient on most platforms.

Also, the Java service can start-up the C-code on-launch if you need to work that way, or vice-versa (i.e, the C-code can fork off a VM and start the Java service).


If the Java code is running as a server, you should send it a request from the C code, which can be in a different process or even a different machine. I personally wouldn't try to get into calling Java code directly from C code unless there was a really good reason to.


One option is to run the Java application in a separate process and use interprocess communications between the java application and the C data collector.

As to compiling Java and linking into C, that's possible if your particular tool set supports it. For example, If using a full featured GNU compiler, gcc and gcj can work together, however you need to understand that Java is not just a language, it's also an environment. Doing a native compilation of Java might not provide you whatever reason you have for using Java for the system.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜