• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Calling java from C

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to call java program from C?
please give me some pointers for the same.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Anjana Ravindran
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
get schwifty. tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic