当前位置: 首页>>代码示例>>Java>>正文


Java URLClassLoader.newInstance方法代码示例

本文整理汇总了Java中java.net.URLClassLoader.newInstance方法的典型用法代码示例。如果您正苦于以下问题:Java URLClassLoader.newInstance方法的具体用法?Java URLClassLoader.newInstance怎么用?Java URLClassLoader.newInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.URLClassLoader的用法示例。


在下文中一共展示了URLClassLoader.newInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getClassLoader

import java.net.URLClassLoader; //导入方法依赖的package包/类
private static ClassLoader getClassLoader(Optional<String> classPath) {
    if (classPath.isPresent()) {
        final File localPath = new File(classPath.get());

        if (!localPath.exists())
            throw new RuntimeException(classPath.get() +  ": does not exist");

        try {
            final URL fileURL = localPath.toURI().toURL();
            return URLClassLoader.newInstance(new URL[] { fileURL });
        } catch (MalformedURLException ex) {
            throw new RuntimeException(ex);
        }
    } else {
        return currentThread().getContextClassLoader();
    }
}
 
开发者ID:adamkewley,项目名称:jobson,代码行数:18,代码来源:CustomAuthenticatorConfig.java

示例2: newModuleClassLoader

import java.net.URLClassLoader; //导入方法依赖的package包/类
private static URLClassLoader newModuleClassLoader() {
  String cp = System.getenv(Constants.ENV_MODULE_CLASSPATH);
  if (cp != null) {
    List<URL> urls = new ArrayList<>();
    for (String s : cp.split(File.pathSeparator)) {
      s = s.trim();
      if (!s.isEmpty()) {
        URL url = null;
        try {
          url = new File(s).toURI().toURL();
        } catch (MalformedURLException e) {
          System.err.println(e.getMessage());
        }
        if (url != null) {
          urls.add(url);
        }
      }
    }

    return URLClassLoader.newInstance(urls.toArray(new URL[urls.size()]));
  } else {
    return null;
  }
}
 
开发者ID:kroepke,项目名称:luna,代码行数:25,代码来源:LunaConsole.java

示例3: getResourceClassLoader

import java.net.URLClassLoader; //导入方法依赖的package包/类
public ClassLoader getResourceClassLoader()
{

    File f = new File("woof", "alfrescoResources");
    if (f.canRead() && f.isDirectory())
    {

        URL[] urls = new URL[1];

        try
        {
            URL url = f.toURI().normalize().toURL();
            urls[0] = url;
        }
        catch (MalformedURLException e)
        {
            throw new AlfrescoRuntimeException("Failed to add resources to classpath ", e);
        }

        return URLClassLoader.newInstance(urls, this.getClass().getClassLoader());
    }
    else
    {
        return this.getClass().getClassLoader();
    }
}
 
开发者ID:Alfresco,项目名称:alfresco-solrclient,代码行数:27,代码来源:SOLRAPIClientTest.java

示例4: generateReport

import java.net.URLClassLoader; //导入方法依赖的package包/类
public void generateReport(String packageName,List<String> flagList) throws IOException
{

    URL testClassesURL = Paths.get("target/test-classes").toUri().toURL();

    URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{testClassesURL},
            ClasspathHelper.staticClassLoader());

    reflections = new Reflections(new ConfigurationBuilder()
            .setUrls(ClasspathHelper.forPackage(packageName,classLoader))
            .addClassLoader(classLoader)
            .filterInputsBy(new FilterBuilder().includePackage(packageName))
            .setScanners(new MethodAnnotationsScanner(), new TypeAnnotationsScanner(), new SubTypesScanner())
    );


    List<Map<String, TestClass>> list = new ArrayList<>();

    for (String flag : flagList)
    {
        list.add(printMethods(flag));
    }

    Gson gson = new Gson();
    String overviewTemplate = IOUtils.toString(getClass().getResourceAsStream("/index.tpl.html"));


    String editedTemplate = overviewTemplate.replace("##TEST_DATA##", gson.toJson(list));

    FileUtils.writeStringToFile(new File("target/test-list-html-report/index.html"), editedTemplate);
    logger.info("report file generated");
}
 
