You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Benoît Thiébault <th...@artenum.com> on 2010/04/06 15:10:11 UTC

Accessing resources

Hi everyone,

This is certainly a very dumb question but I have trouble accessing my resources that are located in the src/resource directory.

In my code, to load an image, I do:
ImageIO.read(getClass().getResourceAsStream("/myImage.png"))

When Maven packages my software, it puts everything in the resource directory at the root of the JAR file.
So when I use my software from another code, including the jar in the classpath, it works.
But if I use my software as a standalone application (without packaging it in a jar file), it can't find the image (which is logical because the file in not at the same place than in the jar).

How do you solve this problem ? Do I really have to run my app from the jar ? How can I configure Eclipse (with m2eclipse plugin installed) to run my code correctly ?

Thank you
Benoît


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: Accessing resources

Posted by Benoît Thiébault <th...@artenum.com>.
Thank you :-)
It was already in the Java Build Path (it has been added automatically when I imported my Maven project with m2eclipse) but it did exclude **...
Benoît

Le 6 avr. 2010 à 15:50, Markku Saarela a écrit :

> 
> In Eclipse simply add this src/main/resources to Java Build Path as source folder then class running in eclipse can see it.
> 
> rgds,
> 
> Markku
> 
> On 6.4.2010 16:39, Benoît Thiébault wrote:
>> :-)
>> Sorry that I have not been clear. Let me detail a bit further.
>> 
>> I have a project that I will call MyProjectA. It is structured according to Maven conventions:
>> 
>> MyProjectA
>> |-- pom.xml
>> `-- src
>>     |-- main
>>     |   `-- resources
>>     |       `-- myImage.png
>>     |   `-- java
>>     |       `-- com
>>     |           `-- mycompany
>>     |               `-- app
>>     |                   `-- Main.java
>>     |                   `-- ClassRequiringImage.java
>>     `-- test
>>         `-- java
>>             `-- com
>>                 `-- mycompany
>>                     `-- app
>>                         `-- MainTest.java
>> 
>> The image is loaded by the ClassRequiringImage class with the code line I described earlier. The Main class can be executed and uses the ClassRequiringImage class. But when it does so, getClass().getResourceAsStream("/myImage.png") returns null (which is logical, I should but the image in src/main/java for this code to work).
>> 
>> I have a second project, MyProjectB that depends on the first one. It is not using Maven to compile and run but Ant. To use the classes of MyProjectA, I just copied the jar in MyProjectB class path.
>> When MyProjectB uses the ClassRequiringImage class from the MyProjectA project, as the image is at the root of the JAR, getClass().getResourceAsStream("/myImage.png") returns the image.
>> 
>> The problem is that I want to have both ways of using the code to work. At first, I had a resource package com.mycompany.app.resource containing the image, but Maven does not seem to include its content when packaging the JAR. This is why I tried to comply with the convention and moved my files to src/main/resources.
>> 
>> Benoît
>> 
>> 
>> Le 6 avr. 2010 à 15:16, maven apache a écrit :
>> 
>>   
>>> 2010/4/6 Benoît Thiébault<th...@artenum.com>
>>> 
>>>     
>>>> Hi everyone,
>>>> 
>>>> This is certainly a very dumb question but I have trouble accessing my
>>>> resources that are located in the src/resource directory.
>>>> 
>>>> In my code, to load an image, I do:
>>>> ImageIO.read(getClass().getResourceAsStream("/myImage.png"))
>>>> 
>>>> When Maven packages my software, it puts everything in the resource
>>>> directory at the root of the JAR file.
>>>> So when I use my software from another code, including the jar in the
>>>> classpath, it works.
>>>> 
>>>>       
>>> What does the " my software " mean? Can you show the structure of your
>>> project?
>>> 
>>>     
>>>> But if I use my software as a standalone application (without packaging it
>>>> in a jar file), it can't find the image (which is logical because the file
>>>> in not at the same place than in the jar).
>>>> 
>>>> How do you solve this problem ? Do I really have to run my app from the jar
>>>> ? How can I configure Eclipse (with m2eclipse plugin installed) to run my
>>>> code correctly ?
>>>> 
>>>> Thank you
>>>> Benoît
>>>> 
>>>> 
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
>>>> For additional commands, e-mail: users-help@maven.apache.org
>>>> 
>>>> 
>>>>       
>> ----
>> Benoît Thiébault
>> 
>>   Artenum - Science&  Groupware
>>   http://www.artenum.com
>> 
>>   24, rue Louis-Blanc, 75010 Paris
>>   tel/fax : +33 (0)1 46 94 67 54
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
>> For additional commands, e-mail: users-help@maven.apache.org
>> 
>>   
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
> 
> 

----
Benoît Thiébault

  Artenum - Science & Groupware
  http://www.artenum.com

  24, rue Louis-Blanc, 75010 Paris
  tel/fax : +33 (0)1 46 94 67 54


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: Accessing resources

Posted by Markku Saarela <ma...@iki.fi>.
In Eclipse simply add this src/main/resources to Java Build Path as 
source folder then class running in eclipse can see it.

rgds,

Markku

On 6.4.2010 16:39, Benoît Thiébault wrote:
> :-)
> Sorry that I have not been clear. Let me detail a bit further.
>
> I have a project that I will call MyProjectA. It is structured according to Maven conventions:
>
> MyProjectA
> |-- pom.xml
> `-- src
>      |-- main
>      |   `-- resources
>      |       `-- myImage.png
>      |   `-- java
>      |       `-- com
>      |           `-- mycompany
>      |               `-- app
>      |                   `-- Main.java
>      |                   `-- ClassRequiringImage.java
>      `-- test
>          `-- java
>              `-- com
>                  `-- mycompany
>                      `-- app
>                          `-- MainTest.java
>
> The image is loaded by the ClassRequiringImage class with the code line I described earlier. The Main class can be executed and uses the ClassRequiringImage class. But when it does so, getClass().getResourceAsStream("/myImage.png") returns null (which is logical, I should but the image in src/main/java for this code to work).
>
> I have a second project, MyProjectB that depends on the first one. It is not using Maven to compile and run but Ant. To use the classes of MyProjectA, I just copied the jar in MyProjectB class path.
> When MyProjectB uses the ClassRequiringImage class from the MyProjectA project, as the image is at the root of the JAR, getClass().getResourceAsStream("/myImage.png") returns the image.
>
> The problem is that I want to have both ways of using the code to work. At first, I had a resource package com.mycompany.app.resource containing the image, but Maven does not seem to include its content when packaging the JAR. This is why I tried to comply with the convention and moved my files to src/main/resources.
>
> Benoît
>
>
> Le 6 avr. 2010 à 15:16, maven apache a écrit :
>
>    
>> 2010/4/6 Benoît Thiébault<th...@artenum.com>
>>
>>      
>>> Hi everyone,
>>>
>>> This is certainly a very dumb question but I have trouble accessing my
>>> resources that are located in the src/resource directory.
>>>
>>> In my code, to load an image, I do:
>>> ImageIO.read(getClass().getResourceAsStream("/myImage.png"))
>>>
>>> When Maven packages my software, it puts everything in the resource
>>> directory at the root of the JAR file.
>>> So when I use my software from another code, including the jar in the
>>> classpath, it works.
>>>
>>>        
>> What does the " my software " mean? Can you show the structure of your
>> project?
>>
>>      
>>> But if I use my software as a standalone application (without packaging it
>>> in a jar file), it can't find the image (which is logical because the file
>>> in not at the same place than in the jar).
>>>
>>> How do you solve this problem ? Do I really have to run my app from the jar
>>> ? How can I configure Eclipse (with m2eclipse plugin installed) to run my
>>> code correctly ?
>>>
>>> Thank you
>>> Benoît
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
>>> For additional commands, e-mail: users-help@maven.apache.org
>>>
>>>
>>>        
> ----
> Benoît Thiébault
>
>    Artenum - Science&  Groupware
>    http://www.artenum.com
>
>    24, rue Louis-Blanc, 75010 Paris
>    tel/fax : +33 (0)1 46 94 67 54
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>    

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: Accessing resources

