Java ClassPath Add addClassPath(File filePath)

Here you can find the source of addClassPath(File filePath)

Description

Adds the specified file path to the current thread's class loader.

License

Open Source License

Parameter

Parameter Description
filePath Directory or jar file.

Exception

Parameter Description
MalformedURLException Thrown if the file path is invalid.

Declaration

public static void addClassPath(File filePath) throws MalformedURLException 

Method Source Code


//package com.java2s;
/*//from w  w  w  . j  a  v  a2 s  .  c  o m
 * Copyright (c) 2013-2015 Netcrest Technologies, LLC. All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class Main {
    /**
     * Adds the specified file path to the current thread's class loader.
     * 
     * @param filePath
     *            Directory or jar file.
     * @throws MalformedURLException
     *             Thrown if the file path is invalid.
     */
    public static void addClassPath(File filePath) throws MalformedURLException {
        ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader();

        // Add the conf dir to the classpath
        // Chain the current thread classloader
        URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { filePath.toURI().toURL() },
                currentThreadClassLoader);

        // Replace the thread classloader - assumes
        // you have permissions to do so
        Thread.currentThread().setContextClassLoader(urlClassLoader);
    }
}

Related

  1. addAllFilesToExtClassLoaderClasspathWithReg(String sDir, String regexpFilter)
  2. addBundleToClassPath(String bundleId)
  3. addClasspath(String path)
  4. addClassPath2ClassLoader(ClassLoader cl, String path)
  5. addClasspathEntries(Collection cpEntries)
  6. addClassPathItems(String[] cpItems)