本文整理汇总了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();
}
}
示例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;
}
}
示例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();
}
}
示例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");
}
示例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());
}
示例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);
}
示例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);
}
示例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());
}
示例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);
}
示例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);
}
}
}
示例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) {}
}
示例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;
}
}
示例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);
}
示例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;
}
示例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);
}