This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
#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.
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.<br /> <br />#:^P
Anjana Ravindran
Ranch Hand
Joined: Aug 22, 2005
Posts: 76
posted
0
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
posted
0
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.
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:
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
posted
0
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 ]