You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@servicemix.apache.org by shamik <sh...@gmail.com> on 2011/07/08 02:10:32 UTC

How to get absolute classpath of a file ?

Hi,
  I'm trying figure a way to get the absolute path of files located under my
project's resource folder. The file paths are injected through spring as : 

<property name="siteConfig" value="classpath:/Test/test.xml" />

The directory is located under src/main/resources folder, which makes it
available as a classpath resource inside the bundle. 

The property is being set as a Spring Resource object inside the java class.
I'm trying to access the file following way :

 File testFile = new File(siteConfig.getURL().getPath()); 

I'm passing this File object to another program which then reads it.

siteConfig.getURL() --> bundle://249.14:1/TEST/test.xml

testFile.getAbsolutePath() --> C:\TEST\test.xml . 

As you can see, the absolute path is just repalcing "bundle://249.14:1" with
"C:\", which is incorrect. As a result, when the program is trying to read
the file, it's not able to pick it up.  

Any pointers will be appreciated.

- Thanks

--
View this message in context: http://servicemix.396122.n5.nabble.com/How-to-get-absolute-classpath-of-a-file-tp4562967p4562967.html
Sent from the ServiceMix - User mailing list archive at Nabble.com.

Re: How to get absolute classpath of a file ?

Posted by shamik <sh...@gmail.com>.
Ok, I misred you on this. I agree with you that reading a File is not the
best idea. Unfortunately, if you are dealing with a third party library
which only accepts a file object, then you re not left with a choice :-(
....
On Thu, Jul 7, 2011 at 6:05 PM, Johan Edstrom-2 [via ServiceMix] <
ml-node+4563042-2059441079-220423@n5.nabble.com> wrote:

> Not what I said, I said that loading it as a File is a bad idea.
>
> On Jul 7, 2011, at 7:00 PM, shamik wrote:
>
> > I don't think its necessarily a bad idea to load a file within the same
> jar.
> > If a file is specific to a bundle, then what's the issue in keeping it
> > inside the jar ? Isn't Spring keeping the application context file inside
>
> > the bundle ? Moreover, if the file is external to the bundle , then
> updating
> > the file becomes and issue as well.
> > On Thu, Jul 7, 2011 at 5:39 PM, Johan Edstrom-2 [via ServiceMix] <
> > [hidden email] <http://user/SendEmail.jtp?type=node&node=4563042&i=0>>
> wrote:
> >
> >> It is not a servicemix limitation, it is an OSGi feature/benefit.
> >>
> >> Using a File to load from a jar is not a good idea either to begin with.
>
> >>
> >>
> >> On Jul 7, 2011, at 6:34 PM, Shamik Bandopadhyay wrote:
> >>
> >>> Thanks for your reply, I had the same feeling looking into it. My
> problem
> >>
> >>> is, I'm using a third party lib which needs a File object as an input
> >> param.
> >>> Even though I'm setting it as a Resource object through Spring, I need
> to
> >>
> >>> transform it to a File.
> >>>
> >>> One alternate is to externalize the files outside the bundle in a
> >>> pre-defined directory and then have it defined as
> >>> file:///....full_file_pathin spring config. But, I don't want to
> >>> externalize it, moreover having this
> >>> hard coded file path makes it difficult to work on different
> >> environments.
> >>>
> >>> Having said that, I'm bit surprised why would Framework like servicemix
>
> >> will
> >>> have such limitation? How would the problem im my case is supposed to
> be
> >>> addressed ?
> >>>
> >>>
> >>>
> >>> On Thu, Jul 7, 2011 at 5:27 PM, Johan Edstrom <[hidden email]<
> http://user/SendEmail.jtp?type=node&node=4563012&i=0>>
> >> wrote:
> >>>
> >>>> It is actually very correct.
> >>>> Absolute classpath like a file resource is not supported in OSGi, nor
> >>>> should
> >>>> you rely on it, you can get around it in equinox, but containers like
> >>>> Knoplerfish
> >>>> will deliver even resources from RAM.
> >>>>
> >>>> So you want to use getting it as a resource, not a file.
> >>>>
> >>>>
> >>>> On Jul 7, 2011, at 6:10 PM, shamik wrote:
> >>>>
> >>>>> Hi,
> >>>>> I'm trying figure a way to get the absolute path of files located
> under
> >>
> >>>> my
> >>>>> project's resource folder. The file paths are injected through spring
>
> >> as
> >>>> :
> >>>>>
> >>>>> <property name="siteConfig" value="classpath:/Test/test.xml" />
> >>>>>
> >>>>> The directory is located under src/main/resources folder, which makes
>
> >> it
> >>>>> available as a classpath resource inside the bundle.
> >>>>>
> >>>>> The property is being set as a Spring Resource object inside the java
>
> >>>> class.
> >>>>> I'm trying to access the file following way :
> >>>>>
> >>>>> File testFile = new File(siteConfig.getURL().getPath());
> >>>>>
> >>>>> I'm passing this File object to another program which then reads it.
> >>>>>
> >>>>> siteConfig.getURL() --> bundle://249.14:1/TEST/test.xml
> >>>>>
> >>>>> testFile.getAbsolutePath() --> C:\TEST\test.xml .
> >>>>>
> >>>>> As you can see, the absolute path is just repalcing
> "bundle://249.14:1"
> >>
> >>>> with
> >>>>> "C:\", which is incorrect. As a result, when the program is trying to
>
> >>>> read
> >>>>> the file, it's not able to pick it up.
> >>>>>
> >>>>> Any pointers will be appreciated.
> >>>>>
> >>>>> - Thanks
> >>>>>
> >>>>> --
> >>>>> View this message in context:
> >>>>
> >>
> http://servicemix.396122.n5.nabble.com/How-to-get-absolute-classpath-of-a-file-tp4562967p4562967.html
> >>>>> Sent from the ServiceMix - User mailing list archive at Nabble.com.
> >>>>
> >>>>
> >>
> >>
> >>
> >> ------------------------------
> >> If you reply to this email, your message will be added to the discussion
>
> >> below:
> >>
> >>
>
> >>
> >>
> >
> >
> > --
> > View this message in context:
> http://servicemix.396122.n5.nabble.com/How-to-get-absolute-classpath-of-a-file-tp4562967p4563037.html
>
> > Sent from the ServiceMix - User mailing list archive at Nabble.com.
>
>
>
> ------------------------------
>  If you reply to this email, your message will be added to the discussion
> below:
>
> http://servicemix.396122.n5.nabble.com/How-to-get-absolute-classpath-of-a-file-tp4562967p4563042.html
>  To unsubscribe from How to get absolute classpath of a file ?, click here<http://servicemix.396122.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4562967&code=c2hhbWlrYkBnbWFpbC5jb218NDU2Mjk2N3w2MzMwMTIyOA==>.
>
>


--
View this message in context: http://servicemix.396122.n5.nabble.com/How-to-get-absolute-classpath-of-a-file-tp4562967p4563068.html
Sent from the ServiceMix - User mailing list archive at Nabble.com.

Re: How to get absolute classpath of a file ?

Posted by Johan Edstrom <se...@gmail.com>.
Not what I said, I said that loading it as a File is a bad idea.

On Jul 7, 2011, at 7:00 PM, shamik wrote:

> I don't think its necessarily a bad idea to load a file within the same jar.
> If a file is specific to a bundle, then what's the issue in keeping it
> inside the jar ? Isn't Spring keeping the application context file inside
> the bundle ? Moreover, if the file is external to the bundle , then updating
> the file becomes and issue as well.
> On Thu, Jul 7, 2011 at 5:39 PM, Johan Edstrom-2 [via ServiceMix] <
> ml-node+4563012-1058602669-220423@n5.nabble.com> wrote:
> 
>> It is not a servicemix limitation, it is an OSGi feature/benefit.
>> 
>> Using a File to load from a jar is not a good idea either to begin with.
>> 
>> 
>> On Jul 7, 2011, at 6:34 PM, Shamik Bandopadhyay wrote:
>> 
>>> Thanks for your reply, I had the same feeling looking into it. My problem
>> 
>>> is, I'm using a third party lib which needs a File object as an input
>> param.
>>> Even though I'm setting it as a Resource object through Spring, I need to
>> 
>>> transform it to a File.
>>> 
>>> One alternate is to externalize the files outside the bundle in a
>>> pre-defined directory and then have it defined as
>>> file:///....full_file_pathin spring config. But, I don't want to
>>> externalize it, moreover having this
>>> hard coded file path makes it difficult to work on different
>> environments.
>>> 
>>> Having said that, I'm bit surprised why would Framework like servicemix
>> will
>>> have such limitation? How would the problem im my case is supposed to be
>>> addressed ?
>>> 
>>> 
>>> 
>>> On Thu, Jul 7, 2011 at 5:27 PM, Johan Edstrom <[hidden email]<http://user/SendEmail.jtp?type=node&node=4563012&i=0>>
>> wrote:
>>> 
>>>> It is actually very correct.
>>>> Absolute classpath like a file resource is not supported in OSGi, nor
>>>> should
>>>> you rely on it, you can get around it in equinox, but containers like
>>>> Knoplerfish
>>>> will deliver even resources from RAM.
>>>> 
>>>> So you want to use getting it as a resource, not a file.
>>>> 
>>>> 
>>>> On Jul 7, 2011, at 6:10 PM, shamik wrote:
>>>> 
>>>>> Hi,
>>>>> I'm trying figure a way to get the absolute path of files located under
>> 
>>>> my
>>>>> project's resource folder. The file paths are injected through spring
>> as
>>>> :
>>>>> 
>>>>> <property name="siteConfig" value="classpath:/Test/test.xml" />
>>>>> 
>>>>> The directory is located under src/main/resources folder, which makes
>> it
>>>>> available as a classpath resource inside the bundle.
>>>>> 
>>>>> The property is being set as a Spring Resource object inside the java
>>>> class.
>>>>> I'm trying to access the file following way :
>>>>> 
>>>>> File testFile = new File(siteConfig.getURL().getPath());
>>>>> 
>>>>> I'm passing this File object to another program which then reads it.
>>>>> 
>>>>> siteConfig.getURL() --> bundle://249.14:1/TEST/test.xml
>>>>> 
>>>>> testFile.getAbsolutePath() --> C:\TEST\test.xml .
>>>>> 
>>>>> As you can see, the absolute path is just repalcing "bundle://249.14:1"
>> 
>>>> with
>>>>> "C:\", which is incorrect. As a result, when the program is trying to
>>>> read
>>>>> the file, it's not able to pick it up.
>>>>> 
>>>>> Any pointers will be appreciated.
>>>>> 
>>>>> - Thanks
>>>>> 
>>>>> --
>>>>> View this message in context:
>>>> 
>> http://servicemix.396122.n5.nabble.com/How-to-get-absolute-classpath-of-a-file-tp4562967p4562967.html
>>>>> Sent from the ServiceMix - User mailing list archive at Nabble.com.
>>>> 
>>>> 
>> 
>> 
>> 
>> ------------------------------
>> If you reply to this email, your message will be added to the discussion
>> below:
>> 
>> http://servicemix.396122.n5.nabble.com/How-to-get-absolute-classpath-of-a-file-tp4562967p4563012.html
>> To unsubscribe from How to get absolute classpath of a file ?, click here<http://servicemix.396122.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4562967&code=c2hhbWlrYkBnbWFpbC5jb218NDU2Mjk2N3w2MzMwMTIyOA==>.
>> 
>> 
> 
> 
> --
> View this message in context: http://servicemix.396122.n5.nabble.com/How-to-get-absolute-classpath-of-a-file-tp4562967p4563037.html
> Sent from the ServiceMix - User mailing list archive at Nabble.com.


Re: How to get absolute classpath of a file ?

Posted by shamik <sh...@gmail.com>.
I don't think its necessarily a bad idea to load a file within the same jar.
If a file is specific to a bundle, then what's the issue in keeping it
inside the jar ? Isn't Spring keeping the application context file inside
the bundle ? Moreover, if the file is external to the bundle , then updating
the file becomes and issue as well.
On Thu, Jul 7, 2011 at 5:39 PM, Johan Edstrom-2 [via ServiceMix] <
ml-node+4563012-1058602669-220423@n5.nabble.com> wrote:

> It is not a servicemix limitation, it is an OSGi feature/benefit.
>
> Using a File to load from a jar is not a good idea either to begin with.
>
>
> On Jul 7, 2011, at 6:34 PM, Shamik Bandopadhyay wrote:
>
> > Thanks for your reply, I had the same feeling looking into it. My problem
>
> > is, I'm using a third party lib which needs a File object as an input
> param.
> > Even though I'm setting it as a Resource object through Spring, I need to
>
> > transform it to a File.
> >
> > One alternate is to externalize the files outside the bundle in a
> > pre-defined directory and then have it defined as
> > file:///....full_file_pathin spring config. But, I don't want to
> > externalize it, moreover having this
> > hard coded file path makes it difficult to work on different
> environments.
> >
> > Having said that, I'm bit surprised why would Framework like servicemix
> will
> > have such limitation? How would the problem im my case is supposed to be
> > addressed ?
> >
> >
> >
> > On Thu, Jul 7, 2011 at 5:27 PM, Johan Edstrom <[hidden email]<http://user/SendEmail.jtp?type=node&node=4563012&i=0>>
> wrote:
> >
> >> It is actually very correct.
> >> Absolute classpath like a file resource is not supported in OSGi, nor
> >> should
> >> you rely on it, you can get around it in equinox, but containers like
> >> Knoplerfish
> >> will deliver even resources from RAM.
> >>
> >> So you want to use getting it as a resource, not a file.
> >>
> >>
> >> On Jul 7, 2011, at 6:10 PM, shamik wrote:
> >>
> >>> Hi,
> >>> I'm trying figure a way to get the absolute path of files located under
>
> >> my
> >>> project's resource folder. The file paths are injected through spring
> as
> >> :
> >>>
> >>> <property name="siteConfig" value="classpath:/Test/test.xml" />
> >>>
> >>> The directory is located under src/main/resources folder, which makes
> it
> >>> available as a classpath resource inside the bundle.
> >>>
> >>> The property is being set as a Spring Resource object inside the java
> >> class.
> >>> I'm trying to access the file following way :
> >>>
> >>> File testFile = new File(siteConfig.getURL().getPath());
> >>>
> >>> I'm passing this File object to another program which then reads it.
> >>>
> >>> siteConfig.getURL() --> bundle://249.14:1/TEST/test.xml
> >>>
> >>> testFile.getAbsolutePath() --> C:\TEST\test.xml .
> >>>
> >>> As you can see, the absolute path is just repalcing "bundle://249.14:1"
>
> >> with
> >>> "C:\", which is incorrect. As a result, when the program is trying to
> >> read
> >>> the file, it's not able to pick it up.
> >>>
> >>> Any pointers will be appreciated.
> >>>
> >>> - Thanks
> >>>
> >>> --
> >>> View this message in context:
> >>
> http://servicemix.396122.n5.nabble.com/How-to-get-absolute-classpath-of-a-file-tp4562967p4562967.html
> >>> Sent from the ServiceMix - User mailing list archive at Nabble.com.
> >>
> >>
>
>
>
> ------------------------------
>  If you reply to this email, your message will be added to the discussion
> below:
>
> http://servicemix.396122.n5.nabble.com/How-to-get-absolute-classpath-of-a-file-tp4562967p4563012.html
>  To unsubscribe from How to get absolute classpath of a file ?, click here<http://servicemix.396122.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4562967&code=c2hhbWlrYkBnbWFpbC5jb218NDU2Mjk2N3w2MzMwMTIyOA==>.
>
>


--
View this message in context: http://servicemix.396122.n5.nabble.com/How-to-get-absolute-classpath-of-a-file-tp4562967p4563037.html
Sent from the ServiceMix - User mailing list archive at Nabble.com.

Re: How to get absolute classpath of a file ?

Posted by Johan Edstrom <se...@gmail.com>.
It is not a servicemix limitation, it is an OSGi feature/benefit.

Using a File to load from a jar is not a good idea either to begin with.


On Jul 7, 2011, at 6:34 PM, Shamik Bandopadhyay wrote:

> Thanks for your reply, I had the same feeling looking into it. My problem
> is, I'm using a third party lib which needs a File object as an input param.
> Even though I'm setting it as a Resource object through Spring, I need to
> transform it to a File.
> 
> One alternate is to externalize the files outside the bundle in a
> pre-defined directory and then have it defined as
> file:///....full_file_pathin spring config. But, I don't want to
> externalize it, moreover having this
> hard coded file path makes it difficult to work on different environments.
> 
> Having said that, I'm bit surprised why would Framework like servicemix will
> have such limitation? How would the problem im my case is supposed to be
> addressed ?
> 
> 
> 
> On Thu, Jul 7, 2011 at 5:27 PM, Johan Edstrom <se...@gmail.com> wrote:
> 
>> It is actually very correct.
>> Absolute classpath like a file resource is not supported in OSGi, nor
>> should
>> you rely on it, you can get around it in equinox, but containers like
>> Knoplerfish
>> will deliver even resources from RAM.
>> 
>> So you want to use getting it as a resource, not a file.
>> 
>> 
>> On Jul 7, 2011, at 6:10 PM, shamik wrote:
>> 
>>> Hi,
>>> I'm trying figure a way to get the absolute path of files located under
>> my
>>> project's resource folder. The file paths are injected through spring as
>> :
>>> 
>>> <property name="siteConfig" value="classpath:/Test/test.xml" />
>>> 
>>> The directory is located under src/main/resources folder, which makes it
>>> available as a classpath resource inside the bundle.
>>> 
>>> The property is being set as a Spring Resource object inside the java
>> class.
>>> I'm trying to access the file following way :
>>> 
>>> File testFile = new File(siteConfig.getURL().getPath());
>>> 
>>> I'm passing this File object to another program which then reads it.
>>> 
>>> siteConfig.getURL() --> bundle://249.14:1/TEST/test.xml
>>> 
>>> testFile.getAbsolutePath() --> C:\TEST\test.xml .
>>> 
>>> As you can see, the absolute path is just repalcing "bundle://249.14:1"
>> with
>>> "C:\", which is incorrect. As a result, when the program is trying to
>> read
>>> the file, it's not able to pick it up.
>>> 
>>> Any pointers will be appreciated.
>>> 
>>> - Thanks
>>> 
>>> --
>>> View this message in context:
>> http://servicemix.396122.n5.nabble.com/How-to-get-absolute-classpath-of-a-file-tp4562967p4562967.html
>>> Sent from the ServiceMix - User mailing list archive at Nabble.com.
>> 
>> 


Re: How to get absolute classpath of a file ?

Posted by Shamik Bandopadhyay <sh...@gmail.com>.
Thanks for your reply, I had the same feeling looking into it. My problem
is, I'm using a third party lib which needs a File object as an input param.
Even though I'm setting it as a Resource object through Spring, I need to
transform it to a File.

One alternate is to externalize the files outside the bundle in a
pre-defined directory and then have it defined as
file:///....full_file_pathin spring config. But, I don't want to
externalize it, moreover having this
hard coded file path makes it difficult to work on different environments.

Having said that, I'm bit surprised why would Framework like servicemix will
have such limitation? How would the problem im my case is supposed to be
addressed ?



On Thu, Jul 7, 2011 at 5:27 PM, Johan Edstrom <se...@gmail.com> wrote:

> It is actually very correct.
> Absolute classpath like a file resource is not supported in OSGi, nor
> should
> you rely on it, you can get around it in equinox, but containers like
> Knoplerfish
> will deliver even resources from RAM.
>
> So you want to use getting it as a resource, not a file.
>
>
> On Jul 7, 2011, at 6:10 PM, shamik wrote:
>
> > Hi,
> >  I'm trying figure a way to get the absolute path of files located under
> my
> > project's resource folder. The file paths are injected through spring as
> :
> >
> > <property name="siteConfig" value="classpath:/Test/test.xml" />
> >
> > The directory is located under src/main/resources folder, which makes it
> > available as a classpath resource inside the bundle.
> >
> > The property is being set as a Spring Resource object inside the java
> class.
> > I'm trying to access the file following way :
> >
> > File testFile = new File(siteConfig.getURL().getPath());
> >
> > I'm passing this File object to another program which then reads it.
> >
> > siteConfig.getURL() --> bundle://249.14:1/TEST/test.xml
> >
> > testFile.getAbsolutePath() --> C:\TEST\test.xml .
> >
> > As you can see, the absolute path is just repalcing "bundle://249.14:1"
> with
> > "C:\", which is incorrect. As a result, when the program is trying to
> read
> > the file, it's not able to pick it up.
> >
> > Any pointers will be appreciated.
> >
> > - Thanks
> >
> > --
> > View this message in context:
> http://servicemix.396122.n5.nabble.com/How-to-get-absolute-classpath-of-a-file-tp4562967p4562967.html
> > Sent from the ServiceMix - User mailing list archive at Nabble.com.
>
>

Re: How to get absolute classpath of a file ?

Posted by Johan Edstrom <se...@gmail.com>.
It is actually very correct.
Absolute classpath like a file resource is not supported in OSGi, nor should 
you rely on it, you can get around it in equinox, but containers like Knoplerfish
will deliver even resources from RAM.

So you want to use getting it as a resource, not a file.


On Jul 7, 2011, at 6:10 PM, shamik wrote:

> Hi,
>  I'm trying figure a way to get the absolute path of files located under my
> project's resource folder. The file paths are injected through spring as : 
> 
> <property name="siteConfig" value="classpath:/Test/test.xml" />
> 
> The directory is located under src/main/resources folder, which makes it
> available as a classpath resource inside the bundle. 
> 
> The property is being set as a Spring Resource object inside the java class.
> I'm trying to access the file following way :
> 
> File testFile = new File(siteConfig.getURL().getPath()); 
> 
> I'm passing this File object to another program which then reads it.
> 
> siteConfig.getURL() --> bundle://249.14:1/TEST/test.xml
> 
> testFile.getAbsolutePath() --> C:\TEST\test.xml . 
> 
> As you can see, the absolute path is just repalcing "bundle://249.14:1" with
> "C:\", which is incorrect. As a result, when the program is trying to read
> the file, it's not able to pick it up.  
> 
> Any pointers will be appreciated.
> 
> - Thanks
> 
> --
> View this message in context: http://servicemix.396122.n5.nabble.com/How-to-get-absolute-classpath-of-a-file-tp4562967p4562967.html
> Sent from the ServiceMix - User mailing list archive at Nabble.com.