-2

I have the following line of code:

InputStreamReader isr = new InputStreamReader(MethodHandles.lookup().lookupClass().getResourceAsStream(csvFile));

could someone explain to a newbe:

MethodHandles.lookup()
lookupClass()
getResourceasStream()

the code works and accesses a csv file located in a jar. I just don't understand what each of the methods is doing

I was able to simplify the line to:

InputStreamReader isr = new InputStreamReader (SQLUtilPROD.class.getResourceAsStream (csvFile));

but still confused. What does SQLUtilProd.class do? and how does getResourceAsStream know to get the file from the jar? What happens if you have multiple jars?

Not sure but I think we use SQLUtil.class to get the class object which in turns gives us access to classLoader which getResourceAsStream uses to locate the file.

If that's true where does classLoader define the path to include the jar?

DCR
  • 14,737
  • 12
  • 52
  • 115

1 Answers1

1

What does SQLUtilProd.class do?

The data for each object contains a reference to an object of class java.lang.Class, and this is returned by the method getClass. There is also one java.lang.Class object describing java.lang.Class. Please read more in this link.

how does getResourceAsStream know to get the file from the jar?

getResourceAsStream will find a resource on the classpath with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. Please visit this link.

What happens if you have multiple jars?

You can get all resources with a helper function like this:

public static List<InputStream> loadResources(
        final String name, final ClassLoader classLoader) throws IOException {
    final List<InputStream> list = new ArrayList<InputStream>();
    final Enumeration<URL> systemResources = 
            (classLoader == null ? ClassLoader.getSystemClassLoader() : classLoader)
            .getResources(name);
    while (systemResources.hasMoreElements()) {
        list.add(systemResources.nextElement().openStream());
    }
    return list;
}

I'm sure that reading this link will help you.

where does classLoader define the path to include the jar?

The system ClassLoader provides access to information in the CLASSPATH. A CLASSPATH may contain directories and JAR files. So you have access to all resources of your project and jar files which they are in the classpath.

Mohsen
  • 4,536
  • 2
  • 27
  • 49
  • 1
    Please inline enough information from the links that your answer can stand on its own even if the linked pages - for any reason - is inaccessible. A good test is reading a printed out version. – Thorbjørn Ravn Andersen Dec 24 '18 at 22:01
  • 1
    @ThorbjørnRavnAndersen Thanks for your advice. I will try to do that. – Mohsen Dec 24 '18 at 22:02
  • 1
    @Spara thanks for you help but I had to down vote your answer. I already read the docs, they are poorly written and don't help me understand the methods. See revised question – DCR Dec 25 '18 at 00:43
  • 3
    @DCR If you read the documentation and didn't mention that (even if you didn't understand it) in your question you cannot blame others for thinking you didn't know they exist as you are a self-admitted newbie. I would advise against downvoting answers. – Thorbjørn Ravn Andersen Dec 25 '18 at 01:07
  • You don’t need to implement your own copy-`Enumeration`-to-`List` routine. Just use [`list = Collections.list(systemResources)`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collections.html#list(java.util.Enumeration)). – Holger Jan 31 '19 at 16:44