The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Java Native Interface
Lesson: Writing a Java Program with Native Methods

Step 5: Create a Shared Library

Remember in Step 1: Write the Java Code you used the following method call to load a shared library named hello into your program at runtime:
System.loadLibrary("hello");
Now you are ready to create this shared library.

In the previous step, Step 4: Write the Native Method Implementation, you created a C file in which you wrote the implementation for the displayHelloWorld native method. You saved the native method in the file HelloWorldImp.c. Now, you must compile HelloWorldImp.c into a shared library, which you name hello to match the library name used in the System.loadLibrary method.

Compile the native language code that you created in the previous two steps into a shared library. On Solaris, you'll create a shared library, while on Windows 95/NT you'll create a dynamic link library (DLL). Remember to specify the path or paths to all necessary header files. See Creating and Loading Shared Libraries for information on forming a shared library name.

On Solaris, the following command builds a shared library libhello.so:

cc -G -I/usr/local/java/include -I/usr/local/java/include/solaris \
      HelloWorldImp.c -o libhello.so

On Win32, the following command builds a dynamic link library hello.dll using Microsoft Visual C++ 4.0:

cl -Ic:\java\include -Ic:\java\include\win32
      -LD HelloWorldImp.c -Fehello.dll
Of course, you need to specify the include path that corresponds to the setup on your own machine.

For more information on the system-dependent mechanisms for loading a shared library, see Creating and Loading Shared Libraries.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.