From owner-freebsd-java Wed Mar 15 13: 2:16 2000 Delivered-To: freebsd-java@freebsd.org Received: from lodge.guild.ab.ca (lodge.guild.ab.ca [209.91.118.66]) by hub.freebsd.org (Postfix) with ESMTP id 6014B37BADA for ; Wed, 15 Mar 2000 13:01:50 -0800 (PST) (envelope-from davidc@acns.ab.ca) Received: from localhost (davidc@localhost) by lodge.guild.ab.ca (8.9.3/8.9.3) with ESMTP id OAA34511; Wed, 15 Mar 2000 14:07:31 -0700 (MST) (envelope-from davidc@acns.ab.ca) Date: Wed, 15 Mar 2000 14:07:31 -0700 (MST) From: Chad David X-Sender: davidc@lodge.guild.ab.ca To: Keith Wong Cc: freebsd-java Subject: Re: JNI and Shared Library In-Reply-To: <38CAAFC3.FE74A64D@1connect.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-java@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org I do not completely understand all that you have said, but hopefully this will help (it may be more than you asked for): Step 1) Java source public class Beep { public static native void beep(int count); } static { System.loadLibrary("beep"); } public static void main(String args[]) { beep(4); } } Step 2) Compile the class and generate header javac Beep.java javah -jni Beep.class Step 3) C Source include include #include "Beep.h" /* generated file */ JNIEXPORT void JNICALL Beep_beep(JNIEnv *e, jclass c, jint count) { int i = (int)count; for ( ; i > 0; i--) { printf("%c", 7); fflush(stdout); usleep(300000); } return; } Step 4) Compile and link that C code cc -Wall -c beep.c -I(the paths for your env) ld -Bshareable -fPIC -o libbeep.so beep.o Step 5) Set your LD_LIBRARY_PATH export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PWD Step 6) Run the program java Beep I hope this helps (and I didn't miss anything). Chad On Sat, 11 Mar 2000, Keith Wong wrote: > Hi Folks, > I have project that we are intended to use JNI to wrap the C native > Code, so I can call java instead of the C. But, we have problem after we > > try to link the imdsi.o to libimdsi.so (Freebsd gcc environment). Then > we are not even run this InsideLine.class. I have compared solaris's cc > compiler and gcc compiler, i see some difference on the linker option, > but i know i am get wrong on the linker shared library is it I pick the > right one. > > Those are the some reference of how to compile the shared library. > Here is the example of how to compile the shared library in Solaris and > Win32: > http://web2.java.sun.com/docs/books/tutorial/native1.1/stepbystep/step5.html > > In Solaris the way to get shared library: > cc -G -I/usr/local/java/include -I/usr/local/java/include/solaris \ > HelloWorldImp.c -o libhello.so > Here is the defination -G in Solaris's cc compiler: > -G in Solaris is: > Passes the option to the link editor to produce a shared object rather > than a dynamically linked executable. This option is passed to ld(1), > and cannot be used with the -dn option. > http://docs.sun.com/ab2/coll.33.5/CUG/@Ab2PageView/2194?Ab2Lang=C&Ab2Enc=iso-8859-1 > > Here is the gcc in the freebsd's linker's option: > http://gcc.gnu.org/onlinedocs/gcc_2.html#SEC13 > > ------------------------------------------------------------------------------------------------------ > > Those are how we link the shared library in gcc > % gcc -shared -I/usr/local/jdk1.1.8/include > -I/usr/local/jdk1.1.8/include/freebsd > -I/usr/home/eproj/codebase/com/ingram > -I/usr/home/eproj/codebase/com/ingram/hw_appl.h -I/usr/include/rpc/rpc.h > -I/usr/home/eproj/codebase/com/ingram/com_ingram_InsideLine.h > InsideLine.c -fPIC -o libimdsi.so > > InsideLine.c: In function `Java_InsideLine_Call_1Server': > InsideLine.c:52: warning: cast to pointer from integer of different size > > InsideLine.c: In function `Java_InsideLine_Close_1Client': > InsideLine.c:74: warning: cast to pointer from integer of different size > > InsideLine.c: In function `Java_InsideLine_Open_1Client': > InsideLine.c:87: warning: cast from pointer to integer of different size > > Then, i try to run the test program. > % java com.ingram.InsideLine > java.lang.UnsatisfiedLinkError: Open_Client > at com.ingram.InsideLine.main(InsideLine.java:23) > > % file libimdsi.so > libimdsi.so: ELF 32-bit LSB shared object, Intel 80386, version 1 > (FreeBSD), not stripped > > >From the output about, i think the libimdsi.so have not linked with the > > Call_Server function. Do you have any ideal how to make that imdsi.o's > three functions linked into the libimdsi.so. I had made full path for > java in FreeBSD enirovnment > setenv LD_LIBRARY_PATH > .:/usr/home/eproj/codebase/com/ingram:/usr/home/eproj/codebase > setenv CLASSPATH > /usr/local/mm.mysql.jdbc-2.0pre3:/usr/local/share/java/classes/jsdk.jar:/usr/home/eproj/codebase > > Other, i have the Helloworld.java and .c (from sun website). i can > compile it and run with no problem, > yes, i know the the Helloworld.c is wrote by us. is that a problem that > becuase i import the .object instead of .c file, > do we have any way to link the .object file?? Is that i am using the gcc > -shared in the correct way? > > > Thanks a million! > > Keith. > > > > To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-java" in the body of the message