Java Reference
In-Depth Information
HelloJni.o:: HelloJni.h
libhello.so: $(CSRCS) HelloJni.h
$(CC) $(INCLUDES) $(CFLAGS_FOR_SO) $(CSRCS) -o libhello.so
# This part of the Makefile is for Java called from C, in javafromc
testjavafromc: javafromc.all hello.all
@echo
@echo "Now we test HelloJni using javafromc instead of java"
@echo
./javafromc HelloJni
@echo
@echo "That was, in case you didn't notice, C->Java->C. And,"
@echo "incidentally, a replacement for JDK program \"java\" itself!"
@echo
javafromc.all: javafromc
javafromc: javafromc.o
$(CC) -L$(LIBDIR) javafromc.o -ljava -o $@
javafromc.o: javafromc.c
$(CC) -c $(INCLUDES) javafromc.c
clean:
rm -f core *.class *.o *.so HelloJni.h
clobber: clean
rm -f javafromc
And you're done! Just run the Java interpreter on the class file containing the main program.
Assuming that you've set whatever system-dependent settings are necessary (possibly in-
cluding both CLASSPATH and LD_LIBRARY_PATH or its equivalent), the program should
run as follows:
C> java jni.HelloJni
Hello from a Native Method // from C
"myNumber" value is 42 // from C
"myNumber" value now 43 // from C
Value of myNumber now 43 // from Java
Congratulations! You've called a native method. However, you've given up portability; the
Java class file now requires you to build a loadable object for each operating system and
Search WWH ::




Custom Search