This question already has answers here:
How to read text file from classpath in Java?
(17 answers)
Closed 3 years ago.
I want to read the configuration file outside the jar, how should I do?
I tried classloader.getResourceAsStream("config.xml") and succeeded while I am running the code inside Intellij. Now I want to build the jars to a folder and place the config.xml under the same folder, not inside the jar, but the program fails to detect the config.xml.
Is there a graceful way of reading the config.xml instead of using File with relative path in the code, which doesn't work while debugging/running inside the IDE?
Yes, turn it into a system property, and provide it to anything running any Java process/application.
Let's say you have a config.xml file located inside /some/path/down/the/line/, then you can do: java --classpath ... -Dapp.config=/some/path/down/the/line/config.xml tld.domain.Application.
Then all you have to do in your Java code is to reference that name/path: final String configFile = System.getProperty("app.config");, and use any well-known routine to read it from there.
Basically, you have to make sure the file/path/location is provided somehow to the Java classpath.
Related
This question already has answers here:
Reading a resource file from within jar
(15 answers)
Closed 2 years ago.
I made a javafx project, when I finished I made a executable jar file. When I run it I get this error:
java.io.FileNotFoundException: src\Radiatorlista.txt
This is how my code looks like:
File radiatorok = new File("src/Radiatorlista.txt");
I know that this path will not work. I don't know how to specify the correct relative path.
This is how my jar file looks like.
The answer is that you need to put your txt files (or whatever you wan't to open) in the folder where your jar file is. For example my jar file is here:
F:/Example/for/my/file
You need to put your files in the file folder.
Then change your code like this:
File file = new File("Mytxt.txt");
After this your program should work.
This question already has answers here:
How to get the current working directory in Java?
(26 answers)
Closed 3 years ago.
Part of my (java) code needs to access a database. When opening a connection it checks if the actual database file exists (I use sqlite). I want to make my code portable, so I want to avoid hard coding the path of the database file. Is there anyway in java to get the path of the .java file? Because I know exactly where the database is from the .java file accessing it.
I've tried using current directory with File but it doesn't give me the path of the actual .java file. When I use android studio the current directory is different than when I simply use a terminal.
The best way to get the path information would be using Paths and Path class from the java.nio. You can input an absolute path or relative path to the Paths.get(String str) to get the Path.
To get the project directory, you can use:
Paths.get(System.getProperty(“user.dir”))
Paths.get(“”)
It will get the complete absolute path from where your application was initialized.
The Paths.get() method will return a Path Object, on which you can call toString() or toUri() to get the path. Hope this helps.
Javadocs - Paths
This question already has answers here:
How to add resources to classpath
(3 answers)
What is a classpath and how do I set it?
(10 answers)
Closed 5 years ago.
I'm on a new branch of a git project on which I've made my own config file called MY_NAME.conf which is located in
Users/myname/IdeasProject/projectName/src/main/resources/config.
However, I'm having some issues getting my branch to run. So, just to make sure things are smooth I set up a new environment and I've cloned into the master. This is all done at
Users/myproject
However, now when I try to run the project I get a message
java.io.IOException: resource not found on classpath:
config/MY_NAME.conf
Why is it looking for this file at all? And even if it is, why is it not finding MY_NAME.conf?
Consider setting the classpath through one of these recommended techniques. Without knowing the specifics of your runtime environment, there should be a mechanism provided to include src/main/resources in your java classpath.
I think the issue is that classpath doesnt include conf folder.
You can modify/set class path with: set CLASSPATH=path1;path2
This question already has answers here:
Read properties file outside JAR file
(8 answers)
Closed 6 years ago.
As stated in spring-boot-reference:
On your application classpath (e.g. inside your jar) you can have an
application.properties that provides a sensible default property value
for name. When running in a new environment, an application.properties
can be provided outside of your jar that overrides the name
I place a duplicated application.properties with overridden name on the same path as jar file, however when running the application with:
java -jar target/myproject-0.0.1-SNAPSHOT.jar
The name value is not overridden, it still refers to the one inside application.properties inside jar file. I also tried:
java -Dspring.config.location=/target/application.properties -jar target/myproject-0.0.1-SNAPSHOT.jar
But it does not work, please help.
Edit
When I change the current directory to target and run it, it works.
java -jar myproject-0.0.1-SNAPSHOT.jar
Why? Why cannot be outside the path and run it?
It doesn't work because you are trying to launch the jar from another folder: spring boot looks for files/folder relative your current folder.
You can:
1) copy application.properties either in ./ or ./config/, relative to your current folder.
2) Or specify -Dspring.config.location:
$ java -Dspring.config.location=target/application.properties -jar target/myproject-0.0.1-SNAPSHOT.jar
You spelt config as conig, should work if you spell it right.
This question already has answers here:
Java resource as File
(6 answers)
Closed 9 years ago.
I have a jar file, say archive1.jar, resides in /home/ext/libs. This has a couple of classes, and also a text file (data.txt) at the root of the hierarchy of the jar. One of the classes of this jar (say Data.java) reads the file to initiate some internal data structure.
Now, the archive1.jar is included as a library of another program, called Parsing, which is executed in a completely different directory /home/progs (archive1.jar is specified in the classpath of the execution command of the program).
The problem I am facing is that now Data.java cannot understand the path of data.txt anymore, because it assumes that data.txt resides in /home/progs instead of being inside archive1.jar in /home/ext/libs.
My question is what change should I make inside Data.java in order for it to understand that the data.txt is inside the archive1.jar? In short, how to make Data.java be able to read data.txt inside archive1.jar, no matter where the calling program is.
Note: In thread Java resource as file, thing was a bit different, and also there was no answer to how to read the file inside the jar. One said it might be impossible.
The way to access resource files inside JAR archives is by using getClass().getResourceAsStream() . Using this ensures that the resource is loaded from the archive not from the filesystem. For example if you have data.txt in the same location as Data.java within the JAR archive, then you would access the data.txt in this way
InputStream is=getClass().getResourceAsStream("data.txt");
Once you have got an InputStream to the resource proceed with the way you like.