Tabnine Logo
ClassLoader.getResourceAsStream
Code IndexAdd Tabnine to your IDE (free)

How to use
getResourceAsStream
method
in
java.lang.ClassLoader

Best Java code snippets using java.lang.ClassLoader.getResourceAsStream (Showing top 20 results out of 43,866)

origin: jenkinsci/jenkins

/**
 * 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);
}
origin: spring-projects/spring-framework

@Override
@Nullable
public InputStream getResourceAsStream(String name) {
  return this.enclosingClassLoader.getResourceAsStream(name);
}
origin: stackoverflow.com

 // 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");
origin: stackoverflow.com

 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);
  }
}
origin: stackoverflow.com

 ClassLoader classLoader = getClass().getClassLoader();
InputStream input = classLoader.getResourceAsStream("/com/example/foo.properties");
// ...
origin: stackoverflow.com

 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("com/example/foo.properties");
// ...
origin: stackoverflow.com

 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("foo.properties");
// ...
Properties properties = new Properties();
properties.load(input);
origin: alibaba/druid

  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;
  }
}
origin: spring-projects/spring-framework

/**
 * 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);
}
origin: eclipse-vertx/vert.x

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) {
 }
}
origin: apache/incubator-dubbo

  @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);
  }
}
origin: apache/incubator-dubbo

  @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);
  }
}
origin: eclipse-vertx/vert.x

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());
 }
}
origin: ctripcorp/apollo

@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);
 }
}
origin: spring-projects/spring-framework

/**
 * 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);
  }
}
origin: alibaba/druid

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);
  }
}
origin: apache/kafka

  public static Object deserialize(String fileName) throws IOException, ClassNotFoundException {
    ClassLoader classLoader = Serializer.class.getClassLoader();
    InputStream fileStream = classLoader.getResourceAsStream(fileName);
    return deserialize(fileStream);
  }
}
origin: eclipse-vertx/vert.x

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());
  }
 }
}
origin: spring-projects/spring-framework

@Test
public void testFindsExistingResourceWithGetResourceAsStreamAndNoOverrides() {
  assertNotNull(thisClassLoader.getResourceAsStream(EXISTING_RESOURCE));
  assertNotNull(overridingLoader.getResourceAsStream(EXISTING_RESOURCE));
}
origin: spring-projects/spring-framework

@Test
public void testDoesNotFindExistingResourceWithGetResourceAsStreamAndNullOverride() {
  assertNotNull(thisClassLoader.getResourceAsStream(EXISTING_RESOURCE));
  overridingLoader.override(EXISTING_RESOURCE, null);
  assertNull(overridingLoader.getResourceAsStream(EXISTING_RESOURCE));
}
java.langClassLoadergetResourceAsStream

Javadoc

Returns a stream for the resource with the specified name. See #getResource(String) for a description of the lookup algorithm used to find the resource.

Popular methods of ClassLoader

  • loadClass
    Loads the class with the specified name, optionally linking it after loading. The following steps ar
  • getResource
    Returns the URL of the resource with the specified name. This implementation first tries to use the
  • getResources
    Returns an enumeration of URLs for the resource with the specified name. This implementation first u
  • getSystemClassLoader
    Returns the system class loader. This is the parent for new ClassLoader instances and is typically t
  • getParent
    Returns this class loader's parent.
  • getSystemResourceAsStream
    Returns a stream for the resource with the specified name. The system class loader's resource lookup
  • getSystemResource
    Finds the URL of the resource with the specified name. The system class loader's resource lookup alg
  • getSystemResources
    Returns an enumeration of URLs for the resource with the specified name. The system class loader's r
  • findClass
  • defineClass
    Constructs a new class from an array of bytes containing a class definition in class file format.
  • findResource
  • findResources
    Finds an enumeration of URLs for the resource with the specified name. This implementation just retu
  • findResource,
  • findResources,
  • getPackage,
  • setPackageAssertionStatus,
  • setDefaultAssertionStatus,
  • getPackages,
  • setClassAssertionStatus,
  • clearAssertionStatus,
  • resolveClass

Popular in Java

  • Parsing JSON documents to java classes using gson
  • requestLocationUpdates (LocationManager)
  • getSystemService (Context)
  • getApplicationContext (Context)
  • Window (java.awt)
    A Window object is a top-level window with no borders and no menubar. The default layout for a windo
  • Date (java.sql)
    A class which can consume and produce dates in SQL Date format. Dates are represented in SQL as yyyy
  • Comparator (java.util)
    A Comparator is used to compare two objects to determine their ordering with respect to each other.
  • Deque (java.util)
    A linear collection that supports element insertion and removal at both ends. The name deque is shor
  • TimeUnit (java.util.concurrent)
    A TimeUnit represents time durations at a given unit of granularity and provides utility methods to
  • ImageIO (javax.imageio)
  • Top 12 Jupyter Notebook Extensions
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimAtomGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyStudentsTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now