You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by laredotornado <la...@gmail.com> on 2011/07/21 18:37:30 UTC
Trouble finding velocity.properties in a jar
Hi,
I'm using Java 1.6, Velocity 1.6.2. I have a Maven JAR project, in which
the "velocity.properties" gets packaged at the root of the JAR. When I
include this JAR in other projects, I'm not able to initialize velocity,
using ...
VelocityEngine ve = new VelocityEngine();
final InputStream resourceStream =
Thread.currentThread().getContextClassLoader().getResourceAsStream("velocity.properties");
final Properties velocityProps = new Properties();
velocityProps.load(resourceStream);
ve.init( velocityProps );
The above dies because the "resourceStream" is null. Any ideas how I
reference this file? Keep in mind that it is packaged within the JAR.
Thanks, - Dave
--
View this message in context: http://old.nabble.com/Trouble-finding-velocity.properties-in-a-jar-tp32108932p32108932.html
Sent from the Velocity - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Trouble finding velocity.properties in a jar
Posted by laredotornado <la...@gmail.com>.
Accessing the properties in another way worked ...
final InputStream resourceStream =
this.getClass().getClassLoader().getResourceAsStream("velocity.properties");
Thanks, - Dave
Nathan Bubna wrote:
>
> I would do two things (the first of which, you have probably done).
> 1) double-check that the build process for the JAR is correctly
> putting the velocity.properties file into the root of the jar and that
> the jar is in the classpath 2) try finding it elsewhere in the
> classloader heirarchy:
>
> InputStream stream =
> Thread.currentThread().getContextClassLoader().getResourceAsStream("velocity.properties");
> if (stream == null) {
> stream =
> ThisClass.class.getClassLoader().getResourceAsStream("velocity.properties");
> if (stream == null) {
> stream = ThisClass.class.getResourceAsStream("velocity.properties");
> if (stream == null) {
> throw new RuntimeException("WTF? i'm not sure the resource
> is really there.");
> }
> }
> }
>
> On Thu, Jul 21, 2011 at 9:37 AM, laredotornado <la...@gmail.com>
> wrote:
>>
>> Hi,
>>
>> I'm using Java 1.6, Velocity 1.6.2. I have a Maven JAR project, in which
>> the "velocity.properties" gets packaged at the root of the JAR. When I
>> include this JAR in other projects, I'm not able to initialize velocity,
>> using ...
>>
>> VelocityEngine ve = new VelocityEngine();
>> final InputStream resourceStream =
>> Thread.currentThread().getContextClassLoader().getResourceAsStream("velocity.properties");
>> final Properties velocityProps = new Properties();
>> velocityProps.load(resourceStream);
>> ve.init( velocityProps );
>>
>> The above dies because the "resourceStream" is null. Any ideas how I
>> reference this file? Keep in mind that it is packaged within the JAR.
>> Thanks, - Dave
>> --
>> View this message in context:
>> http://old.nabble.com/Trouble-finding-velocity.properties-in-a-jar-tp32108932p32108932.html
>> Sent from the Velocity - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
>
--
View this message in context: http://old.nabble.com/Trouble-finding-velocity.properties-in-a-jar-tp32108932p32115227.html
Sent from the Velocity - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Trouble finding velocity.properties in a jar
Posted by Nathan Bubna <nb...@gmail.com>.
I would do two things (the first of which, you have probably done).
1) double-check that the build process for the JAR is correctly
putting the velocity.properties file into the root of the jar and that
the jar is in the classpath 2) try finding it elsewhere in the
classloader heirarchy:
InputStream stream =
Thread.currentThread().getContextClassLoader().getResourceAsStream("velocity.properties");
if (stream == null) {
stream = ThisClass.class.getClassLoader().getResourceAsStream("velocity.properties");
if (stream == null) {
stream = ThisClass.class.getResourceAsStream("velocity.properties");
if (stream == null) {
throw new RuntimeException("WTF? i'm not sure the resource
is really there.");
}
}
}
On Thu, Jul 21, 2011 at 9:37 AM, laredotornado <la...@gmail.com> wrote:
>
> Hi,
>
> I'm using Java 1.6, Velocity 1.6.2. I have a Maven JAR project, in which
> the "velocity.properties" gets packaged at the root of the JAR. When I
> include this JAR in other projects, I'm not able to initialize velocity,
> using ...
>
> VelocityEngine ve = new VelocityEngine();
> final InputStream resourceStream =
> Thread.currentThread().getContextClassLoader().getResourceAsStream("velocity.properties");
> final Properties velocityProps = new Properties();
> velocityProps.load(resourceStream);
> ve.init( velocityProps );
>
> The above dies because the "resourceStream" is null. Any ideas how I
> reference this file? Keep in mind that it is packaged within the JAR.
> Thanks, - Dave
> --
> View this message in context: http://old.nabble.com/Trouble-finding-velocity.properties-in-a-jar-tp32108932p32108932.html
> Sent from the Velocity - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Trouble finding velocity.properties in a jar
Posted by Nathan Bubna <nb...@gmail.com>.
On Thu, Jul 21, 2011 at 11:04 AM, Christopher Schultz
<ch...@christopherschultz.net> wrote:
> Nathan,
>
> On 7/21/2011 1:36 PM, Nathan Bubna wrote:
>> On Thu, Jul 21, 2011 at 10:31 AM, Christopher Schultz
>> <ch...@christopherschultz.net> wrote:
>>> Dave,
>>>
>>> On 7/21/2011 12:37 PM, laredotornado wrote:
>>>> final InputStream resourceStream =
>>>> Thread.currentThread().getContextClassLoader().getResourceAsStream("velocity.properties");
>>>
>>> Should that have a leading "/" in the path?
>>>
>>>> The above dies because the "resourceStream" is null. Any ideas how I
>>>> reference this file? Keep in mind that it is packaged within the JAR.
>>>
>>> If you have an exotic ClassLoader hierarchy, you may be getting the
>>> wrong ClassLoader -- one that isn't expecting to load yourjar.jar or
>>> have one of it's parent ClassLoaders do it.
>>>
>>> Nathan's suggestion to try CurrentClass.class.getResourceAsStream after
>>> CurrentClass.class.getClassLoader.getResourceAsStream just makes your
>>> code harder to read and doesn't add anything: Class.getResourceAsStream
>>> already delegates to Class.getClassLoader.getResourceAsStream.
>>
>> i've wondered about that, but never bothered to investigate.
>
> I just read the Javadoc, not the source.
>
>> i
>> cribbed that code from Tools' ClassUtils class, which i derived from
>> somebody else's apparently redundant code. wanna change ClassUtils to
>> remove the redundancy? :)
>
> Sure, why not. I've had more action today in the velocity repo than my
> previous 5 years as a committer.
:)
> The diffs between 2.0 and 2.0.x are pretty thin, but 2.0 went out on
> 2010-05-10. Maybe time to think about 2.0.1?
sure. i'd be willing to walk you through it and help where needed. :)
> -chris
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Trouble finding velocity.properties in a jar
Posted by Christopher Schultz <ch...@christopherschultz.net>.
Nathan,
On 7/21/2011 1:36 PM, Nathan Bubna wrote:
> On Thu, Jul 21, 2011 at 10:31 AM, Christopher Schultz
> <ch...@christopherschultz.net> wrote:
>> Dave,
>>
>> On 7/21/2011 12:37 PM, laredotornado wrote:
>>> final InputStream resourceStream =
>>> Thread.currentThread().getContextClassLoader().getResourceAsStream("velocity.properties");
>>
>> Should that have a leading "/" in the path?
>>
>>> The above dies because the "resourceStream" is null. Any ideas how I
>>> reference this file? Keep in mind that it is packaged within the JAR.
>>
>> If you have an exotic ClassLoader hierarchy, you may be getting the
>> wrong ClassLoader -- one that isn't expecting to load yourjar.jar or
>> have one of it's parent ClassLoaders do it.
>>
>> Nathan's suggestion to try CurrentClass.class.getResourceAsStream after
>> CurrentClass.class.getClassLoader.getResourceAsStream just makes your
>> code harder to read and doesn't add anything: Class.getResourceAsStream
>> already delegates to Class.getClassLoader.getResourceAsStream.
>
> i've wondered about that, but never bothered to investigate.
I just read the Javadoc, not the source.
> i
> cribbed that code from Tools' ClassUtils class, which i derived from
> somebody else's apparently redundant code. wanna change ClassUtils to
> remove the redundancy? :)
Sure, why not. I've had more action today in the velocity repo than my
previous 5 years as a committer.
The diffs between 2.0 and 2.0.x are pretty thin, but 2.0 went out on
2010-05-10. Maybe time to think about 2.0.1?
-chris
Re: Trouble finding velocity.properties in a jar
Posted by Nathan Bubna <nb...@gmail.com>.
On Thu, Jul 21, 2011 at 10:31 AM, Christopher Schultz
<ch...@christopherschultz.net> wrote:
> Dave,
>
> On 7/21/2011 12:37 PM, laredotornado wrote:
>> final InputStream resourceStream =
>> Thread.currentThread().getContextClassLoader().getResourceAsStream("velocity.properties");
>
> Should that have a leading "/" in the path?
>
>> The above dies because the "resourceStream" is null. Any ideas how I
>> reference this file? Keep in mind that it is packaged within the JAR.
>
> If you have an exotic ClassLoader hierarchy, you may be getting the
> wrong ClassLoader -- one that isn't expecting to load yourjar.jar or
> have one of it's parent ClassLoaders do it.
>
> Nathan's suggestion to try CurrentClass.class.getResourceAsStream after
> CurrentClass.class.getClassLoader.getResourceAsStream just makes your
> code harder to read and doesn't add anything: Class.getResourceAsStream
> already delegates to Class.getClassLoader.getResourceAsStream.
i've wondered about that, but never bothered to investigate. i
cribbed that code from Tools' ClassUtils class, which i derived from
somebody else's apparently redundant code. wanna change ClassUtils to
remove the redundancy? :)
> -chris
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Trouble finding velocity.properties in a jar
Posted by Christopher Schultz <ch...@christopherschultz.net>.
Dave,
On 7/21/2011 12:37 PM, laredotornado wrote:
> final InputStream resourceStream =
> Thread.currentThread().getContextClassLoader().getResourceAsStream("velocity.properties");
Should that have a leading "/" in the path?
> The above dies because the "resourceStream" is null. Any ideas how I
> reference this file? Keep in mind that it is packaged within the JAR.
If you have an exotic ClassLoader hierarchy, you may be getting the
wrong ClassLoader -- one that isn't expecting to load yourjar.jar or
have one of it's parent ClassLoaders do it.
Nathan's suggestion to try CurrentClass.class.getResourceAsStream after
CurrentClass.class.getClassLoader.getResourceAsStream just makes your
code harder to read and doesn't add anything: Class.getResourceAsStream
already delegates to Class.getClassLoader.getResourceAsStream.
-chris