Jar cannot find the path to the file inside - java

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

Related

How to properly get resource Java project

In my project i currently have a setup using eclipse
But when i try to load the file "bg.png" by calling
getClass().getResource("/res/bg.png") or getClass().getResourceAsStream("/res/bg.png") I get a NPE
Can anyone tell me whats happening here? I never really thought there was much difference between how both methods locate their files
TIA
getClass().getResource[AsStream]() uses the class loader to load resources: the same mechanism as the one used to load class files based on the classpath.
So, to be able to load the resource, it must be in a jar file or under a directory that is part of the classpath. That is not the case here.
Move the res directory to the src directory: the file will then be in an Eclipse source directory, and Eclipse will "compile" it by simply copying the file to its bin/classes/whatever destination directory, which is in the classpath when running the application.

Accessing a file within Java project

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.

Eclipse project can not reference a file using relative paths

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.

Why does a runnable JAR exported from Eclipse continue to use old configuration settings?

I am exporting a runnable JAR from Eclipse. When run, it reads in a configuration file and based on this configuration file opens other files also for configuration purposes. However, when I move this JAR to another location it continues to try to use the values in the old configuration file, even though a new configuration file is present. Why does it do this?
The problem may be that the file is included in the Eclipse build path, so when it gets exported that file is built with the JAR. Then, regardless of whatever path is given for the configuration file on the command line, the JAR will use the file that was already packaged inside of itself. (This was my problem. Removing it from the build path resolved the issue.)

Classpath in a Java Web Start application

I need to load an .xml file in META-INF, it works when the application isn't sandboxed, but in Java Web Start a different classloader (which is more restricted) seems to be used, so the file found in myproj.jar/META-INF/myfile.xml isn't loaded. It however works if I put the file in the current directory of the loading class (I put it under com/blabla/myproj/whatever/META-INF/myfile.xml).
I couldn't find any classpath settings within the .jnlp file, but perhaps this can be done with a manifest? I don't know how they work, so if that's the solution please supply an example.
Stuff in META-INF should not be readable by code in your jar file, since that code should be agnostic to the fact that it's packaged in a JAR. Instead, since it is meta information (meta-inf) about the Jar itself, only the application that loads the jar file should access it.

Categories