• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

getResource returning null, revisited

 
Ranch Hand
Posts: 137
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's been years since I've had trouble with the getResource method, and the code below has been working for a couple of years. But for some unknown reason the code started returning nulls.

What has changed?
Previously, to compile for distribution, I would copy source to a directory and compile and use jlink via the CLI, using Java 11, due to not figuring out how to make a proper Jar with a modular program from within Eclipse. That version works fine. (And, the source, in Eclipse, runs fine.)

Yesterday, I started a new project in Eclipse and set it to use Java 8. Then I copied the source code from the Java 11 version into the Java 8 project. This code also runs fine in Eclipse. Then I exported the Java 8 code to a .jar and tried running it, but got an IOException due to a null URL.

Here is a code fragment. Some diagnostics were added for posting here at CodeRanch.



The idea of getting the various urls came from another coderanch post from 4 years ago. Following is the output from running in Eclipse.


Following is the output from my Windows Command Prompt. I was careful about running the code with the exact same Java version that is used in Eclipse for that project.



I get this output from both a Windows 10 and an Ubuntu 18.04 desktop (two different computers). I looked at the code in JEditorPane(line 415), and the value of the URL is null which throws the IOException. The code "textArea.setPage(ntroURL);" is AboutPanel's line 40 cited in the stack trace.

This isn't a case of the getResource command failing because it cannot find a resource because the path is incorrect, as far as I can tell. The relative path of "." (when assigning helpUrl) should eliminate that idea, yes?

Any thoughts on how to proceed?
 
Phil Freihofner
Ranch Hand
Posts: 137
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem goes away if I move my resources to a subfolder and no longer use ".." as part of the address.

I thought we could use ".." for relative addressing?

Was this at one time possible?
 
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Phil Freihofner wrote:The problem goes away if I move my resources to a subfolder and no longer use ".." as part of the address.

I thought we could use ".." for relative addressing?

Was this at one time possible?



Relative addressing has always been possible. However if you choose to use it, you run the risk of it not working if the current working directory changes, or if the location of the resource relative to the current working directory is changed. It looks like the new version of Eclipse may have sprung the latter gotcha on you.
 
Phil Freihofner
Ranch Hand
Posts: 137
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took a look at the API for Class.getResource() from Java 6, and it seems the same rules were in use then as they are now. The description refers to "." being changed to "/".  So maybe "../res" becomes "///res"? But the Eclipse IDE instead handles the "../" in the standard HTML way?

Of course, the compiler doesn't know if the address being specified is going to be called from in a file system context or a jar context. But maybe the Eclipse IDE should enforce that conversion rather than allowing the more lenient "file system" rules to apply, if that is what is happening?

I'm thinking it must be a false memory on my part that I had used "../" with jars in the past.

Relative addressing should be safe for code packed and run as a jar, I would think.
 
Ranch Hand
Posts: 127
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did some digging into a src.zip from a jdk8. The resolving of the "path" (or url rather) comes down to some nested class within some sun.misc class which tries to URL.openConnection().getInputStream() and returns null upon IOException. As a directory can't have an InputStream my best guess is that at some internal C code of the VM itself some syscall to something like openFile(".") is made - this fails - causes a ripple of exceptions to bubble back up and ends in the try-catch to return null when the give String points to a directory. This at least explains why "." and ".." return null. As for why the last call returns null: Somewhere in the stack a replace(".","/") is done so "../res/file" becomes "///res/file" which in the end results in a wrong absolute path which causes the aforementioned stack to return null again.
So, my guess is: Class.getResource() isn't really meant to be able to get the current or even the parent directory but to only proceed further down from the current one, at least when try to use relative paths instead of absolute ones.
 
See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic