• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing files on classpath using parent directory sign (../)

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Another problem with resources on classpath by me
I am trying to access resource on classpath using getResourceAsStream:

If path is something like this: , then I have no problems.
However, I have situations where I must use "go to parent" sign ../ to access certain files. I am constructing path manually based on certain parameters.
Final result is something like this: .
This works fine in Eclipse. It works fine when I run it outside of eclipse (from command line in programs target folder where .class are contained). However, as soon as I pack those in jar file, it throws nullpointer exception.

Is there a possibility for getResourceAsStream to support such a format when exported to jar.
If not, is there a Java/Utility class that can get file path without ../ signs. For example to convert to format?
Thanks in advance!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't cross post. It wastes people time and effort..... Your other topic has been deleted.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Coss wrote:
Is there a possibility for getResourceAsStream to support such a format when exported to jar.
If not, is there a Java/Utility class that can get file path without ../ signs. For example to convert to format?



My first instinct would try to use regular expressions to remove the ".." from the path -- specifically delete "[^/]*/\.\." from the path.

Henry
 
John Coss
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Henry

Sorry, it was never my intention to double-post topic, but it happened that forum showed me some error page when I posted topic in the first place.

Anyway, if all else fails I can always create method that will 'parse' path without (../) signs. If I recall there might be class or utility in java that do all these automatically, but I can't remember it's name (or it was c#).

Thanks
 
John Coss
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I found how you can transform path from /net/original/../original/xsdtestxml.xml to /net/original/xsdtestxml.xml

You can use URI class found in java.net package. Simply create URI object with input path and then call normalize() method. It should fix path in format without ../ signs.

Here is a code:
String finalPath = URI.create("/net/original/../original/xsdtestxml.xml").normalize().getPath();

This returns string that is what I needed: /net/original/xsdtestxml.xml

Solution found
 
Being a smart alec beats the alternative. This tiny ad knows what I'm talking about:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic