1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils;
15 import java.util.Properties;
19 * @author Toni Kalajainen
21 public class LibraryPathUtils {
23 // The problem with JMF is that it is (should be)
24 // Located in Java CLASSPATH. For some reason eclipse java VM does not
25 // Look into CLASSPATH.
26 // The following code adds CLASSPATH to System enviroment java.library.path
27 public static void addCLASSPATHtoEclipseVariables() {
28 String CLASSPATH = System.getenv("CLASSPATH");
29 if (CLASSPATH != null)
30 for (String classPath : CLASSPATH.split(";")) {
31 if (!classPath.toLowerCase().endsWith(".jar"))
32 addLibraryPath(classPath);
33 addClassPath(classPath);
35 CLASSPATH = System.getenv("PATH");
36 if (CLASSPATH != null)
37 for (String classPath : CLASSPATH.split(";")) {
38 addLibraryPath(classPath);
41 CLASSPATH = System.getenv("LD_LIBRARY_PATH");
42 if (CLASSPATH != null)
43 for (String classPath : CLASSPATH.split(";")) {
44 addLibraryPath(classPath);
47 // printEnvVariables();
50 public static void addLibraryPath(String newLibrary) {
51 if (newLibrary == null || "".equals(newLibrary))
53 String java_library_path = System.getProperty("java.library.path");
54 if (java_library_path != null) {
55 for (String library : java_library_path.split(";"))
56 if (library.equals(newLibrary))
60 // System.out.println("Adding path: " + newLibrary);
62 if (java_library_path == null || "".equals(java_library_path))
63 java_library_path = newLibrary;
65 if (!java_library_path.endsWith(";"))
66 java_library_path += ";";
67 java_library_path += newLibrary + ";";
70 System.setProperty("java.library.path", java_library_path);
73 public static void addClassPath(String newLibrary) {
74 if (newLibrary == null || "".equals(newLibrary))
76 String java_library_path = System.getProperty("java.class.path");
77 if (java_library_path != null) {
78 for (String library : java_library_path.split(";"))
79 if (library.equals(newLibrary))
83 // System.out.println("Adding cp: " + newLibrary);
85 if (java_library_path == null || "".equals(java_library_path))
86 java_library_path = newLibrary;
88 if (!java_library_path.endsWith(";"))
89 java_library_path += ";";
90 java_library_path += newLibrary + ";";
93 System.setProperty("java.class.path", java_library_path);
96 public static void printEnvVariables() {
97 // Add env variable CLASSPATH
98 Properties props = System.getProperties();
99 for (Object key : props.keySet()) {
100 Object value = props.get(key);
101 System.out.println(key + "=" + value);
104 System.out.println("\n\nEnv:");
105 for (Object key : System.getenv().keySet()) {
106 Object value = System.getenv().get(key);
107 System.out.println(key + "=" + value);
112 * Find location of a jar library
114 * @param library name of the library
115 * @return full location of the library
117 public static String findLibrary(String library) {
118 String CLASSPATH = System.getenv("CLASSPATH");
120 if (CLASSPATH == null)
122 // 1st search case sensitive
123 for (String classPath : CLASSPATH.split(";"))
124 if (classPath.endsWith(library))
127 // Widen search for insensitive
128 for (String classPath : CLASSPATH.split(";"))
129 if (classPath.toLowerCase().endsWith(library.toLowerCase()))
135 * This method searches for CLASSPATH and converts libraries there into
136 * separate key,value pairs in system properties. The key will be
137 * name of the library and value will be it's location in file system.
139 * This method acts as a helper tool for loading external libraries into
140 * the visibility of bundle class loader.
142 * LibraryPathUtils.createSystemPropertiesFromClassPathLibraries();
144 * This call should be invoked at the very beginning of application.
145 * This way external libraries can by dynamicly loaded at runtime.
146 * The way to do this, is to add "external:$library.jar$" into plugin
147 * settings runtime/classpath,
148 * in Manifest.mf it is "Bundle-ClassPath: external:$library.jar$"
150 public static void createSystemPropertiesFromClassPathLibraries() {
151 String CLASSPATH = System.getenv("CLASSPATH");
153 if (CLASSPATH == null)
156 // 1st search case sensitive
157 for (String classPath : CLASSPATH.split(";")) {
158 //System.out.println(classPath);
159 if (!classPath.toLowerCase().endsWith(".jar")) continue;
160 File lib = new File(classPath);
162 //System.out.println("Warning library \""+classPath+"\" does not exist yet it exists in CLASSPATH.");
166 String key = lib.getName();
167 String value = lib.getAbsolutePath();
168 System.setProperty(key, value);
169 //System.out.println(key+"="+value);