Open Resource with Relative Path in Java

open resource with relative path in Java

Supply the path relative to the classloader, not the class you're getting the loader from. For instance:

resourcesloader.class.getClassLoader().getResource("package1/resources/repository/SSL-Key/cert.jks").toString();

Java relative path start from my src folder

I see you are asking about the relative path but seems like you want to just read the file from resources, I believe the most common way to read resources is by using the class loader, example:

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("filefolder/file.xml").getFile());

How to read file from relative path in Java project? java.io.File cannot find the path specified

If it's already in the classpath, then just obtain it from the classpath instead of from the disk file system. Don't fiddle with relative paths in java.io.File. They are dependent on the current working directory over which you have totally no control from inside the Java code.

Assuming that ListStopWords.txt is in the same package as your FileLoader class, then do:

URL url = getClass().getResource("ListStopWords.txt");
File file = new File(url.getPath());

Or if all you're ultimately after is actually an InputStream of it:

InputStream input = getClass().getResourceAsStream("ListStopWords.txt");

This is certainly preferred over creating a new File() because the url may not necessarily represent a disk file system path, but it could also represent virtual file system path (which may happen when the JAR is expanded into memory instead of into a temp folder on disk file system) or even a network path which are both not per definition digestable by File constructor.

If the file is -as the package name hints- is actually a fullworthy properties file (containing key=value lines) with just the "wrong" extension, then you could feed the InputStream immediately to the load() method.

Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("ListStopWords.txt"));

Note: when you're trying to access it from inside static context, then use FileLoader.class (or whatever YourClass.class) instead of getClass() in above examples.

Load file from resources by relative path (Spring)

As file - file.txt is in resources directory, normally this would be copied to class-path by build process(tool like Maven, Gradle). And this would easy with to load file with relative path.

This thread had extensively talked about how to load file from class-path.
How to really read text file from classpath in Java

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();

Issue with relative paths of resources files in an executable jar using Maven

The error you have is related to the loading of the properties file, but I think you will get an other one with the dictionnary loading.

  1. For the properties file:

    JWNL.initialize(ClassLoader.getSystemResourceAsStream("properties.xml"));

Be sure that your file is located at the root of your jar. That should be the case if your source file is in src/main/resources as it is the default maven configuration. If your properties file is in a "resources" folder in your jar as you said, this become :

JWNL.initialize(ClassLoader.getSystemResourceAsStream("resources/properties.xml"));

  1. For the dictionnary:
    The API is not designed to read dictionnary files inside the jar. In edu.mit.jwi.data.FileProvider at line 363, you would see:

    File directory = toFile(url);
    if (!directory.exists())
    throw new IOException("Dictionary directory does not exist: " + directory);

    so you have to keep them outside your jar, in the filesystem :

    dict = new Dictionary(new File("WordNet-3.0\\dict"));
    dict.open();

Note:
Of course, putting a hard coded absolute path in your code would be a bad idea.

You could put the dictionnary directory in the same directory as your jar, if your jar is always run from this directory, and access it with a relative file path in your code as above (see How does Java resolve a relative path in new File()?)

Or you could let the user configure the directory path with a command line arguments (https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html) or an environnement variable (https://docs.oracle.com/javase/tutorial/essential/environment/env.html)

Example with an environnement variable:

  • user define an environnement variable, for exemple :

    set WORD_NET_DICTIONARY = "C:\WordNet-3.0\Dict"
  • you can get this env variable in your code and configure your library:

    String wordNetDicDir = System.getenv("WORD_NET_DICTIONARY");
    if (wordNetDicDir != null) {
    dict = new Dictionary(new File(wordNetDicDir));
    dict.open();
    } else {
    // throw exception or log error
    ...
    }

Relative resource paths in Java project in IntelliJ IDEA

I've encountered this issue many times and what worked for me was using InputStream

InputStream is = Main.class.getClassLoader().getResourceAsStream("name_of_file.png");

Using InputStream will allow you read from various file types. Now to load in the icon you can do

Icon icon = new ImageIcon(ImageIO.read(is));

How does Java resolve a relative path in new File()?

There is a concept of a working directory.

This directory is represented by a . (dot).

In relative paths, everything else is relative to it.

Simply put the . (the working directory) is where you run your program.

In some cases the working directory can be changed but in general this is

what the dot represents. I think this is C:\JavaForTesters\ in your case.

So test\..\test.txt means: the sub-directory test

in my working directory, then one level up, then the

file test.txt. This is basically the same as just test.txt.

For more details check here.

http://docs.oracle.com/javase/7/docs/api/java/io/File.html

http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html



Related Topics



Leave a reply



Submit