Relative path Not works - java

I have a folder structure like:
myDir
resources
myConfig.cfg
in
infile.txt
myProgram.jar -->runnable jar file
I tried to use the relative path ./in/infile.txt
I'm mentioning these relative paths from myConfig.cfg.
In code, i use like,
Properties properties = new Properties();
properties.load(new FileInputStream("./resources/myConfig.cfg"));
What is the error ?
The same worked for another jar and the same path.

Try this
properties.load(this.getClass().getResourceAsStream("/myConfig.cfg"));
Actually when compiled resource folder is not used but all the resource files are moved under "classes" dir

Related

ClassLoader.getResource returning null while using File.getAbsolutePath

I have a config.ini file I need to open that is quite far back in directory so I used File.getAbsolutePath() to set the base directory and concatenated the remainder of the path.
Printing the path, I get the correct path that I can paste into file explorer, but the object returned is null.
So I started by initializing my Properties and ClassLoader as such:
Properties prop = new Properties();
ClassLoader classLoader = Test.class.getClassLoader();
Then I create the path. I tried escaping a back slash(1) as well as forward slash(2), both return null but both paths work in file explorer.
String absPath = new File("").getAbsolutePath();
absPath = absPath.concat("\\resources\\config\\config.ini"); // (1)
absPath = absPath.concat("/resources/config/config.ini"); // (2)
then I try to set the URL to open an InputStream
URL res = Objects.requireNonNull(classLoader.getResource(absPath), "Unable to open config.ini");
InputStream is = new FileInputStream(res.getFile());
However, the following returns null.
classLoader.getResource(absPath)
I expected this to properly open the file because the path was correct. I am using Intelij and I read that I needed to add the .ini resource file under settings > compiler, which I did but that did not resolve my issue.
Thank you!
That's not the way to load resources via class-loaders.
If your classpath is something like the following ...
java -cp resources;lib/my.jar ... org.mypack.MyClass
then you load it with this path
getClassLoader().getResource("/config/config.ini");
Your classpath includes the resources folder, and the class-loader loads from there.
The absolute path of the OS is quite certainly not in classpath.
In any case, you must be certain that the resource folder is in classpath.
If your config file is not in classpath then you can't use classloaders to load that file.
Just one more thing, if your configuration is not in classpath, but in a child directory of your working dir, why can't you simply use new FileInputStream("resources/config/config.ini");

Trouble reading/writing file from relative path

I've been trying to read and write to a file under my resources directory in my project. However, regardless of what I seem to do it doesn't allow me to do so.
This is my project hierarchy for reference:
Out of all these:
Paths.get("memes.txt")
Paths.get("resources/memes.txt")
Paths.get("/resources/memes.txt")
...
None have worked. What am I doing wrong?
Is it a Maven project ?
Try this :
//Get file from resources folder
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
Ref : https://www.mkyong.com/java/java-read-a-file-from-resources-folder/
Your application can be packed as .jar file - a zip format.
There, or in the built classes directory should be memes.txt. A so-called resource, and in case of a jar not really a file system file. But is located on the class path.
URL url = getClass().getResource("/memes.txt");
InputStream in = getClass().getResourceAsStream("/memes.txt");
The path is relative to the package directory of the class, or absolute as above.
Try it using "../resources/memes.txt" with ".." you go up one directory from your current file (i assume that you are using the code in SwearTest.java)

Java can't read file except when in project root directory

I am using IDEA 14 to follow a simple Java tutorial (JDBC). As part of this, I am storing some configuration properties in a file called jdbcTutorial.properties. When I put this in the root directory of the project, I can read it with the following:
Properties props = new Properties();
props.load(new FileInputStream("jdbcTutorial.properties"));
However, as soon as I move it to any other directory in the project, I get the error "No such file or directory". This happens regardless of whether I specify a relative or absolute path:
Maybe there are more standard ways to use config files, but I would really like to understand the behavior I am observing. Thanks for helping!
By default root directory would be added to your project's build path. Since the directory in which you are putting the file is not added in your project's build path jvm is unable to find the file. You have two options:
Add the folder where you are putting your prop to build path.
Access the file with full path i.e. /home/user/workspace/....
When you build a project, IDEA takes the files in the resources directory and puts them in the executable jar. So to get an input stream from that file, you need to get it directly from inside the jar. Instead of FileInputStream, use
getClass().getResourceAsStream("jdbcTutorial.properties")
When no path is supplied for a file, java assumes that this file is in the project's root folder. So the relative path "." always points to this folder. When the file is somewhere else, put the appropriate relative path before the path name, like "./files/configuration/jdbcTutorial.properties".
It work also when you put an absolute file path before the path name, like "C:/Users/Me/Documents/Java/workspace/thisProject/files/configuration/jdbcTutorial.properties".

How to load properties from resources folder?

I've been able to get the following to work by loading a properties file which exists in the same folder as the class files (the bin folder)
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("config.properties");
I want to load the config.properties file from a resources folder which is at the same level as the bin folder. If I pass it "resources/config.properties" it finds the file in /bin/resources, but passing it "/resources/config.properties does not look above the bin folder like I want.
Ideas?
Try to use Properties like this:
Properties props = new Properties();
InputStream resourceStream = Your_Class_Name.class.getResourceAsStream("config.properties"))
props.load(resourceStream);
class.getResourceAsStream method loads from jars and folders defined in your classpath. If you have that config.properties file under resources folder, you should add that folder in your classpath.
Something like
java -classpath "resources" ...

How to access a folder above a running jar

Background:
i have a running .jar called main.jar in the location C:\Program Files\folder\files\bin\main.jar on Windows. I also have a properties file in the same location named p.properties. At the same time, i have a .png file at C:\Program Files\folder\files\graphic\img.png.
My Question:
how do i access the p.properties file from the .jar, compared to accessing the img.png file. i believe that i can just use new File("p.properties"); from the .jar, but how would i access the img file? it is in a different folder, and i've tried new File("graphic/img.png"); but that didnt work. how would i do this? and am i correct in accessing the properties file?
Thanks in advance!!!!
In a relative path, .. refers to the parent directory. For example,
new File("../graphic/img.png");
By the way, these paths are relative to the program's current working directory, and it might not be the path where your .jar file is located. To find the path where your jar is located you can use:
File jarFilePath = getClass().getProtectionDomain()
.getCodeSource().getLocation().getPath()

Categories

Resources