Java ClassPath isInClassPath(String location)

Here you can find the source of isInClassPath(String location)

Description

Returns true if the specified location is in the JVM classpath.

License

Apache License

Parameter

Parameter Description
location the directory or jar name to test for

Exception

Parameter Description
MalformedURLException an exception

Return

true if location is in the JVM classpath

Declaration

public static boolean isInClassPath(String location) throws MalformedURLException 

Method Source Code


//package com.java2s;
/*//w w  w  .  ja  v  a 2 s .  c  om
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.util.StringTokenizer;

public class Main {
    /**
     * Returns true if the specified location is in the JVM classpath. This may
     * ignore additions to the classpath that are not reflected by the value in
     * <code>System.getProperty("java.class.path")</code>.
     * 
     * @param location the directory or jar name to test for
     * @return true if location is in the JVM classpath
     * @throws MalformedURLException
     */
    public static boolean isInClassPath(String location) throws MalformedURLException {
        return isInClassPath(new File(location).toURI().toURL());
    }

    /**
     * Returns true if the specified location is in the JVM classpath. This may
     * ignore additions to the classpath that are not reflected by the value in
     * <code>System.getProperty("java.class.path")</code>.
     * 
     * @param location the directory or jar URL to test for
     * @return true if location is in the JVM classpath
     * @throws MalformedURLException
     */
    public static boolean isInClassPath(URL location) throws MalformedURLException {
        String classPath = System.getProperty("java.class.path");
        StringTokenizer st = new StringTokenizer(classPath, File.pathSeparator);
        while (st.hasMoreTokens()) {
            String path = st.nextToken();
            if (location.equals(new File(path).toURI().toURL())) {
                return true;
            }
        }
        return false;
    }
}

Related

  1. getInputStreamFromClasspathFile(final String fileName)
  2. getResourceFromClasspath(final String fileName)
  3. getStreamFromClassPath(String source)
  4. getSystemClassPath()
  5. getSystemClasspathEntries()
  6. loadDirectoryFromClasspath(String path)
  7. loadFromClasspath(Class clazz, String fileName)
  8. loadFromClasspath(final String configFileName)
  9. loadResourceFromClasspath(Class clazz, String resourceName)