File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Accessing files on classpath using parent directory sign (../) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Accessing files on classpath using parent directory sign (../)" Watch "Accessing files on classpath using parent directory sign (../)" New topic
Author

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

John Coss
Greenhorn

Joined: Jul 30, 2012
Posts: 7
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!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40

Please don't cross post. It wastes people time and effort..... Your other topic has been deleted.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40

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

Joined: Jul 30, 2012
Posts: 7
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

Joined: Jul 30, 2012
Posts: 7
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Accessing files on classpath using parent directory sign (../)