The difference in the two code sniplets is whom you are asking to load the
resource. In the first example, you are asking a Class object to load the
resource, in the second you are asking a ClassLoader to load the resource.
There is a subtle difference.
All resources, like classes, are part of a package. Also like classes, the
package 'path' is the same as the directory path in which the resource is
located. That package must be specified when you are loading the resource.
When using the getResource method of the Class class, it will check to see
if a package has been specified for your resource ( it looks for '/'
atChar(0) ). If the package has not been specified, then it will prepend
its own package name to the resource name before calling getResource in the
Class' ClassLoader.
In your second example, calling getResource on ClassLoader, you need to
provide the fully specified path to the resource. If you are not sure what
that is, look at the fully specified package path of the class that
contained your first example below and convert all '.' to '/' and the path
must begin with a '/'. For Example:
"/com/acme/myserver.conf"
Cheers
Chris
-----Original Message-----
From: Wes Mckean [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, October 03, 2000 9:16 AM
To: 'jBoss'
Subject: RE: [jBoss-User] Stupid Question
Until I am a Java guru, I must remain modest :)
My server has no problem loading the resource when I do the code you gave as
an example:
URL mletURL = getClass.getResource( "myserver.conf" );
In my server, and in jBoss' startup code. It adds an MLet class to the
server this way, then sets the class loader:
MLet mlet = new MLet();
server.registerMBean( mlet, new ObjectName( server.getDefaultDomain(),
"service", "MLet"));
Thread.currentThread().setContextClassLoader(mlet);
It then calls:
URL mletURL = getClass().getClassLoader().getResource( "myserver.conf" );
This doesn't work for me. I don't understand where the configuration file
should be loaded, and more importantly, I don't understand how jBoss finds
its configuration in the jBoss\conf directory using this bit of code.
<sigh>
Perhaps a pilgramage to Mount Telkel is in order :) Thanks for helping.
Wes
-----Original Message-----
From: Tom Cook [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, October 03, 2000 1:16 AM
To: jBoss
Subject: [jBoss-User] Stupid Question
There are no stupid questions, just stupid people...
Wes Mckean writes:
> Ok, so this isn't exactly a jBoss question, but...
>
> I am patterning a JMX server on jBoss' architecture, and I can't get my
> server to find the MLet configuration file when I do the following:
>
> URL mletURL =
> getClass().getClassLoader().getResource("myserver.conf");
>
> It does work with:
>
> URL mletURL = getClass().getResource("myserver.conf");
>
> Any insight is appreciated.
>From the API doc for java.lang.Class.getResource:
This method delegates the call to its class loader, after making these
changes to the resource name: if the resource name starts with "/", it
is unchanged; otherwise, the package name is prepended to the resource
name after converting "." to "/". If this object was loaded by the
bootstrap loader, the call is delegated to ClassLoader.getSystemResource.
So
getClass.getResource( "myserver.conf" );
is equivalent to
getClass.getResource( "com.myorg.myproject.myserver.conf" );
Tom
P.S. The stupid people are the ones who don't ask questions.
--
Tom Cook - Software Engineer
"We rarely find that people have good sense unless they agree
with us."
- Francois, Duc de la Rochefoucauld
LISAsoft Pty Ltd - www.lisa.com.au
--------------------------------------------------
38 Greenhill Rd. Level 3, 228 Pitt Street
Wayville, SA, 5034 Sydney, NSW, 2000
Phone: +61 8 8272 1555 Phone: +61 2 9283 0877
Fax: +61 8 8271 1199 Fax: +61 2 9283 0866
--------------------------------------------------
--
--------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Problems?: [EMAIL PROTECTED]
--
--------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Problems?: [EMAIL PROTECTED]
--
--------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Problems?: [EMAIL PROTECTED]