Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting System.loadLibrary to use an already loaded JNI library



> We have our own java launcher, based on the simpleJavaLauncher sample
> code. Before it creates and invokes the JVM it loads one of our JNI
> libraries, call it libFoo.jnilib, so as to call one of its methods.
> It does this using NSCreateObjectFileImageFromFile(), NSLinkModule(),
> NSLookupSymbolInModule(), etc. Later our Java code calls
> System.loadLibrary() for libFoo.jnilib, which loads a second copy of
> the library (verified with 'info sharedlibrary dyld' in gdb). Is
> there any way to get System.loadLibrary() to recognise that
> libFoo.jnilib is already loaded and use that copy rather than loading
> it again?

What's the old line--any programming problem can be solved with one more
layer of indirection? On Win32 there's a problem where if you load twice
in different classloaders it bombs out, which makes unit testing a bear. I
wrote this wrapper. You should be able to modify it to fix your problem
too.

(If you don't control the code calling System.loadLibrary() that's another
matter....)

/*<stdheader>****************************************************
* ClassLib Java Class Library
* Copyright (C) 2000,2001 Amber Archer Consulting Co., Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
***************************************************</stdheader>*/

package org.amberarcher.os;

import java.util.HashMap;
import java.util.Map;

/**
* Singleton used by the OSPlug API to manage shared library
* code. Many JVMs have a bug where they throw a spurious
* Exception when they load a shared object again from a
* separate ClassLoader. So long as you keep Librarian in
* the system CLASSPATH, it should be able to distinguish
* between Errors due to this problem and Errors due to a
* missing or malformed library.
*
* @author <a href="mailto:email@hidden";>Kyle F. Downey</a>
* @version $Revision: 1.3 $
*/
public class Librarian {

/**
* Singleton accessor method.
*/
public synchronized static Librarian getInstance() {
if (null == instance) {
instance = new Librarian();
}

return instance;
}

/**
* Attempts to load the named shared library for the
* given service class.
*
* @param serviceClass the desired OSService interface (not the
* implementation class that's making the request)
* @param libName the Java shared library name
* @exception UnsupportedServiceException if we can't load
* that library for any reason
*/
public synchronized void loadLibrary(Class serviceClass, String
libName) {
Boolean alreadyLoaded = (Boolean)libMap.get(libName);
if (null != alreadyLoaded) {
// if we tried before and it failed, don't bother
// re-trying--just fail immediately
if (alreadyLoaded == Boolean.FALSE) {
throw new UnsupportedServiceException(serviceClass);
}
} else {
// we must not yet have tried
try {
System.loadLibrary(libName);
libMap.put(libName, Boolean.TRUE);
} catch (Error e) {
e.printStackTrace();
libMap.put(libName, Boolean.FALSE);
throw new UnsupportedServiceException(e, serviceClass);
}
}
}

private Librarian() {
libMap = new HashMap();
}

private static Librarian instance;
private Map libMap;
}
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >Getting System.loadLibrary to use an already loaded JNI library (From: Mike Richmond <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.