Java Reference
In-Depth Information
these libraries is built and maintained with the ldconfig program. The library
loader in the JVM, however, works as the shared library system in Solaris, where
the LD_LIBRARY_PATH is searched for shared libraries. If you try a JNI method
and get library errors, check your LD_LIBRARY_PATH first. Here, we used “ . ”,
meaning “current directory.” In practice, you wouldn't do this. You would de-
ploy your shared library to a standard location and have LD_LIBRARY_PATH
preset to that directory or directories. We just wanted to show you how it
works here.
Let's see our class in action now.
$ java GetUser
mschwarz
$ su
Password:
# export LD_LIBRARY_PATH=.
# java GetUser
root
# exit
exit
$
To JNI or Not to JNI
We dislike religious debates. We have no desire to nail down what taints
the purity of Java and what does not. A warning we do want to give you
is, if you are an experienced UNIX C/C++ developer, you must resist the
temptation to use JNI and native methods all over the place. The Java
APIs are extensive, and there are probably classes that already do what
you want to do. You will be tempted to use native methods because “you
know how to do it in C.” Resist. Find the Java way. JNI is a great way to
introduce subtle and hard to find bugs into your Java programs. Leave
that to the API and JVM coders. ;-)
That said, we don't want to discourage you from making use of JNI
when it is the right way, or the only way, for what you need to do. The
tool is there. Use it. Just remember what it does cost you in portability
and what it may cost you in maintenance and debugging. Design
decisions have costs and benefits. Try to find the balance.
Search WWH ::




Custom Search