Login  Register

Re: JOGL TempJarCache natives jars already in classpath

Posted by sam on Aug 14, 2013; 2:10pm
URL: https://forum.jogamp.org/JOGL-TempJarCache-natives-jars-already-in-classpath-tp4029804p4029810.html

gouessej wrote
I'm happy to see that it works for you now. Maybe Sven has a better suggestion, it would be better if you used the build-in mechanism of GlueGen that already works in applets and Java Web Start.
another interesting idea,

I looked up the code and there is already some special hook to specify a custom library loader. The advantage is using this custom library loader I can extract the files to a temporary directory instead of the primary one + I don't need to now the exact file names.

code

1. before starting JOGL set that it should use a custom library loader:
System.setProperty("jnlp.launcher.class", SandBoxLibraryLoader.class.getCanonicalName());

2. the code of SandBoxLibraryLoader:
public final class SandBoxLibraryLoader {
	/**
	 * extract the given library of the classpath and put it to a temporary file
	 */
	public static File toTemporaryFile(String libName) throws IOException {
		// convert to native library name
		libName = System.mapLibraryName(libName);

		// create
		String extension = Files.getFileExtension(libName);
		File file = File.createTempFile(StringUtils.removeEnd(libName, extension), "." + extension);
		file.deleteOnExit();

		URL res = SandBoxLibraryLoader.class.getResource("/" + libName);
		if (res == null)
			throw new FileNotFoundException("can't extract: " + libName);
		try (InputStream in = res.openStream();
				OutputStream to = new BufferedOutputStream(new FileOutputStream(file))) {
			ByteStreams.copy(in, to);
		} catch (IOException e) {
			System.err.println("can't extract: " + libName);
			e.printStackTrace();
			throw new FileNotFoundException("can't extract: " + libName);
		}
		return file;
	}

	/**
	 * convention for custom library loader
	 */
	public static void loadLibrary(String libName) throws IOException {
		File file = toTemporaryFile(libName);
		// use System.load as it supports absolute file paths
		System.load(file.getAbsolutePath());
	}
}