How to access a folder above a running jar - java

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

Related

Get relative path from jar file

I tried to reach a special path in Ubuntu, relative to the current jar file.
In Windows it is working without any problem:
String jarPath = Configuration.class.getProtectionDomain().getCodeSource().getLocation().getPath();
File f = new File(jarPath+"/../../configurationFile.xml");
However, in Linux I always get the jar file but I cannot step back two directories to the configurationFile.xml
/some/directory/where/xml/is/located/xyz.jar/configurationFile.xml: Not a directory
However, if I do
pwd /some/directory/where/xml/is/located/xyz.jar/../../
it works without any problems.
What I am doing wrong here?
I cannot figure it out.
Use only directories in your path.
After you determined the path to your jar file, extract the path to its directory and use directories only.

Load a file outside of a Jar

I have a folder called lib that contains all my Jar files and in one of the Jar files class, I have a main method which is called by a batch file. In the same folder location as my lib, I have another folder structure path/to/a/resource/myresource.txt
How can I load this file from a class inside the Jar file? I tried the following and both resulted in null:
getClass().getResource("path/to/a/resource/myresource.txt")
getClass().getClassLoader().getResource("path/to/a/resource/myresource.txt")
Any ideas? Even with an absolute path, it failed! Any suggestions?
You can use:
getClass().getResourceAsStream("path/to/a/resource/myresource.txt")
However, for this to work, you need to add the path '.' to the Class-Path entry of the JAR's MANIFEST.MF file.
http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
Two things you tried are used to read files from class-path since this folder is not on your classpath you can read it directly with any of the java File IO classes.
File file = new File("C:/folder/myFile.txt");
or if you know the relative path:
File file = new File("../../path/myFile.txt");
Your path seems not to be precise enough. Further, this question has been worked before.
Have a look here:
How to Load File Outside of, but Relative to, the JAR?
How to get the path of a running JAR file?
You can either load the file from file system
new FileReader(relativeOrAbsoluteFilesystemLocation)
or you can add the directory in question to your classpath:
java -cp "lib/*;lib" ...
and then use your original method.
(Unix uses : rather than ; as classpath separator)

Java- relative path of text file in main?

I was wondering how to specifiy a relative path name to a text file that is stored in my src folder of a java project. I would like to store a string with the path of this text file. For example if I had example.txt located in the src folder of my java project how would I go about finding its relative path? I am also doing this in my main so I'm having trouble using .getResource(). How would I do this? Thanks.
My files path is as followed from the properties in eclipse
/MyProject/src/data.txt
I've tried:
String path = "/MyProject/src/data.txt";
But that doesn't work?
If you are using eclipse, place your text file in the root of the project folder, outside the /src and /bin folders. It should be now accessible via a relative path directly.
If you want to access a file in src folder, you have to append the /src/ prefix before the file path/name
Use the src/ prefix before the file path/name.
String path = "src/data.txt";
The path is relative to the directory you execute the "java" command.e.g.
/opt/projects/myproject>$ java -cp <whatever your classpath is that contains your class files> com.mycompany.mypackage.MyJavaClass
in this case the MyJavaClass would find files relative to the directory
/opt/projects/myproject
There is another way to do this if you like: you can use load resources, which are found via the classpath mechanism.
getClass().getResource("foo.txt");
You can see this posting for more info
Preferred way of loading resources in Java

Read a file places in same dir as class?

reader = new CSVReader(new FileReader("file.txt"));
The file is placed in the same directory as the java class. Anyhow I get FileNotFoundException.
What is wrong?
Relative paths are relative to the current working directory. In your code sample, if file.txt isn't in your current directory, it won't be found.
Be wary of using relative paths in your code. That's because it's impossible to tell at compile time what the current working directory will be when your code is run.
If the file is part of your deployment, store it on the classpath and access it via ClassLoader.getResourceAsStream(), if it's truly external data that the user can change, put the file name in a configuration of some sort.
File needs to be in the root folder of the project.
Your application doesn't run in that directory. You would either have to use an absolute (or classpath-relative) path, or move the file to the directory where the application runs. In Eclipse, for example, this would be the root folder of your project.

FileNotFoundException in Netbeans

I have a java application project in Netbeans. I have just one class.
I try to do this
FileReader fr = new FileReader("sal.html");
I have the file sal.html under the same package. But I get this error when I run:
Errorjava.io.FileNotFoundException: sal.html (The system cannot find the file specified)
My guess is that Netbeans is invoking the JVM from your project's root folder. Quoting a portion of the File Javadoc:
By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.
To verify relative path resolution you could try:
System.out.println(System.getProperty("user.dir"));
System.out.println(new File("sal.html").getAbsolutePath());
You could then move your file to wherever java is looking for it. Most probably your project's root folder.
You could also consider using the class loader to read files as resources inside packages using getClass().getResourceAsStream("sal.html");. This is the preferred way of accessing resources since you no longer have to worry about absolute vs. relative paths. If a resource is in your classpath, you can access it. See this answer for more.
Put your file to main project folder. Not to any sub folders like src, or bin etc. Then it will detect your file.
Click on file view in Netbeans. Move sal.html to the project folder. Such that you will see it like this
- JavaProject
+ build
+ lib
+ nbproject
+ src
+ build.xml
manifest.mf
sal.html
Now
FileReader fr = new FileReader("sal.html");
will work.
System.out.println(System.getProperty("user.dir"));
System.out.println(new File("sal.html").getAbsolutePath());
Then it will show where the JVM is retrieving the files from. Usually for linux in the /home/username/NetbeansProjects/ApplicationName/.
Put your resources or files to this path
I think your problem is in the relative path to the file. Try to declare FileReader with full path to file.
FileNotFoundException means file not found.
The build folder for the netbeans is different where there is no file sal.html.
Try using absolute path in place of using relative path.
This is not a "File not found" problem.
This is because each class hold its own resources (let it be file, image etc.) which can be accessed only through a resource loader statement which is as below:
InputStream in = this.getClass().getResourceAsStream("sal.html");
The only fix is that you will get an InputStream instead of a file.
Hope this helps.

Categories

Resources