Get relative path from jar file - java

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.

Related

How to find the path of a file local eclipse project

I have created a .txt file in my Eclipse Java project, and I want to find out the path to it so I can use it for a Scanner. I do not want to find out the path on my local drive, as I will be planning to share the program to someone else, and they will have a different folder structure, rather a path that can be used on anybodies machine.
Here is the code:
this.file = new File("<insert path here>");
you can use :
= new File("Build Path"); (your .java file exist in your build path)
The build path is used for building your application. It contains all of your source files and all Java libraries that are required to compile the application.
In eclipse, the default behavior is for the Java system property user.dir to be set to the project directory. This is what dictates where the "root" of File operations is. So if you created a file test.txt in the root project directory, you should be able to access it with new File("test.txt").
However, as Andrew Thompson mentioned in his comment, the more correct method would be using embedded resources.
Try one of These:
1.
System.getProperty("user.dir");
2.
File currentDirFile = new File(".");
String helper = currentDirFile.getAbsolutePath();
String currentDir = helper.substring(0, helper.length() - currentDirFile.getCanonicalPath().length());//this line may need a try-catch
I have not tested it just found while googling

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

How to find path of my folder

We are working on project.
Every colleague have different folder for the install
In my case the folder of my files is in
C:\\p4_3202\\CAR\\car.rt.appl\\dev\\car.components\\cars\\res\\car.rt.components.cars\\resources\\js;
for other colleague it could be in
C:\\my_3202\\CAR2\\car.rt.appl\\dev\\car.components\\cars\\res\\car.rt.components.cars\\resources\\js;
it is depends how you config your perforce.
I need to read files from my folder but i don't know the name of the folder ( as i explained it could be different )
File folderFile = new File(folder);
How i can find the location of my folder ? ( c:\p4\......test.js )
I tried with
System.getProperty("sun.java.command");
System.getProperty("user.home")
but it didn't give me the path of my folder
I would use a system property for each user. So all users tell where perforce is installed (might already exist a property for this, look at the docs).
This could then be read by your code like:
System.getenv().get("PROP");
On a unix/Linux system you can set the property in a shell/environment variable using:
export PROP=thepath
Windows was a long time ago for me but if I remember correctly its somewhere under System on control panel :)
Update:
http://www.itechtalk.com/thread3595.html
file.getAbsolutePath() or file.getAbsoluteFile()
String path = new java.io.File(".").getCanonicalPath();
If it are files for reading only, being stored inside the final produced jar, then use URL url = getClass().getResource("/.../...") or InputStream in = getClass().getResourceAsStream("/.../...").
That uses a path inside the jar/class path. Using only the jar would never do with File.

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.

Categories

Resources