Java Path File Read nio getTestClassPathURLClassLoader(ClassLoader parent)

Here you can find the source of getTestClassPathURLClassLoader(ClassLoader parent)

Description

get Test Class Path URL Class Loader

License

Open Source License

Parameter

Parameter Description
parent a class loader to be the parent for the returned one

Return

an UrlClassLoader with urls made of the 'test.class.path' jtreg property and with the given parent

Declaration

public static URLClassLoader getTestClassPathURLClassLoader(ClassLoader parent) 

Method Source Code


//package com.java2s;
/*//from   w w  w  . j  a  va2s . com
 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.io.File;

import java.net.MalformedURLException;

import java.net.URL;
import java.net.URLClassLoader;

import java.nio.file.Path;
import java.nio.file.Paths;

import java.util.Arrays;

public class Main {
    /**
     * Returns the value of 'test.class.path' system property.
     */
    public static final String TEST_CLASS_PATH = System.getProperty("test.class.path", ".");

    /**
     * @param parent a class loader to be the parent for the returned one
     * @return an UrlClassLoader with urls made of the 'test.class.path' jtreg
     *         property and with the given parent
     */
    public static URLClassLoader getTestClassPathURLClassLoader(ClassLoader parent) {
        URL[] urls = Arrays.stream(TEST_CLASS_PATH.split(File.pathSeparator)).map(Paths::get).map(Path::toUri)
                .map(x -> {
                    try {
                        return x.toURL();
                    } catch (MalformedURLException ex) {
                        throw new Error(
                                "Test issue. JTREG property" + " 'test.class.path'" + " is not defined correctly",
                                ex);
                    }
                }).toArray(URL[]::new);
        return new URLClassLoader(urls, parent);
    }
}

Related

  1. breadcrumbPath(String urlPrefix, String path, char sep)
  2. checkReadableDirectory(final Path directory)
  3. getReaderForFile(final Path file)
  4. getResourceAsStream(String resourcePath, ClassLoader classLoader, Class clazz, String resourceDesc, Consumer logger)
  5. load(Path p, PrintStream errorStream, String propertyInfo)
  6. loadFile(final Path filePath)
  7. loadFile(final Path p)
  8. loadFile(Path file)