Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #21724
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2013-01-25 22:32 -0800 |
| References | <f7a28e33-5e29-45fd-8c15-762e8ee4cdc2@googlegroups.com> <50f885a5$0$286$14726298@news.sunsite.dk> <kdusbn$vid$1@news.albasani.net> <51033811$0$281$14726298@news.sunsite.dk> |
| Message-ID | <5cb10a36-8d56-41e5-b0ab-1d6dfadfe48f@googlegroups.com> (permalink) |
| Subject | Re: resources from JAR files |
| From | Lew <lewbloch@gmail.com> |
Arne Vajhøj wrote: > BGB wrote: >> FWIW, AFIAK: >> it can also be noted that it does matter which class you pick, like That's partly correct, as five minutes' reading of the 'getResource()' method docs will reveal. What actually matters is what classloader you pick; any number of classes (and frequently all the ones you have access to) are loaded by one classloader. As for the seeming arbitrariness of the idiom that started this thread, perhaps the programmer picked a class known to have the correct classloader, is all. >> generally you want the class and resource file to be in the same package That's not true. >> and JAR and similar (the class basically telling the JVM where to look, That's not true either, and that's not true. You can have the resource in any package that makes sense. Common conventions are 'resource', 'resources', 'res', or those relative to the "official" package root of your application, such as 'com.lewscanon.slicedbread.resources'. And as stated, it's "basically" the classloader telling the JVM where to look. >> otherwise the resource may not be found). The resource will be found if it's in the location specified by the argument to the call. That can be the default package, the same package, a different package, or whatever. Same JAR, different JAR, remote URL, whatever. > > so, while a person can pick an arbitrary class, it may not necessarily > > find the resource. You should never pick an arbitrary class, however you may pick an arbitrary class from the set of those that use the appropriate classloader, or even use the classloader directly. It's all good. If you read the Javadocs, you will not guess but know that the call is correct. >> so, generally, picking 'this' or similar makes sense, since normally a Because generally you want the same classloader as the caller's. What is "or similar"? >> person will package the resources along with their own code. Normally by what metric? You put the resources where the architecture of the system mandates. Again, that can be remote - quite common for applets in their day - from a JAR, from anywhere accessible to a classloader. You are correct to the extent that the default classloader is often the one you want, so 'this' or 'Type.class' do just fine. But that's an arbitrary choice. >> or such... False analogy. > I would use "the class" of this, so ... > And the class method can be used in static context. Note: He means "method" in the English sense here, as in "means of getting to the classloader", not as in "the class literal is a Java method". Bear in mind that the 'Class' version of 'getResource()' is a convenience method - the 'ClassLoader' version is the workhorse. Don't use these calls by rule of thumb as BGB suggests. Breaking into 'Class' methods and more so 'ClassLoader' methods is of the world of reflection, and classpaths, and package-to-real-world connections, and stuff that that breaks type safety. This is part of the heart of what makes Java Java. This is stuff you need to actually know, not do by cargo-cult programming. -- Lew
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread
resources from JAR files bob smith <bob@coolfone.comze.com> - 2013-01-17 08:30 -0800
Re: resources from JAR files markspace <markspace@nospam.nospam> - 2013-01-17 08:54 -0800
Re: resources from JAR files Roedy Green <see_website@mindprod.com.invalid> - 2013-01-17 15:07 -0800
Re: resources from JAR files Lew <lewbloch@gmail.com> - 2013-01-17 15:16 -0800
Re: resources from JAR files Roedy Green <see_website@mindprod.com.invalid> - 2013-01-17 16:36 -0800
Re: resources from JAR files Arne Vajhøj <arne@vajhoej.dk> - 2013-01-17 19:44 -0500
Re: resources from JAR files Arne Vajhøj <arne@vajhoej.dk> - 2013-01-17 18:13 -0500
Re: resources from JAR files BGB <cr88192@hotmail.com> - 2013-01-25 15:08 -0600
Re: resources from JAR files Arne Vajhøj <arne@vajhoej.dk> - 2013-01-25 20:57 -0500
Re: resources from JAR files Lew <lewbloch@gmail.com> - 2013-01-25 22:32 -0800
csiph-web