开发者ID:cyildirim,项目名称:Lahiya,代码行数:33,代码来源:LahiyaTestCaseReport.java

示例5: loadJars

import java.net.URLClassLoader; //导入方法依赖的package包/类
private static ClassLoader loadJars(String[] jarFiles) {
    URL[] urls = Stream.of(jarFiles)
            .map(name -> {
                try {
                    return new File(name).toURI().toURL();
                } catch (MalformedURLException e) {
                    System.out.println("Fail to load jar file: " + name);
                    e.printStackTrace();
                    return null;
                }
            })
            .toArray(URL[]::new);
    return URLClassLoader.newInstance(urls, ClassLoader.getSystemClassLoader());
}
 
开发者ID:Piasy,项目名称:FridaAndroidTracer,代码行数:15,代码来源:FridaAndroidTracer.java

示例6: loadClass

import java.net.URLClassLoader; //导入方法依赖的package包/类
/**
 * @param className A non-null fully qualified class name.
 * @param codebase A non-null, space-separated list of URL strings.
 */
public Class<?> loadClass(String className, String codebase)
        throws ClassNotFoundException, MalformedURLException {

    ClassLoader parent = getContextClassLoader();
    ClassLoader cl =
             URLClassLoader.newInstance(getUrlArray(codebase), parent);

    return loadClass(className, cl);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:14,代码来源:VersionHelper12.java

示例7: loadBundles

import java.net.URLClassLoader; //导入方法依赖的package包/类
private List<Tuple<PluginInfo,Plugin>> loadBundles(List<Bundle> bundles) {
    List<Tuple<PluginInfo, Plugin>> plugins = new ArrayList<>();

    for (Bundle bundle : bundles) {
        // jar-hell check the bundle against the parent classloader
        // pluginmanager does it, but we do it again, in case lusers mess with jar files manually
        try {
            final List<URL> jars = new ArrayList<>();
            jars.addAll(Arrays.asList(JarHell.parseClassPath()));
            jars.addAll(bundle.urls);
            JarHell.checkJarHell(jars.toArray(new URL[0]));
        } catch (Exception e) {
            throw new IllegalStateException("failed to load bundle " + bundle.urls + " due to jar hell", e);
        }

        // create a child to load the plugins in this bundle
        ClassLoader loader = URLClassLoader.newInstance(bundle.urls.toArray(new URL[0]), getClass().getClassLoader());
        for (PluginInfo pluginInfo : bundle.plugins) {
            // reload lucene SPI with any new services from the plugin
            reloadLuceneSPI(loader);
            final Class<? extends Plugin> pluginClass = loadPluginClass(pluginInfo.getClassname(), loader);
            final Plugin plugin = loadPlugin(pluginClass, settings);
            plugins.add(new Tuple<>(pluginInfo, plugin));
        }
    }

    return Collections.unmodifiableList(plugins);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:29,代码来源:PluginsService.java

示例8: nonNullParameters

import java.net.URLClassLoader; //导入方法依赖的package包/类
static void nonNullParameters() {
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  NullAwayNativeModels.class.getResource(null);
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  NullAwayNativeModels.class.isAssignableFrom(null);
  String s = null;
  // BUG: Diagnostic contains: passing @Nullable parameter 's' where @NonNull is required
  File f = new File(s);
  // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required
  URLClassLoader.newInstance(null, NullAwayNativeModels.class.getClassLoader());
}
 
开发者ID:uber,项目名称:NullAway,代码行数:12,代码来源:NullAwayNativeModels.java

示例9: marshallClassCastExceptionTest

import java.net.URLClassLoader; //导入方法依赖的package包/类
@Test
public void marshallClassCastExceptionTest() throws Exception {
    JAXBContext jaxbContext;
    Marshaller marshaller;
    URLClassLoader jaxbContextClassLoader;
    // Generate java classes by xjc
    runXjc(XSD_FILENAME);
    // Compile xjc generated java files
    compileXjcGeneratedClasses();

    // Create JAXB context based on xjc generated package.
    // Need to create URL class loader ot make compiled classes discoverable
    // by JAXB context
    jaxbContextClassLoader = URLClassLoader.newInstance(new URL[] {testWorkDirUrl});
    jaxbContext = JAXBContext.newInstance( TEST_PACKAGE, jaxbContextClassLoader);

    // Create instance of Xjc generated data type.
    // Java classes were compiled during the test execution hence reflection
    // is needed here
    Class classLongListClass = jaxbContextClassLoader.loadClass(TEST_CLASS);
    Object objectLongListClass = classLongListClass.newInstance();
    // Get 'getIn' method object
    Method getInMethod = classLongListClass.getMethod( GET_LIST_METHOD, (Class [])null );
    // Invoke 'getIn' method
    List<Long> inList = (List<Long>)getInMethod.invoke(objectLongListClass);
    // Add values into the jaxb object list
    inList.add(Long.valueOf(0));
    inList.add(Long.valueOf(43));
    inList.add(Long.valueOf(1000000123));

    // Marshall constructed complex type variable to standard output.
    // In case of failure the ClassCastException will be thrown
    marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(objectLongListClass, System.out);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:37,代码来源:JaxbMarshallTest.java

示例10: setKitPath

import java.net.URLClassLoader; //导入方法依赖的package包/类
public void setKitPath(String kitPath) {
  settings.setKitPath(kitPath);
  if (kitPath != null && !kitPath.isEmpty()) {
    try {
      URL[] urls = discoverKitClientJars(kitPath);
      urlClassLoader = URLClassLoader.newInstance(urls, Thread.currentThread().getContextClassLoader());
    } catch (Exception e) {
      throw new RuntimeException("Please make sure the kitPath is properly set, current value is : " + kitPath, e);
    }
  }
}
 
开发者ID:Terracotta-OSS,项目名称:tinypounder,代码行数:12,代码来源:KitAwareClassLoaderDelegator.java

示例11: loadClass

import java.net.URLClassLoader; //导入方法依赖的package包/类
/**
 * Refreshes the class that the ArJClassEditor runs with the current .class file
 * @throws MalformedURLException if the class fails to load into the ArJClassEditor
 */
public void loadClass() throws MalformedURLException
{
	URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
	try 
	{
		cls = Class.forName(fileDot, true, classLoader);
	} 
	catch (ClassNotFoundException e) {}
}
 
开发者ID:arjunvnair,项目名称:ArJ-class-editor,代码行数:14,代码来源:ArJClassEditor.java

示例12: getURLClassLoader

import java.net.URLClassLoader; //导入方法依赖的package包/类
ClassLoader getURLClassLoader(String[] url)
    throws MalformedURLException {
        ClassLoader parent = getContextClassLoader();
        /*
         * Classes may only be loaded from an arbitrary URL code base when
         * the system property com.sun.jndi.ldap.object.trustURLCodebase
         * has been set to "true".
         */
        if (url != null && "true".equalsIgnoreCase(trustURLCodebase)) {
            return URLClassLoader.newInstance(getUrlArray(url), parent);
        } else {
            return parent;
        }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:15,代码来源:VersionHelper12.java

示例13: loadBundles

import java.net.URLClassLoader; //导入方法依赖的package包/类
private List<Tuple<PluginInfo,Plugin>> loadBundles(List<Bundle> bundles) {
    List<Tuple<PluginInfo, Plugin>> plugins = new ArrayList<>();

    for (Bundle bundle : bundles) {
        // jar-hell check the bundle against the parent classloader
        // pluginmanager does it, but we do it again, in case lusers mess with jar files manually
        try {
            final List<URL> jars = new ArrayList<>();
            jars.addAll(Arrays.asList(JarHell.parseClassPath()));
            jars.addAll(bundle.urls);
            JarHell.checkJarHell(jars.toArray(new URL[0]));
        } catch (Exception e) {
            throw new IllegalStateException("failed to load bundle " + bundle.urls + " due to jar hell", e);
        }

        // create a child to load the plugins in this bundle
        ClassLoader loader = URLClassLoader.newInstance(bundle.urls.toArray(new URL[0]), getClass().getClassLoader());
        for (PluginInfo pluginInfo : bundle.plugins) {
            final Plugin plugin;
            if (pluginInfo.isJvm()) {
                // reload lucene SPI with any new services from the plugin
                reloadLuceneSPI(loader);
                Class<? extends Plugin> pluginClass = loadPluginClass(pluginInfo.getClassname(), loader);
                plugin = loadPlugin(pluginClass, settings);
            } else {
                plugin = new SitePlugin(pluginInfo.getName(), pluginInfo.getDescription());
            }
            plugins.add(new Tuple<>(pluginInfo, plugin));
        }
    }

    return Collections.unmodifiableList(plugins);
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:34,代码来源:PluginsService.java

示例14: execute

import java.net.URLClassLoader; //导入方法依赖的package包/类
public List<String> execute(Parser parser, String astJson) {
    List<String> synthesizedPrograms = new LinkedList<>();
    List<DSubTree> asts = getASTsFromNN(astJson);

    classLoader = URLClassLoader.newInstance(parser.classpathURLs);

    CompilationUnit cu = parser.cu;
    List<String> programs = new ArrayList<>();
    for (DSubTree ast : asts) {
        Visitor visitor = new Visitor(ast, new Document(parser.source), cu, mode);
        try {
            cu.accept(visitor);
            if (visitor.synthesizedProgram == null)
                continue;
            String program = visitor.synthesizedProgram.replaceAll("\\s", "");
            if (! programs.contains(program)) {
                String formattedProgram = new Formatter().formatSource(visitor.synthesizedProgram);
                programs.add(program);
                synthesizedPrograms.add(formattedProgram);
            }
        } catch (SynthesisException|FormatterException e) {
            // do nothing and try next ast
        }
    }

    return synthesizedPrograms;
}
 
开发者ID:capergroup,项目名称:bayou,代码行数:28,代码来源:Synthesizer.java

示例15: testURLClassLoaderURL

import java.net.URLClassLoader; //导入方法依赖的package包/类
private static void testURLClassLoaderURL(URL jarURL,
        int mainVersionExpected, int helperVersionExpected,
        int resourceVersionExpected) throws ClassNotFoundException,
        NoSuchMethodException, IllegalAccessException,
        IllegalArgumentException, InvocationTargetException, IOException {
    System.out.println(
            "Testing URLClassLoader MV JAR support for URL: " + jarURL);
    URL[] urls = { jarURL };
    int mainVersionActual;
    int helperVersionActual;
    int resourceVersionActual;
    try (URLClassLoader cl = URLClassLoader.newInstance(urls)) {
        Class c = cl.loadClass("testpackage.Main");
        Method getMainVersion = c.getMethod("getMainVersion");
        mainVersionActual = (int) getMainVersion.invoke(null);
        Method getHelperVersion = c.getMethod("getHelperVersion");
        helperVersionActual = (int) getHelperVersion.invoke(null);
        try (InputStream ris = cl.getResourceAsStream("versionResource");
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(ris))) {
            resourceVersionActual = Integer.parseInt(br.readLine());
        }
    }

    assertEquals(mainVersionActual, mainVersionExpected,
                     "Test failed: Expected Main class version: "
                     + mainVersionExpected + " Actual version: "
                     + mainVersionActual);
    assertEquals(helperVersionActual, helperVersionExpected,
                     "Test failed: Expected Helper class version: "
                     + helperVersionExpected + " Actual version: "
                     + helperVersionActual);
    assertEquals(resourceVersionActual, resourceVersionExpected,
                     "Test failed: Expected resource version: "
                     + resourceVersionExpected + " Actual version: "
                     + resourceVersionActual);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:38,代码来源:RuntimeTest.java


注:本文中的java.net.URLClassLoader.newInstance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。