The Artima Developer Community
Sponsored Link

Java Answers Forum
How to do that ?Java application communicates with C application?

4 replies on 1 page. Most recent reply: Dec 19, 2002 2:12 AM by Alex Shaw

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Alex Shaw

Posts: 15
Nickname: alexhaha
Registered: Dec, 2002

How to do that ?Java application communicates with C application? Posted: Dec 17, 2002 11:47 PM
Reply to this message Reply
Advertisement
Who can give me some advice about the topic?Thanks


V. Srikanth

Posts: 9
Nickname: iamnobody
Registered: Dec, 2002

Re: How to do that ?Java application communicates with C application? Posted: Dec 18, 2002 4:26 AM
Reply to this message Reply
Ever wonder on how to integrate JAVA and C programs ??
Ever had a question that "I have built a C library with my own reusable funtions and how do I use them in my Java application ??",

Following SIX STEPS demonstrate how to do that through a traditional Hello World program..

Have fun integrating your applications.
-Srikanth.

Step 1: (Write the JAVA code )

######################################################
cat Main.java


class Main {
public static void main(String[] args) {
new HelloWorld().displayHelloWorld();
}
}

class HelloWorld {
public native void displayHelloWorld();

static {
System.loadLibrary("hello");
}
}
##########################################################

* Note the keyword "native" used for the method displayHelloWorld(). This indicate it's method defined in a native library and not in any other Java class.
* Note the call to "System.loadlibrary" , this is the one which will load the "native library" in which displayHelloWorld is defined. (in case Unix system, System.loadLibrary("hello") will look for "libhello.so"

Step 2: (Compile the java code)

Using the command "javac Main.java"

This will generate two class files "Main.class" and "HelloWorld.class"

Step 3: (Create the .h file)

Using the command "javah HelloWorld"

This will generate header file HelloWorld.h which contain the Prototype of function to be put in the C file that we are going to write.

####################################################################### ############

cat HelloWorld.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */

#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: displayHelloWorld
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif
cat HelloWorld.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class HelloWorld */

#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloWorld
* Method: displayHelloWorld
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

################################################## #####################

Step 4: (Write the native method implementation)

Write the C program which will implementation for JNICALL Java_HelloWorld_displayHelloWorld() function.

#################################################################### ################

cat HelloWorldImp.c
#include "HelloWorld.h"
#include <stdio.h>

JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *myjni, jobject myobject) {
printf("Hello World!\n");
return;
}

#################################################################### #################



Step 5: (Create a shared library).

Standard way of creating a ".so" file in Unix.
Using "cc -o libhello.so -G HelloWorldImp.c"


Step 6: (Run the program)

Now run the program as using "java Main"

You will be delighed to see the output "Hello World" on the screen !!!

Will this help Alex???

Alex Shaw

Posts: 15
Nickname: alexhaha
Registered: Dec, 2002

Re: How to do that ?Java application communicates with C application? Posted: Dec 18, 2002 6:49 PM
Reply to this message Reply
Thank you above all.
I think I hadn't express my meaning.There is a C application running on the server,and there is a Java application running on the client,what mechanism should be used? Socket?CORBA?RMI-IIOP? Usually the samples refered in the book are java server and java client,now I want to get some samples such as C server and java client,so may you give some help?

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: How to do that ?Java application communicates with C application? Posted: Dec 19, 2002 1:10 AM
Reply to this message Reply
CORBA would be the answer to your problems.

Alex Shaw

Posts: 15
Nickname: alexhaha
Registered: Dec, 2002

Re: How to do that ?Java application communicates with C application? Posted: Dec 19, 2002 2:12 AM
Reply to this message Reply
Because I know little about the realization of corba in C,Who can give some samples of corba which work with C and java.THX

Flat View: This topic has 4 replies on 1 page
Topic: big/little endian-do u know what it means?!Help!!! Previous Topic   Next Topic Topic: help with debug

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use