/** * Finds a system resource (which should be loaded from the parent * classloader). * * @param name The name of the system resource to load. * Must not be <code>null</code>. * * @return a stream to the named resource, or <code>null</code> if * the resource cannot be found. */ private InputStream loadBaseResource(String name) { return parent == null ? super.getResourceAsStream(name) : parent.getResourceAsStream(name); }
@Override @Nullable public InputStream getResourceAsStream(String name) { return this.enclosingClassLoader.getResourceAsStream(name); }
// From ClassLoader, all paths are "absolute" already - there's no context // from which they could be relative. Therefore you don't need a leading slash. InputStream in = this.getClass().getClassLoader() .getResourceAsStream("SomeTextFile.txt"); // From Class, the path is relative to the package of the class unless // you include a leading slash, so if you don't want to use the current // package, include a slash like this: InputStream in = this.getClass().getResourceAsStream("/SomeTextFile.txt");
package dummy; import java.io.*; public class Test { public static void main(String[] args) { InputStream stream = Test.class.getResourceAsStream("/SomeTextFile.txt"); System.out.println(stream != null); stream = Test.class.getClassLoader().getResourceAsStream("SomeTextFile.txt"); System.out.println(stream != null); } }
ClassLoader classLoader = getClass().getClassLoader(); InputStream input = classLoader.getResourceAsStream("/com/example/foo.properties"); // ...
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); InputStream input = classLoader.getResourceAsStream("com/example/foo.properties"); // ...
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); InputStream input = classLoader.getResourceAsStream("foo.properties"); // ... Properties properties = new Properties(); properties.load(input);
private InputStream getFileAsStream(String filePath) throws FileNotFoundException { InputStream inStream = null; File file = new File(filePath); if (file.exists()) { inStream = new FileInputStream(file); } else { inStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath); } return inStream; } }
/** * Open an InputStream for the specified class. * <p>The default implementation loads a standard class file through * the parent ClassLoader's {@code getResourceAsStream} method. * @param name the name of the class * @return the InputStream containing the byte code for the specified class */ @Nullable protected InputStream openStreamForClass(String name) { String internalName = name.replace('.', '/') + CLASS_FILE_SUFFIX; return getParent().getResourceAsStream(internalName); }
public static void loadConfig() { try (InputStream is = JULLogDelegateFactory.class.getClassLoader().getResourceAsStream("vertx-default-jul-logging.properties")) { if (is != null) { LogManager.getLogManager().readConfiguration(is); } } catch (IOException ignore) { } }
@Override public InputStream getResourceAsStream(final String name) { if (name.endsWith(ClassUtils.CLASS_EXTENSION)) { String qualifiedClassName = name.substring(0, name.length() - ClassUtils.CLASS_EXTENSION.length()).replace('/', '.'); JavaFileObjectImpl file = (JavaFileObjectImpl) classes.get(qualifiedClassName); if (file != null) { return new ByteArrayInputStream(file.getByteCode()); } } return super.getResourceAsStream(name); } }
@Override public InputStream getResourceAsStream(final String name) { if (name.endsWith(ClassUtils.CLASS_EXTENSION)) { String qualifiedClassName = name.substring(0, name.length() - ClassUtils.CLASS_EXTENSION.length()).replace('/', '.'); JavaFileObjectImpl file = (JavaFileObjectImpl) classes.get(qualifiedClassName); if (file != null) { return new ByteArrayInputStream(file.getByteCode()); } } return super.getResourceAsStream(name); } }
public String getVersion() { try (InputStream is = getClass().getClassLoader().getResourceAsStream("META-INF/vertx/vertx-version.txt")) { if (is == null) { throw new IllegalStateException("Cannot find vertx-version.txt on classpath"); } try (Scanner scanner = new Scanner(is, "UTF-8").useDelimiter("\\A")) { return scanner.hasNext() ? scanner.next() : ""; } } catch (IOException e) { throw new IllegalStateException(e.getMessage()); } }
@Override public void initialize() { try { InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(APP_PROPERTIES_CLASSPATH); if (in == null) { in = DefaultApplicationProvider.class.getResourceAsStream(APP_PROPERTIES_CLASSPATH); } initialize(in); } catch (Throwable ex) { logger.error("Initialize DefaultApplicationProvider failed.", ex); } }
/** * Create a new {@link WebSphereClassPreDefinePlugin}. * @param transformer the {@link ClassFileTransformer} to be adapted * (must not be {@code null}) */ public WebSphereClassPreDefinePlugin(ClassFileTransformer transformer) { this.transformer = transformer; ClassLoader classLoader = transformer.getClass().getClassLoader(); // First force the full class loading of the weaver by invoking transformation on a dummy class try { String dummyClass = Dummy.class.getName().replace('.', '/'); byte[] bytes = FileCopyUtils.copyToByteArray(classLoader.getResourceAsStream(dummyClass + ".class")); transformer.transform(classLoader, dummyClass, null, null, bytes); } catch (Throwable ex) { throw new IllegalArgumentException("Cannot load transformer", ex); } }
public static byte[] readByteArrayFromResource(String resource) throws IOException { InputStream in = null; try { in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource); if (in == null) { return null; } return readByteArray(in); } finally { JdbcUtils.close(in); } }
public static Object deserialize(String fileName) throws IOException, ClassNotFoundException { ClassLoader classLoader = Serializer.class.getClassLoader(); InputStream fileStream = classLoader.getResourceAsStream(fileName); return deserialize(fileStream); } }
private void testGetResourceAsStream(long ver, ClassLoader cl) throws Exception { try (InputStream is = cl.getResourceAsStream(resourceName)) { assertNotNull(is); try (Scanner scanner = new Scanner(is, "UTF-8").useDelimiter("\\A")) { assertTrue(scanner.hasNext()); JsonObject json = new JsonObject(scanner.next()); assertEquals(ver, json.getLong("ver", -1L).longValue()); } } }
@Test public void testFindsExistingResourceWithGetResourceAsStreamAndNoOverrides() { assertNotNull(thisClassLoader.getResourceAsStream(EXISTING_RESOURCE)); assertNotNull(overridingLoader.getResourceAsStream(EXISTING_RESOURCE)); }
@Test public void testDoesNotFindExistingResourceWithGetResourceAsStreamAndNullOverride() { assertNotNull(thisClassLoader.getResourceAsStream(EXISTING_RESOURCE)); overridingLoader.override(EXISTING_RESOURCE, null); assertNull(overridingLoader.getResourceAsStream(EXISTING_RESOURCE)); }