Documentation Archive Developer
Search

ADC Home > Reference Library > Technical Q&As > Legacy Documents > Java >

Legacy Documentclose button

Important: This document is part of the Legacy section of the ADC Reference Library. This information should not be used for new development.

Current information on this Reference Library topic can be found here:

Checking for the presence of a native library from Java


Q: My Java application relies on a native library to be present in the MRJ Libraries folder, which is in the Extensions folder. How do I check to see if the user has correctly placed my library in the MRJ Libraries folder?

A: There are many ways to do this. Perhaps the simplest way is to do the following:


if (!(new File(System.getProperty("java.home"), "myLibrary").exists()))
    // report an error to the user

This is probably not the best approach. Native libraries really should reside either in the Extensions folder of the System, or in the local application directory. For example, if your application uses GL4Java, you will not be able to use the above approach because the native library is not in the MRJ Libraries folder. Furthermore, libraries used by a single app should not go in the Extensions folder but instead in the same folder as the app. This simplifies configuration, installation and de-installation, and reduces the chance of library name conflicts with some other vendor's code.

If you really do need to find a library in the extensions folder, you can do the following:


import com.apple.mrj.MRJFileUtils;
import com.apple.mrj.MRJApplicationUtils;
try
{
    if ( MRJApplicationUtils.isMRJToolkitAvailable() )
    {
        File theFile = MRJFileUtils.
            findFolder(MRJFileUtils.kExtensionFolderType);
        File libraryFile = new File( theFile, "myLibrary" );
        if (( libraryFile.exists() )
            System.out.println( "Found: "  + libraryFile.getPath() );
        else
            System.out.println( "Library " + libraryFile.getPath() 
                + " not found" );
     }
     else
        System.out.println("MRJ Toolkit is not available on this machine.");
}
catch ( FileNotFoundException fnf )
{
    fnf.printStackTrace();
}

Keep in mind that you will need to add MRJToolkitStubs.zip to your project in order to link successfully. This library file contains the proper class declarations, but only stub implementations. The Java classes, such as MRJFileUtils which is used by this example, are stored in MRJClasses.zip, which is placed in the Extensions folder as part of the normal MRJ installation.

MRJFileUtils and MRJApplicationUtils are part of MRJToolkit which is part of the MRJ SDK. The MRJToolkit provides several convenient functions for adding standard Macintosh functionality to your Java application. For more information on findFolder( ) or other methods in the toolkit, please consult the document About MRJToolkit in the MRJ SDK.

This example is designed to work on a Macintosh using Macintosh Runtime for Java 2.1.4 or later. You may need to use a different approach on other platforms. Note that we check to see if the MRJ Toolkit is available before we call findFolder( ). Although this is not necesssary in this case (findFolder( ) will return an empty file object if MRJ Toolkit is not available), it is good practice to take this approach when writing platform-specific code.

[Nov 29 1999]