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