ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader(); // Add the conf dir to the classpath // Chain the current thread classloader URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{new File("mtFile").toURL()}, currentThreadClassLoader); // Replace the thread classloader - assumes // you have permissions to do so Thread.currentThread().setContextClassLoader(urlClassLoader);
private URL findFile(String importFile) throws IOException { File file = new File(this.inputDir, importFile); URL result = null; if (file.exists()) result = file.toURL(); else if (this.resourceLoader != null) result = this.resourceLoader.getResource(importFile); if (result == null) throw new FileNotFoundException(importFile); return result; }
public static void addPath(String s) throws Exception { File f = new File(s); URL u = f.toURL(); URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class urlClass = URLClassLoader.class; Method method = urlClass.getDeclaredMethod("addURL", new Class[]{URL.class}); method.setAccessible(true); method.invoke(urlClassLoader, new Object[]{u}); }
/** * Create a URL from a string representing a path to a local file. * The path string can be just a path, or can start with file:/, file:/// * @param onestr path string * @return */ private static URL urlFromPathString(String onestr) { URL oneurl = null; try { if (onestr.startsWith("file:/")) { oneurl = new URL(onestr); } else { oneurl = new File(onestr).toURL(); } } catch (Exception err) { LOG.error("Bad URL " + onestr + ", ignoring path"); } return oneurl; }
try { if (StringUtils.indexOf(path, "file:/") == 0) { url = new URL(path); } else if (StringUtils.indexOf(path, "hdfs:/") == 0 || StringUtils.indexOf(path, "viewfs:/") == 0) { currentTS = -1L; if (!new File(localFile.toString()).exists() || currentTS < timeStamp) { LOG.info("Copying " + remoteFile + " to " + localFile); FileSystem remoteFS = remoteFile.getFileSystem(conf); url = new File(path).toURL();
public boolean addlocaldriverjar(String line) { // If jar file is in the hdfs, it should be downloaded first. String jarPath = arg1(line, "jar path"); File p = new File(jarPath); if (!p.exists()) { beeLine.error("The jar file in the path " + jarPath + " can't be found!"); return false; } URLClassLoader classLoader = (URLClassLoader) Thread.currentThread().getContextClassLoader(); try { beeLine.debug(jarPath + " is added to the local beeline."); URLClassLoader newClassLoader = new URLClassLoader(new URL[]{p.toURL()}, classLoader); Thread.currentThread().setContextClassLoader(newClassLoader); beeLine.setDrivers(Arrays.asList(beeLine.scanDrivers(false))); } catch (Exception e) { beeLine.error("Fail to add local jar due to the exception:" + e); beeLine.error(e); } return true; }
if (!xmlFile.exists() || !xmlFile.isFile()) { } else { try { url = xmlFile.toURL(); } catch (MalformedURLException ex) { throw new CacheXmlException( File defaultFile = DistributionConfig.DEFAULT_CACHE_XML_FILE; if (!xmlFile.equals(defaultFile)) { if (!xmlFile.exists()) { throw new CacheXmlException( String.format("Declarative Cache XML file/resource %s does not exist.",
protected XMLStreamReader2 createSR(File f, boolean forER, boolean autoCloseInput) throws XMLStreamException { ReaderConfig cfg = createPrivateConfig(); try { /* 18-Nov-2008, TSa: If P_BASE_URL is set, and File reference is * relative, let's resolve against base... */ if (!f.isAbsolute()) { URL base = cfg.getBaseURL(); if (base != null) { URL src = new URL(base, f.getPath()); return createSR(cfg, SystemId.construct(src), URLUtil.inputStreamFromURL(src), forER, autoCloseInput); } } final SystemId systemId = SystemId.construct(f.toURL()); return createSR(cfg, systemId, new FileInputStream(f), forER, autoCloseInput); } catch (IOException ie) { throw new WstxIOException(ie); } }
private URLClassLoader createExtensionClassLoader(File targetFolder) { URLClassLoader urlClassLoader; try { urlClassLoader = new URLClassLoader(new URL[] {targetFolder.toURL()}); } catch (MalformedURLException e) { throw new RuntimeException(e); } return urlClassLoader; } }
/** * Returns the URL of the {@code WEB-INF/classes} folder. * <p> * This finds the URLs using the {@link ServletContext}. * * @return the collection of URLs, not null */ public static URL forWebInfClasses(final ServletContext servletContext) { try { final String path = servletContext.getRealPath("/WEB-INF/classes"); if (path != null) { final File file = new File(path); if (file.exists()) return file.toURL(); } else { return servletContext.getResource("/WEB-INF/classes"); } } catch (MalformedURLException e) { /*fuck off*/ } return null; }
/** * Create a URL from a string representing a path to a local file. * The path string can be just a path, or can start with file:/, file:/// * @param onestr path string * @return */ private static URL urlFromPathString(String onestr) { URL oneurl = null; try { if (StringUtils.indexOf(onestr, "file:/") == 0) { oneurl = new URL(onestr); } else { oneurl = new File(onestr).toURL(); } } catch (Exception err) { LOG.error("Bad URL {}, ignoring path", onestr); } return oneurl; }
File file = new File("c:\\myjar.jar"); URL url = file.toURL(); URL[] urls = new URL[]{url}; ClassLoader cl = new URLClassLoader(urls); Class cls = cl.loadClass("com.mypackage.myclass");
File saturnAppLibDir = new File( project.getBuild().getDirectory() + System.getProperty("file.separator") + "saturn-run"); if (!saturnAppLibDir.exists()) { saturnAppLibDir.mkdirs(); List<String> runtimeArtifacts = project.getRuntimeClasspathElements(); for (String path : runtimeArtifacts) { File tmp = new File(path); copy(tmp, saturnAppLibDir); throw new MojoExecutionException("unzip saturn-executor.zip failed", e); File saturnExecutorDir = new File(saturnHomeCaches, "saturn-executor-" + saturnVersion); executorClassLoader = new URLClassLoader( new URL[] { new File(saturnExecutorDir, "saturn-executor.jar").toURL() }, ClassLoader.getSystemClassLoader()); } catch (MalformedURLException e) {
public Class<?> run() throws Exception { try { return ClassLoader.getSystemClassLoader().loadClass(VIRTUAL_MACHINE_CLASSNAME); } catch (ClassNotFoundException cnfe) { for (File jar : getPossibleToolsJars()) { try { Class<?> vmClass = new URLClassLoader(new URL[] {jar.toURL()}).loadClass(VIRTUAL_MACHINE_CLASSNAME); LOGGER.info("Located valid 'tools.jar' at '{}'", jar); return vmClass; } catch (Throwable t) { LOGGER.info("Exception while loading tools.jar from '{}': {}", jar, t); } } throw new ClassNotFoundException(VIRTUAL_MACHINE_CLASSNAME); } } });
/** * Returns the URL of the {@code WEB-INF/classes} folder. * <p> * This finds the URLs using the {@link ServletContext}. * * @return the collection of URLs, not null */ public static URL forWebInfClasses(final ServletContext servletContext) { try { final String path = servletContext.getRealPath("/WEB-INF/classes"); if (path != null) { final File file = new File(path); if (file.exists()) return file.toURL(); } else { return servletContext.getResource("/WEB-INF/classes"); } } catch (MalformedURLException e) { /*fuck off*/ } return null; }
/** * Create a URL from a string representing a path to a local file. * The path string can be just a path, or can start with file:/, file:/// * @param onestr path string * @return */ private static URL urlFromPathString(String onestr) { URL oneurl = null; try { if (StringUtils.indexOf(onestr, "file:/") == 0) { oneurl = new URL(onestr); } else { oneurl = new File(onestr).toURL(); } } catch (Exception err) { LOG.error("Bad URL " + onestr + ", ignoring path"); } return oneurl; }