This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

class.getResource(.) ... why?  RSS feed

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In some web service code, I am seeing the following:




What does the getResource() thing do? It returns the directory of the SnapshotService class as a URI... But why? As far as I can tell, the URL-as-a-String is always used.
 
Sheriff
Posts: 23509
47
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It searches the classpath and produces a URL which points to the actual location of the SnapshotService class. For example if the class is deployed in a jar then it's going to be a file://...jar:... kind of URL. You could do some debugging to see the actual value if you were interested.
 
Tony Ennis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did. the getResource() returned the directory the class lived in as a 'file' type URI. I suppose what I am really asking is, "Why would you want to?"
 
author
Sheriff
Posts: 23510
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case, I am not sure if it actually does anything. That constructor is for defining a relative url, from a base url. But the specified url is fully qualified -- unless there is some option that I don't know about, I am not sure if it does anything.

Henry
 
Paul Clapham
Sheriff
Posts: 23509
47
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what that constructor does either. And that's after I read the API documentation for it, which just made my eyes bleed. It might be interesting to see what URL was the result.
 
Tony Ennis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah! But I specified the web service URL on the input of the tool that generated that snippet. So perhaps if had specified
qqq.wsdl then that code would look for the wsdl in the same directory as the class. That could be useful when I'm trying
to generate the service code for the first time or from scratch.

 
Paul Clapham
Sheriff
Posts: 23509
47
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tony Ennis wrote:So perhaps if had specified qqq.wsdl then that code would look for the wsdl in the same directory as the class.



Yeah, that's probably what it would do. Or more precisely it would produce a URL referring to the WSDL under the assumption that it was in the same directory as the class.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!