• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Rancher
Posts: 1049
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 14282
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 27370
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14282
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
Saloon Keeper
Posts: 26020
186
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
This will take every ounce of my mental strength! All for a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic