Java Reference
In-Depth Information
The result of the command line above is a file named
NativeHelloWorld.
obj
in the
build
\
objs
directory which must be linked to create a Windows
DLL file.
On a Sun Solaris system, the compilation command line is
cc -c -Ibuild/headers -I$JAVA
-
HOME/include \
-I$JAVA
-
HOME/include/sparc \
-obuild/objs/NativeHellowWorld.o \
src/javatech/jni22/NativeHelloWorld.cpp
where we have used the
“\
„
continuation character to break the long line into
four lines.
22.3.6 Create a shared library
To create a Windows DLL file, we use the Windows
link
command line tool,
which is part of the Visual C
++
installation:
link -dll build\objs\NativeHelloWorld.obj
-out:build\NativeHelloWorld.dll
Alternatively, the
cl
tool can be used to do the linking as follows:
cl -LD build
\
objs
\
NativeHelloWorld.obj -link
-out:build
\
NativeHelloWorld.dll
Both of these commands place the DLL directly into the
build
directory. The
former also puts the associated but unneeded EXP and LIB files in the build
directory, while the latter puts the EXP and LIB files in the directory from which
it is run.
22.3.7 Run the Java class
The final step is to run the Java class. We start Java in the normal way and specify
the
CLASSPATH
and the
javatech.jni22.JNIHelloWorld
class to be run.
We also must tell the operating system where to find the shared library file. In
Windows, the only search path used is
PATH
,soitisimportant to add the
build
directory (where we created the DLL file) to the
PATH
environment variable:
set PATH=build;%PATH%
java -classpath build/classes javatech.jni22.JNIHelloWorld
Alternatively, instead of modifying the
PATH
variable, we can utilize the
java.library.path
system property:
java -classpath build/classes -Djava.library.path=build
javatech.jni22.JNIHelloWorld
Search WWH ::
Custom Search