I want to write an app that should run on Mac OS X (Mandatory) and
preferably also
on Windows and Other OS:es. My problem is that the code needs to access
some
functionality that is not available in the Java API.
Therefore I need to use the Java Native Interface. is it possible to
load JNI code,
so that it will automatically find the right shared library for the
platform used?
What I want to do is check what the current App with user focus is, and
create a record
each time the front app changes. (Something like Time Slice, but
possible to integrate
with Swedish book keeping and invoicing software)
I use the attached class to load jni libraries that are located in some
library directory besides the .jar file a class is defined in.
/*
* $Id$
* This is an unpublished work copyright (c) 2003 Jens-Uwe Mager
* 30177 Hannover, Germany, email@hidden
*/
package org.mager;
import java.io.*;
import java.net.*;
public class NativeUtil {
/**
* Locate a native library that is supposed to be in the same
* directory as the jar file containing the class needing the native
* library. If the class is not inside a jar file, assume a lib
* directory besides the class files. Also put the properties
* os.name and os.arch into the file name to be able to have native
* libraries for several platforms in this directory.
*/
public static String findLibraryBesides(Class c, String base) {
String resName = c.getName().replace('.', '/') + ".class";
String url = ClassLoader.getSystemClassLoader().getResource(resName).toString();
if (url.startsWith("jar:")) {
url = url.substring("jar:".length(), url.length());
url = url.substring(0, url.length() - resName.length() - 1);
/*
* Strip off the last segment, the name of the jar archive.
* We assume the native library is located in the same
* directory as the jar archive.
*/
int i = url.lastIndexOf('/');
if (i != -1)
url = url.substring(0, i);
} else {
/*
* The class file is not in a jar archive, assume the native
* library is in a directory named lib besides the classes.
*/
url = url.substring(0, url.length() - resName.length());
url += "lib";
}
/*
* Convert the base native library name to the OS specific name.
* If this name has '.' in it, put also the properties os.name
* and os.arch into it so it looks like
* base-os.name-os.arch.suffix. Convert blanks into '_' for Mac
* OS X.
*/
String libName = System.mapLibraryName(base);
int i = libName.lastIndexOf('.');
if (i != -1) {
String osName = System.getProperty("os.name").replace(' ', '_');
if (osName.startsWith("Windows"))
osName = "Windows";
String osArch = System.getProperty("os.arch").replace(' ', '_');
libName = libName.substring(0, i) + "-" + osName + "-" + osArch +
libName.substring(i);
}
url = url + "/" + libName;
String path = null;
try {
File f = new File(new URI(url));
if (f.exists())
path = f.getAbsolutePath();
} catch (URISyntaxException ex) {}
return path;
}
/**
* Use findLibraryBesides to load the native library, or fall back
* to searching the native library along the system defined library
* path.
*/
public static void loadLibraryBesides(Class c, String base) {
String path = findLibraryBesides(c, base);
if (path == null)
System.loadLibrary(base);
else
System.load(path);
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden
This email sent to email@hidden