Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » Maven Integration (M2E) » Specifying resources for runtime usage?
Specifying resources for runtime usage? [message #29231] Fri, 19 June 2009 08:11 Go to next message
Eclipse UserFriend
Originally posted by: ddd.asd.com

I have some xml files in a maven module in:

src/main/resources

which is loaded in my application with:

URL url = this.getClass().getClassLoader().getResource("somexml.xml");

When running my application from eclipse the files are found and used
correctly.

When I build my application using "mvn install" and then run it those
resources are not found/used. I have opened the .jar files (created during
the packaging) and the resources are included. I have also tried to add:

<build>
<resources>
<resource>
<directory> src\main\resources</directory>
</resource>
</resources>
</build>

to the pom.xml file but that does not help.

Any ideas on how to use resources from the packaged jar files when running
an application?
Re: Specifying resources for runtime usage? [message #29269 is a reply to message #29231] Fri, 19 June 2009 09:14 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ddd.asd.com

"mlt" <ddd@asd.com> wrote in message news:h1fh8m$m8v$1@build.eclipse.org...
>I have some xml files in a maven module in:
>
> src/main/resources
>
> which is loaded in my application with:
>
> URL url = this.getClass().getClassLoader().getResource("somexml.xml");
>
> When running my application from eclipse the files are found and used
> correctly.
>
> When I build my application using "mvn install" and then run it those
> resources are not found/used. I have opened the .jar files (created during
> the packaging) and the resources are included. I have also tried to add:
>
> <build>
> <resources>
> <resource>
> <directory> src\main\resources</directory>
> </resource>
> </resources>
> </build>
>
> to the pom.xml file but that does not help.
>
> Any ideas on how to use resources from the packaged jar files when
> running an application?
>
>

The resources is loaded:
....
URL url =
this.getClass().getClassLoader().getResource("somexmlfile.xml ");
try {
file = new File(url.getPath());
} catch (Exception e) {

}

I have also tried:
file = new File(url.toURI());

but this throws:

URI is not hierarchical exception.

It seems that this problem is not that the resource is not found but rather
how to use it. I will find another forum, but if anyone in here has an idea
you are welcome to make a comment.
Re: Specifying resources for runtime usage? [message #29307 is a reply to message #29269] Fri, 19 June 2009 13:01 Go to previous message
Eugene Kuleshov is currently offline Eugene KuleshovFriend
Messages: 504
Registered: July 2009
Senior Member
mlt wrote:
>> I have some xml files in a maven module in:
>> src/main/resources
>> which is loaded in my application with:
>>
>> URL url =
>> this.getClass().getClassLoader().getResource("somexml.xml");
>>
>> When running my application from eclipse the files are found and used
>> correctly.
>>
>> When I build my application using "mvn install" and then run it those
>> resources are not found/used. I have opened the .jar files (created
>> during the packaging) and the resources are included. I have also
>> tried to add:
>>
>> <build>
>> <resources>
>> <resource>
>> <directory> src\main\resources</directory>
>> </resource>
>> </resources>
>> </build>
You shouldn't need to do that, because this folder is default one used
by maven-resources-plugin
Have you tried to look inside jar file and check if resource files are
actually in there? If they aren't then try to run mvn help:effective-pom
and see if the actual resource folders are there. If folders are there,
please post the log from execution "mvn -X clean package" command.
Also, when running in Eclipse, please run "Maven / update project
configuration" to make sure that eclipse project settings match your pom.xml
> The resources is loaded:
> ....
> URL url =
> this.getClass().getClassLoader().getResource("somexmlfile.xml ");
> try {
> file = new File(url.getPath());
> } catch (Exception e) {
>
> }
That is not going to work when resource is inside a jar file.
However you can print the url and see where it is being resolved to
and then check if that place is actually exist

URL url =
this.getClass().getClassLoader().getResource("somexmlfile.xml ");
System.err.println(url.toString());

regards,
Eugene
Previous Topic:Maven plug-in for Eclipse 3.0
Next Topic:project plan/roadmap? 1.0 release date?
Goto Forum:
  


Current Time: Sat Apr 20 00:16:04 GMT 2024

Powered by FUDForum. Page generated in 0.02605 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top