I have a project which uses relative file paths to access resources. While the project is run in IDE, the path looks like.
\\src\\musicTest\\Track4O.mp3
When the project is built into a jar file, this no longer works. Upon opening the jar file, I noticed that the file structure looked like
musicTest.jar\\musicTest\\Track4O.mp3
In both cases, the class attempting to load the resource was in the same location as the mp3.
If anyone has a solution for either: maintaining the file path so that the build will run as intended or: solving the relative file path problem, it would be much appreciated.
Related
I wrote java code using REST which retrieves the employee details like id and name from a saved file. Absolute path is working fine while running the tomcat server, but once i provide relative path tomcat is not finding the file path. I'm using BufferedReader to read from the file.
Eclipse server plugin will compile the java sources into binary class files and put it into a work dir, so the relative path will not working. The quickest way to make it work is to put your files into the source directory. Then Eclipse server plugin will copy that file to the classes folder in your work directory. Then you can use relative path to read it.
I'm building a Groovy plugin for Android Studio which will execute some gradle tasks. However I need to load a XML file from within the JAR file from where the code executes.
The JAR file is as following:
com/packagename/code
META-INF/gradle-plugins
filename.xml
Because the xml file is at the root of the JAR I used this to load in the resource into an inputstream
new InputStreamReader(CheckStyleTask.class.getResourceAsStream("/filename.xml"))
The weird thing is that it only works sometimes. Sometimes it returns the file, sometimes it returns null. For example I took this line to check the path:
println(CheckStyleTask.class.getResource("/filename.xml").getPath())
And the path is completely right. So it is able to find the file with getResource, but with getResourceAsStream it returns null.
I think something is wrong with the building of the jar but in IntelliJ I set the gradle task as 'jar' which did create a working jar with working xml file.
Am I doing anything wrong regarding the building or is there anything wrong with the xml file?
There is a difference between the two functions:
https://stackoverflow.com/a/20069798/5387592
You should precise: in which circumstances, the results differ : after create new jar, or after launching again.
1 check if the file is really embedded in the jar: change .jar to .zip and check inside
2 check the real path.
In Eclipse for example, you put in a folder, but this folder doesn't appear in resulting jar
3 use advices: take absolute path as you do
In my Maven project I have a properties file that has a property for a location of keystore file file=filename.p12 (I think the file type doesn't really matter now).
The problem I have that when i built it with maven, I see that the file is inside the root of jar and when i run java -jar the-jar-file.jar I get the IO exception that the filename.p12 is not found.
Everything runs fine in Eclipse, it finds the file and the application runs. Not to confuse somebody, I keep a copy of that filename.p12 as well in src/main/resources folder so that the paths are resolved running in Eclipse and standalone. But this is going to be my other question.
What I can't do is to get the filename.p12 as a resource, because I have external jar that gets as argument my properties file and then handles that properties file itself where the row file=filename.p12 is. Why is the file not found inside the jar, even though I see it's there? My other property files that I have open with Spring's ClassPathResource run just fine.
In order to access internal/embedded resources you need to use Class#getResource or Class#getResourceAsStream depending on your needs
I have a Java project which uses a third party application. I have the license file (.lic format) stored in the resources folder. Upon running the Ant script, it will copy this file into the /lib/jar directory as it rolls up the project into a Jar file to use on the server. This is where I will need to access the file when running the system live. Here is how the folder structure looks
MyProject
src
package
AccessingClass.java
resources
File.lic
lib
jar
File.lic (upon copy from Ant)
I am not sure the best way to do this so any suggestions other than how I have been trying will probably be helpful. The 3rd party project has a method in a class like License.setLicense(), which can either take a String to the location or an InputStream of the file.
I have been playing around with feeding it an InputStream, but always get a null value when calling getClass().getResourceAsStream(). Here is everything I have tried:
getClass().getResourceAsStream("../../../lib/jar/File.lic");
getClass().getResourceAsStream("/File.lic");
And as a backup I also tried (for local builds I figure I would try the resource folder):
getClass().getResourceAsStream("../../../resources/File.lic");
getClass().getResourceAsStream("/File.lic");
Is there a better method to perform this action? Or would someone be able to tell me why what I am trying is failing? Thanks ahead of time.
Are you running this code standalone or in IDE env looks like classpath issue. If you are running at command prompt you have to set classpath to lib dir if in ide make sure you resources dir is in classpath.
First, you need to ensure that the JAR is added in your class path.
Below should work.
InputStream inputStream =
getClass().getClassLoader().getResourceAsStream("/resources/File.lic");
Assuming File.lic is placed in root folder of the jar.
My file is located under the src directory. However, when I try to call it using "src/readme.txt" the file is not found.
In fact, it states that java is looking for "C:\Documents and settings\john\My Documents\Downloads\eclipse-win32\eclipse\coolCarsProject\src\readme.txt".
How do I fix this? I do not want to put in the absolute path all the time.
Do I need to fix the classpath, buildpath, or change the project root, etc? It is not at all obvious from the roughly 1000 settings in Eclipse for a newbie.
First, you have to decide if you want to load the file from the file system, or if the file will in fact be bundled with your application code.
If the former, then you should really think about how your application will be launched when actually deployed, because using a relative file path means that the program should always be started from the same location: a relative path is relative to the location from where the program is started (the current directory). If this is really what you want, then edit your launch configuration in Eclipse, go to the Arguments tab, and set the working directory you want. But the src directory is not where you should put this file, since this will copy the file to the target directory, along with the classes, that will probably be put in a jar once you'll deploy the application.
If the latter, then you should not treat the file as a file, but as a resource that is loaded by the ClassLoader, using ClassLoader.getResourceAsStream() (or Class.getResourceAsStream()). Read the javadoc of those methods to understand the path to pass. If you put the file directly under src, it will be copied by Eclipse to the target directory, along with your classes, in the default package. And you should thus use SomeClass.class.getResourceAsStream("/readme.txt") to load it.
Using paths relative to the current working directory is not a good idea in general, as it's often quite hard to establish what your current working directory will be. In Eclipse, it will be your project folder (unless you set it to something different in your launch configuration), in webapps it will be the webapp's root directory, in a command line app it could be anything.
Try this one:
String filePath = ".\\userFiles\\data.json";
where «.\» is a root for the Eclipse project, «userFiles» is a folder with the user's files inside of Eclipse project. Since we are talking about Windows OS, we have to use «\» and not «/» like in Linux, but the «\» is the reserved symbol, so we have to type «\\» (double backslash) to get the desired result.