Posted by Benoît Thiébault <th...@artenum.com>.
:-)
Sorry that I have not been clear. Let me detail a bit further.

I have a project that I will call MyProjectA. It is structured according to Maven conventions:

MyProjectA
|-- pom.xml
`-- src
    |-- main
    |   `-- resources
    |       `-- myImage.png
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- Main.java
    |                   `-- ClassRequiringImage.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- MainTest.java

The image is loaded by the ClassRequiringImage class with the code line I described earlier. The Main class can be executed and uses the ClassRequiringImage class. But when it does so, getClass().getResourceAsStream("/myImage.png") returns null (which is logical, I should but the image in src/main/java for this code to work).

I have a second project, MyProjectB that depends on the first one. It is not using Maven to compile and run but Ant. To use the classes of MyProjectA, I just copied the jar in MyProjectB class path.
When MyProjectB uses the ClassRequiringImage class from the MyProjectA project, as the image is at the root of the JAR, getClass().getResourceAsStream("/myImage.png") returns the image.

The problem is that I want to have both ways of using the code to work. At first, I had a resource package com.mycompany.app.resource containing the image, but Maven does not seem to include its content when packaging the JAR. This is why I tried to comply with the convention and moved my files to src/main/resources.

Benoît


Le 6 avr. 2010 à 15:16, maven apache a écrit :

