1 /*-----------------------------------------------------------------------------------*/
\r
3 * Modify java.library.path at runtime.
\r
4 * @author Allan CORNET - INRIA 2007
\r
6 /*-----------------------------------------------------------------------------------*/
\r
7 package org.scilab.modules.jvm;
\r
8 /*-----------------------------------------------------------------------------------*/
\r
9 import java.io.IOException;
\r
10 import java.io.File;
\r
11 import java.lang.reflect.Field;
\r
12 /*-----------------------------------------------------------------------------------*/
\r
13 /*http://forum.java.sun.com/thread.jspa?threadID=135560&start=15&tstart=0 */
\r
14 /*-----------------------------------------------------------------------------------*/
\r
17 * LibraryPath to overload java.library.path.
\r
19 public class LibraryPath {
\r
21 private static final String JAVALIBRARYPATH = "java.library.path";
\r
25 protected LibraryPath() {
\r
26 /* indicate that the requested operation is not supported */
\r
27 throw new UnsupportedOperationException();
\r
29 /*--------------------------------------------------------------------------*/
\r
31 * checks if pathToAdd already exists
\r
32 * @param currentpaths list of current paths
\r
33 * @param pathToAdd path to add
\r
34 * @return a boolean true if path already exists
\r
36 private static boolean pathAlreadyExists(String currentpaths, String pathToAdd) {
\r
37 String[] paths = currentpaths.split("" + File.pathSeparatorChar);
\r
38 for (String libraryPath : paths) {
\r
39 if (libraryPath.equalsIgnoreCase(pathToAdd)) { return true; }
\r
43 /*--------------------------------------------------------------------------*/
\r
45 * add a path to java.library.path
\r
46 * @param p path to add
\r
47 * @throws IOException return a exception
\r
49 public static void addPath(final String p) throws IOException {
\r
50 if (!pathAlreadyExists(System.getProperty(JAVALIBRARYPATH), p)) {
\r
51 String newLibPath = p + File.pathSeparator + System.getProperty(JAVALIBRARYPATH);
\r
52 System.setProperty(JAVALIBRARYPATH, newLibPath);
\r
54 Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
\r
55 fieldSysPath.setAccessible(true);
\r
56 if (fieldSysPath != null) {
\r
57 fieldSysPath.set(System.class.getClassLoader(), null);
\r
59 } catch (NoSuchFieldException e) {
\r
60 throw new IOException("Error NoSuchFieldException, could not add path to " + JAVALIBRARYPATH);
\r
61 } catch (IllegalAccessException e) {
\r
62 throw new IOException("Error IllegalAccessException, could not add path to " + JAVALIBRARYPATH);
\r
67 /*--------------------------------------------------------------------------*/
\r
69 * get the scilab java.library.path.
\r
70 * @return librarypath
\r
72 public static String[] getLibraryPath() {
\r
73 String librarypath = System.getProperty(JAVALIBRARYPATH);
\r
74 String[] paths = librarypath.split("" + File.pathSeparatorChar);
\r
78 /*-----------------------------------------------------------------------------------*/
\r