• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help me understand a ClassLoader a little better  RSS feed

 
Jon Swanson
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If someone is feeling kind today, would you mind looking through my logic and seeing if I came to the right conclusion about why this code was not working?

Here is the method I was trying to call-

loadKeyRingFromResource
public void loadKeyRingFromResource(java.lang.String resourceName,
byte[] digest)
throws java.lang.ExceptionLoad a key ring from a resource file (a file that is packaged into the JAR file).

In my jar file, I have a keys sub-directory and the file pubring.gpg.

So I was thinking I could do this:



but I get-

java.lang.NullPointerException
at com.verhas.licensor.License.loadKeyRing(License.java:281)
at com.verhas.licensor.License.loadKeyRingFromResource(License.java:230)
at freethink.license.TestLicense.checkLicense(TestLicense.java:40)
at freethink.license.TestLicense.main(TestLicense.java:66)

line 40 is actually line 2 above.

Step 1. See if I am getting the resource myself. So I changed the code to this:



and I get:

jar:file:/C:/Users/ChemModeling/Documents/FreeThink/Java/InterfaceTests/license/License/classes/TestLicense.jar!/keys/pubring.gpg
java.lang.NullPointerException
at com.verhas.licensor.License.loadKeyRing(License.java:281)
at com.verhas.licensor.License.loadKeyRingFromResource(License.java:230)
at freethink.license.TestLicense.checkLicense(TestLicense.java:40)
at freethink.license.TestLicense.main(TestLicense.java:66)

I have access to the source-

line 230


line 281 is the start of the while loop


step 2. More testing on my end-



and I get

jar:file:/C:/Users/ChemModeling/Documents/FreeThink/Java/InterfaceTests/license/License/classes/TestLicense.jar!/keys/pubring.gpg
15321348071131121160188591474257483011239201411523850173190117602361511941125224
...
241169133246228176203
java.lang.NullPointerException
at com.verhas.licensor.License.loadKeyRing(License.java:281)
at com.verhas.licensor.License.loadKeyRingFromResource(License.java:230)

at freethink.license.TestLicense.checkLicense(TestLicense.java:52)
at freethink.license.TestLicense.main(TestLicense.java:80)

The original code used "License.class.getClassLoader()" while I used "getClass()". One useful bit I found on StackOverflow-

•When you use .getClass().getResource(fileName) it considers the location of the fileName is the same location of the of the calling class.
•When you use .getClass().getClassLoader().getResource(fileName) it considers the location of the fileName is the root
but I think I made that distinction irrelevant by using "/" at the start of my file names.

Is the problem that by hard-coding License.class, that Java is looking in the jar file of the License class for the resource in the first case, whereas when I use getClass, it is looking in the jar file of TestLicense, which is where the files actually are?

In any case, I called loadKeyRing with my InputStream and that worked. I'm just wondering if I understand why loadKeyRingFromResource didn't work.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has that got anything to do with class loaders?
Check that the key ring and other resources actually exist in the .jar, and also that you are using their correct name. Does your name include a path? Does that path match the structure of the .jar? If the resourec doesn’t exist, you are liable to get null values. Have a look at the getResource() method’s documentation.
 
Jon Swanson
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I did check that.

If the statements:



work, doesn't that cover the case that the key ring does exist in the jar file?

and don't the statements:



indicate that the file can be accessed?

So if this statement fails:



and I look in the source code for what this method does and find the line that fails is:



but I can run the command



or



and they work, doesn't it come down to understanding more about ClassLoader() and how that affects where Java looks for the resources? All the examples I see use getClass().getClassLoader().getResource(). But what I think is true is that when I am using that command, I am searching just the jar file containing the class I am calling it from, whereas the command License.class.getClassLoader() is only looking in the jar file that contains the License class. Which seems like a bit of a mistake, as why would I put my keyring in their jar file? Which made me wonder if I was understanding where getResource searched when you had jar files.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!