> 2010/4/6 Benoît Thiébault <th...@artenum.com>
> 
>> Hi everyone,
>> 
>> This is certainly a very dumb question but I have trouble accessing my
>> resources that are located in the src/resource directory.
>> 
>> In my code, to load an image, I do:
>> ImageIO.read(getClass().getResourceAsStream("/myImage.png"))
>> 
>> When Maven packages my software, it puts everything in the resource
>> directory at the root of the JAR file.
>> So when I use my software from another code, including the jar in the
>> classpath, it works.
>> 
> What does the " my software " mean? Can you show the structure of your
> project?
> 
>> But if I use my software as a standalone application (without packaging it
>> in a jar file), it can't find the image (which is logical because the file
>> in not at the same place than in the jar).
>> 
>> How do you solve this problem ? Do I really have to run my app from the jar
>> ? How can I configure Eclipse (with m2eclipse plugin installed) to run my
>> code correctly ?
>> 
>> Thank you
>> Benoît
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
>> For additional commands, e-mail: users-help@maven.apache.org
>> 
>> 

----
Benoît Thiébault

  Artenum - Science & Groupware
  http://www.artenum.com

  24, rue Louis-Blanc, 75010 Paris
  tel/fax : +33 (0)1 46 94 67 54


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Re: Accessing resources

Posted by maven apache <ap...@gmail.com>.
2010/4/6 Benoît Thiébault <th...@artenum.com>

> Hi everyone,
>
> This is certainly a very dumb question but I have trouble accessing my
> resources that are located in the src/resource directory.
>
> In my code, to load an image, I do:
> ImageIO.read(getClass().getResourceAsStream("/myImage.png"))
>
> When Maven packages my software, it puts everything in the resource
> directory at the root of the JAR file.
> So when I use my software from another code, including the jar in the
> classpath, it works.
>
What does the " my software " mean? Can you show the structure of your
project?

> But if I use my software as a standalone application (without packaging it
> in a jar file), it can't find the image (which is logical because the file
> in not at the same place than in the jar).
>
> How do you solve this problem ? Do I really have to run my app from the jar
> ? How can I configure Eclipse (with m2eclipse plugin installed) to run my
> code correctly ?
>
> Thank you
> Benoît
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>