• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
John Coss
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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!
 
Henry Wong
author
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic