best way to integrate java and C code

J

Jack

Greetings,

I am looking to know what the best approach is to integrate java and C
code together. I have some existing C code that I need to integrate
with some other java code - is this just a basic interface, or is this
a complex effort using something other than an interace ?

Regards,

Jack
 
K

KC Wong

I am looking to know what the best approach is to integrate java and C
code together. I have some existing C code that I need to integrate
with some other java code - is this just a basic interface, or is this
a complex effort using something other than an interace ?

You can use:
1. JNI. You write an "interface" to the C program in Java, invoke javah on
it which results in a C header file, then implement it to call your C
program. Then call that "interface" in Java. You should check java.sun.com
for documentation and tutorial, as there're much more (e.g. accessing Java
objects, members and methods from C side, threading, invoking JVM) than what
I've said.

2. Socket. Listen on one side, connect from another. You'll be handling the
communication yourself, but in some cases it could be better than using JNI.

3. If your C program is an ActiveX object and does not have multiple
interfaces, you can use an open source project called "JACOB". It does the
JNI for you. (http://danadler.com/jacob/)


HTH,

KC
 
R

Roedy Green

1. JNI. You write an "interface" to the C program in Java, invoke javah on
it which results in a C header file, then implement it to call your C
program. Then call that "interface" in Java. You should check java.sun.com
for documentation and tutorial, as there're much more (e.g. accessing Java
objects, members and methods from C side, threading, invoking JVM) than what
I've said.

for details see http://mindprod.com/jgloss/jni.html
 
J

Jack

Roedy Green said:
4. Sometimes you can exec the C program and have it communicate via
the input/outputstreams. see http://mindprod.com/jgloss/exec.html
for details.

Thank you - I assume the recommendations listed would also work if I
want to have graphics and UI input and output via Java interfaced with
a series of core code programs written in C ?

-Jack
 
R

Roedy Green

Thank you - I assume the recommendations listed would also work if I
want to have graphics and UI input and output via Java interfaced with
a series of core code programs written in C ?

Each interfacing technique has its drawbacks. See
http://mindprod.com/jgloss/jni.html
and http://mindprod.com/jgloss/socket.html
http://mindprod.com/exec.html

to figure out which approach will work best.

Exec is the easiest, but slowest and crudest.

JNI lets you do anything, and quickly, but are the most complex.

Sockets are intermediate.
 
J

John

Are there any good tips for implementing the socket technique in a secure
way? I think the naive way would be to just listen for connections on
either the C or Java end, but how do you securely allow only a specified
local instance to connect?
 
G

Gordon Beaton

Are there any good tips for implementing the socket technique in a
secure way? I think the naive way would be to just listen for
connections on either the C or Java end, but how do you securely
allow only a specified local instance to connect?

If the server binds the listen socket to the localhost address
127.0.0.1 in addition to the port number, remote processes are
prevented from connecting. Local processes can still connect to the
localhost address.

If you only want a *specific* local process to connect, you need to be
more clever. For example, the server can listen on a "random" port
(specify 0, the system will choose a free one) before starting the
client and passing it the port number on the command line. The client
then connects back to the server which only accepts a single
connection.

This leaves a short (subsecond) window of opportunity for another
process to connect before the client, but this should be sufficient
for most applications. If your processes don't have a parent-child
relationship or you need more security, use an authentication
mechanism when the client connects instead.

There are also some unix-specific mechanisms that might be simpler if
that's your platform. For example, you can use a named pipe instead of
a socket (Java can treat it like any file) and rely on the uid-based
security provided by the filesystem. On the other hand, once you start
using platform specific mechanisms you might as well use JNI.

/gordon
 
R

Roedy Green

Are there any good tips for implementing the socket technique in a secure
way? I think the naive way would be to just listen for connections on
either the C or Java end, but how do you securely allow only a specified
local instance to connect?

From a client point of view there is nothing to it. You just type
https: instead of http: in your URL.

You might look up http://mindprod.com/jgloss/security.html
and follow the links.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top