Google Answers Logo
View Question
 
Q: Calling Java classes from C programs ( Answered 4 out of 5 stars,   0 Comments )
Question  
Subject: Calling Java classes from C programs
Category: Computers > Programming
Asked by: lk_sf-ga
List Price: $10.00
Posted: 19 May 2006 13:58 PDT
Expires: 18 Jun 2006 13:58 PDT
Question ID: 730502
If I have a C program that needs to call a method in a java class (and
receive back the results), how do I do it? I need some sample code.
Answer  
Subject: Re: Calling Java classes from C programs
Answered By: maniac-ga on 19 May 2006 15:52 PDT
Rated:4 out of 5 stars
 
Hello Lk_sf,

There is a pretty comprehensive article describing this method and
source code available at
  http://webservices.sys-con.com/read/45840.htm
or equivalently at
  http://java.sys-con.com/read/45840.html
which is an article titled "Calling Java from C" [published in two
magazines?]. The direct link to the source code (a zip file), batch
files to build / run the applications under Windows 2000, and sample
files to run is:
  http://res.sys-con.com/story/45840/Havey0908.zip
Note that the command files are for Windows, the article indicates
that the code should be platform independent (if that is important to
you).

In the middle of the article is a good introduction into the issues
related to getting data back to your C program. This example uses a
string, but other methods (such as using JNI to call back the C code)
could be used as well.

There are several other good references - try searching with phrases such as
  calling "java from c"
or
  calling java from c
The first is more specific, the second will also bring up references
of Java calling C. In addition, a phrase like
  java jni
or
  calling jni
brings up general information related to the Java Native Interface.

Please make a clarification request if the answer is incomplete or unclear.

Good luck with your work.

  --Maniac

Request for Answer Clarification by lk_sf-ga on 19 May 2006 19:27 PDT
Thank you,
So is it always calling the 'public static void main(String args[])' of the class?
What if my class is a library?
I was looking for a way to somehow instanciate an object and call a
public method, not through 'void main'.
Am I missing anything?

Clarification of Answer by maniac-ga on 20 May 2006 12:06 PDT
Hello Lk_sf,

The method described in the article which implements this by calling a
java "main program" is the simplest to implement. The steps that get
done "behind the scenes" include:
 - starting / connecting to the Java virtual machine
 - running the code within that environment
 - destroying all the objects and other required clean up steps

What you have asked for (creating a Java object and calling a public
method) is much more complicated and involves several pages of code.
Let me provide some references on that approach:

[1] Starting the Java VM is described at
  http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/invocation.html
as JNI_CreateJavaVM. This was done inside the code referenced in the
original answer but a simple version is also provided at
  http://www.inonit.com/cygwin/jni/invocationApi/c.html

[2] Creating the java object (I assume you want constructors to run...)
  http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/functions.html#object_operations
is done using a call to NewObject (or one of the variants). See 
  http://www.javaworld.com/javaworld/jw-05-2001/jw-0511-legacy.html
for one of the simplest examples I could find for this (and the
related code). There is a more complicated example (in four parts)
starting at
  http://www.javaworld.com/javaworld/jw-03-2001/jw-0316-itw-jni.html
Its this interface (and the next one) that has pretty meager references.

[3] Calling a method for that object is described at
  http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/functions.html#wp16656
and in the previous two code references. See also
  http://www.javaworld.com/javaworld/jw-05-2002/jw-0510-integrate.html
which indicates that four JNI calls are required to make a method
call. There is some additional source code at
  http://www.lordjoe.com/JavaCourse/JNIMethods.html
(doing matrix multiplication) that may be helpful. Try searching with phrases like
  callobjectmethod jni
or
  callobjectmethod jni source code
for more examples.

[4] At this point, you should clean up the Java environment. This is
also described at
  http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/invocation.html
as DestroyJavaVM. This is a pretty straight forward call - several of
the code files already referenced will call this.

Let me know if you need additional references to this (more complicated) method.

  --Maniac
lk_sf-ga rated this answer:4 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy