my dog learned polymorphism*
The moose likes Java in General and the fly likes Calling java from C Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calling java from C" Watch "Calling java from C" New topic
Author

Calling java from C

Anjana Ravindran
Ranch Hand

Joined: Aug 22, 2005
Posts: 76
Is it possible to call java program from C?
please give me some pointers for the same.
shan sundaram
Greenhorn

Joined: Feb 07, 2004
Posts: 15
U can try to use the follwing syntax

#include <stdlib.h>
status = system( command )
Where:
const char *command;
is a string containing a command that should be submitted to the operating system. This should not have a '\n' on the end.
int status;
is the status returned by the operating system when the command finishes execution.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
You can do that, but it isn't really "calling" Java code. When you do system() in C, you are executing the Java code as a separate process.

If you really want to call Java code from C, within a single process, you can do it. You use a part of JNI (Java Native Interface) called the Invocation Interface. Using this, a C program can start up a JVM, then tell the JVM to load classes, run methods, retrieve data from Java objects etc.

Google for Java Native Interface and Java Invocation Interface.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Anjana Ravindran
Ranch Hand

Joined: Aug 22, 2005
Posts: 76
Tried as you said.

my jdk resides in C:\j2sdk1.4.2\bin and i have HelloWorld.java and HelloWorld.class inside the bin folder. I am able to run HelloWorld program in command using the following commands

cd C:\j2sdk1.4.2\bin
java -classpath . HelloWorld

It worked well and displayed HelloWorld.

When i tried calling HelloWorld program from C, it is not working and giving following error
Before calling java program
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
status of system call1After calling java program


Call Java from C program
#include <stdio.h>
#include <stdlib.h>

int main()
{
int status = 0;
printf("Before calling java program\n");
system("cd C:\\j2sdk1.4.2\\bin");
status = system("java -classpath . HelloWorld");
printf("status of system call %d", status);
printf("After calling java program\n");
}


And my HelloWorld program for reference

public class HelloWorld {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World");
}

}
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Your "cd" and "java" commands are running in separate shells, so the "cd" has no effect on the current directory seen by "java". If you wish to continue using the system() way of invoking Java from C (it's easy, but slow and inflexible), you could perhaps write a script that does the "cd" then the "java", then use system() to call the script.
Casper Maxwell
Ranch Hand

Joined: Aug 04, 2005
Posts: 88
If you want to use Java libraries from C side then you can use JNI. The following chapter of the JNI book describes this usage:

http://java.sun.com/docs/books/jni/html/invoke.html
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Casper Maxwell:
If you want to use Java libraries from C side then you can use JNI. The following chapter of the JNI book describes this usage:

http://java.sun.com/docs/books/jni/html/invoke.html


... er, like I said already.

The original poster needs to decide whether the Java needs to run in the same process as the C, or whether it can or must run in a separate process. JNI is the solution for the former, something like calling system() is a solution for the latter. The former is efficient and fast, but quite difficult to program. The latter is inefficient and slow, but much easier to program.
Casper Maxwell
Ranch Hand

Joined: Aug 04, 2005
Posts: 88
The former is efficient and fast, but quite difficult to program. The latter is inefficient and slow, but much easier to program.


Yes. You are right. I just noticed your answer..
[ July 27, 2006: Message edited by: Casper Maxwell ]
 
 
subject: Calling java from C
 
Similar Threads
I want to send a compiled code project (that is written from eclipse) so it can work from c how c
IO question
pointers
problem with packages and classpath
How to use JNI