Getting Resources from a jar: classloader vs class resourceasstream

I am trying to implement a method that when called, get a string from a particular resource in the jar that the class is loaded from.

For example:

import mypath.myclass; //from a jar String result = gitid.getGitId(myclass.class);

On the backed I am currently using:

InputStream is = null; BufferedReader br = null; String line; is = c.getResourceAsStream("/com/file.text");

The problem is I keep getting the same resource for no matter what class I give it.

I have also tried:

is = c.getClassLoader().getResourceAsStream("/com/file.text");

This fails completely.

Any suggestions would be much appreciated.

Also,what is the difference between calling getResourceAsStream from the class loader vs the class?


The Class.getResourceAsStream() gets a ClassLoader instance, pretty much the same you get from Class.getClassLoader() call.

What you could do, is get the URL for a given class and replace class resource path your path of your file. for example, the following code will return resource from the same jar:

Class c = String.class; URL u = c.getResource('/' + c.getName().replace('.', '/') + ".class"); String s = u.toString(); URL url = new URL(s.substring(0, s.indexOf('!')) + "!/META-INF/MANIFEST.MF"); InputStream is = url.openStream();

You'll have to handle not jarred class folders separately.


Probably all classes were loaded by the same ClassLoader instance. So as long as the path of the resource doesn't change you will get the same resource every time.


This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream(java.lang.String).


  • Jasper Reports: How to combine two report streams into one PDF output?
  • getResource() to a file on runtime
  • Java catching exceptions and subclases
  • How to read data from socket connection - android
  • How could I write a BsonDocument object into a file, and read it again, using Java
  • Zend Framework 2, Module Redirect
  • Why does PHP appear to evaluate this condition incorrectly?
  • Runtime error in UVA Online Judge [closed]
  • Contact form problem - I do receive messages, but no contents (blank page)
  • Do I have to inject the service in the unit test if I cal testBed.get() previously?
  • CFBundleDevelopmentRegion not works as expected
  • Is it better to use the “hidden” CSS attribute or fetch each set of new images?
  • How dotnet build chooses the output name
  • Java : How to tint this PNG programmatically?
  • In Java, how can I construct a File from a resource?
  • HttpListener.IsSupported is false on XP SP3
  • Read text file that is not in the main package in a runnable jar
  • jQuery ready not fired after rails link_to is clicked
  • Using $this when not in object context
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • How would I use PHP exceptions to define a redirect?
  • MySQL WHERE-condition in procedure ignored
  • Join two tables and save into third-sql
  • Perl system calls when running as another user using sudo
  • How to model a transition system with SPIN
  • ORA-29908: missing primary invocation for ancillary operator
  • Web-crawler for facebook in python
  • AT Commands to Send SMS not working in Windows 8.1
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • trying to dynamically update Highchart column chart but series undefined
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • Converting MP3 duration time
  • To Get the radio button value in ruby on rails
  • java string with new operator and a literal