• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get relative path of a resource in the project using only the resource name  RSS feed

 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone ! I need to get the relative path of a resource in the project while knowing only the resource name .I am looking for something that can dynamically look in my project file structure and find the resource with the name i specified . Ultimately i need to create a file object using the relative path .


Any suggestion is much appreciated .
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know it this will do you any good or not, but take a look here: Resource Bundle API
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand your use case, or what you mean.

Resources are supposed to be static, and their location is supposed to be known ahead of time.
 
Anurag Verma
Ranch Hand
Posts: 170
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For this you need to write a code to scan all your classpath (File system and jar files too) for matching resourcename. There is a code at github that you can refer which scans Interfaces with an annotation. you can alter it as per your need.

https://github.com/vermaanuragmaxima/wscreator/blob/adf0b5bab5f0a7768d4fd80ce72718c0aa36e1d5/src/main/java/com/maximaconsulting/webservices/WebServicesScanner.java
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I don't understand your use case, or what you mean.

Resources are supposed to be static, and their location is supposed to be known ahead of time.


It's for test automation , anyone can know the format of the name representing future resources but their location in the projects may not be known yet . Aka i have resources now that represent opportunity table data (in a opportunity table folder) but later on i may have something else. If i require only resource name inside my method i can write code now that will not need modification.

 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't get it. Why do you need to write tests for design decisions which haven't been made yet?
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote:It's for test automation , anyone can know the format of the name representing future resources but their location in the projects may not be known yet . Aka i have resources now that represent opportunity table data (in a opportunity table folder) but later on i may have something else. If i require only resource name inside my method i can write code now that will not need modification.


You'd still need to retest and repackage your build. I also think that code that dynamically looks for resources runs the risk of "resource hell", where you get weird bugs because you're locating the wrong version of resources.

If you really want to do this, you can probably use this library, but I don't recommend going this way: https://code.google.com/p/reflections/
 
Tim Holloway
Saloon Keeper
Posts: 18797
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not all resources have corresponding files. And resources in a JAR file don't have physical files, since the physical file is the JAR file itself.
 
sal jefferson
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to know what directory the resource is in. So for example, if there is a directory called "resources" on the users machine you can get the root directory of the user, and the root directory of the resource like this:


Then, all you need to know is the resource file name and you can resolve the Path to that file like this:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!