How to Get Resources Directory Path Programmatically

How to get resources directory path programmatically

I'm assuming the contents of src/main/resources/ is copied to WEB-INF/classes/ inside your .war at build time. If that is the case you can just do (substituting real values for the classname and the path being loaded).

URL sqlScriptUrl = MyServletContextListener.class
.getClassLoader().getResource("sql/script.sql");

How to get absolute path to file in /resources folder of your project

You can use ClassLoader.getResource method to get the correct resource.

URL res = getClass().getClassLoader().getResource("abc.txt");
File file = Paths.get(res.toURI()).toFile();
String absolutePath = file.getAbsolutePath();

OR

Although this may not work all the time, a simpler solution -

You can create a File object and use getAbsolutePath method:

File file = new File("resources/abc.txt");
String absolutePath = file.getAbsolutePath();

Get resources folder path c#

If the files are stored in your project folder, you can retrieve the files using
System.AppDomain.CurrentDomain.BaseDirectory.
This statement retrieves the path as to where your application is installed.
Click Here to get a detailed explanation on this.

Get resources folder path consistently across test and production code

Since your application writes data, I recommend that the variable containing the target directory path be maintained outside your project code. I suggest using an environment variable. If the path were stored in environment variable IMG_DIR, you could access this information from your Java code with System.getenv("IMG_DIR"). Having this configuration item external to your JAR makes it trivially simple to move the JAR to any execution environment.

How can I access a folder inside of a resource folder from inside my jar File?

Finally, I found the solution:

final String path = "sample/folder";
final File jarFile = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath());

if(jarFile.isFile()) { // Run with JAR file
final JarFile jar = new JarFile(jarFile);
final Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
while(entries.hasMoreElements()) {
final String name = entries.nextElement().getName();
if (name.startsWith(path + "/")) { //filter according to the path
System.out.println(name);
}
}
jar.close();
} else { // Run with IDE
final URL url = Launcher.class.getResource("/" + path);
if (url != null) {
try {
final File apps = new File(url.toURI());
for (File app : apps.listFiles()) {
System.out.println(app);
}
} catch (URISyntaxException ex) {
// never happens
}
}
}

The second block just work when you run the application on IDE (not with jar file), You can remove it if you don't like that.

How can I get file path on raw resources in Android Studio?

how can i get file path on raw resources?

You can't. There is no path. It is a file on your development machine. It is not a file on the device. Instead, it is an entry in the APK file that is your app on the device.

If your library supports an InputStream instead of a filesystem path, getResources().openRawResource() on a Context to get an InputStream on your raw resource.

How do I gain access to the files within the resources directory in my Maven web service project?

If you're using the classloader to get a resource in the classpath (resources folder), just use the relative path:

ReadCSVFiles.class.getClassLoader().getResource("csvfiles/data.csv");

Javadoc

Get path of a file in resourcer even when its in a jar

I had a similar error with reading properties file. After a bit of research I added this piece of code to pom:

<build>
<resources>
<resource>
<directory>src/main/resources/</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

And this is how I read from the file:

/**
* loads default config from internal source
*
* @return true/false upon success/failure of loading config
*/
private static boolean loadDefault() {

if (null != properties) return false;

try (InputStream resourceStream = Config.class.getClassLoader().getResourceAsStream("config.properties")) {
properties = new Properties();
properties.load(resourceStream);
return true;

} catch (NullPointerException | IOException exception) {

String detail = ExceptionHandler.format(exception, "Could not load default config");
log.error(detail);

properties = null;
return false;
}
}


Related Topics



Leave a reply



Submit