load file within a jar - java

I need to package a configuration file within a jar. the configuration file is under the root of the jar file.
However I got the following error:
Caused by: java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.(Unknown Source)
File url = new File(MyClass.class.getClassLoader().getResource("my.conf").toURI());

You should use getResourceAsStream() instead. If the file is embedded in your JAR the URI is most likely bundle:// URI
InputStream is = this.getClass().getResourceAsStream("my.conf");

Why do you need a file? IF you need to read the config use
Class.getResourceAsStream("/my.conf");
This will need only to be the file in the one folder with the root of your package( the same as in the root of the jar)

The file should be in the same package as the MyClass. I just realized you are creating a File object. Instead try using getResourceAsStream(). This is the right way if you want to read the contents from a classpath resource. Here is the example.

Related

How to get the path of .key file from spring application programmatically?

I need to get the path of a key file that i have placed in the root folder of my spring application. Everything works as expected when i run it locally. But when i deploy the application to the server i get a FileNotFoundException.
File file = new File("testfile.key");
String path = file.getAbsolutePath();
I have tried placing the file in the resource folder as well.
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
File file = new File(classLoader.getResource("testfile.key").getFile());
Just need to pass the file path to another method (3rd party library) which will read the content.
Any help would be much appreciated.
This should be a resource, placed in the resource folder (if using maven).
You can access it using
this.getClass().getResource("testfile.key");
The root for accessing files can change between environments but the root for resources is a directory that's specified during compilation. For maven driven projects this is:
src/main/resources

getResourceAsStream path related to project stucture?

I know this should be simple, but surprisingly I can't google an answer.
I have this structure :
Myproj
|-src
|--com.mypackagename
|---MyClass.java
|-xml
|--book.xml
XMLInputFactory factory = XMLInputFactory.newInstance();
ClassLoader cl = MyClass.class.getClassLoader();
XMLStreamReader reader = factory.createXMLStreamReader(cl.getResourceAsStream("xml/book.xml"));
and it doesn't find my xml. Obviously, the path is wrong. Please, help me - how shall i set it right
The problem is that getResourceAsStream() will only load resources from the classpath. And as per given directory your xml file is not located on classpath. So, place your xml file under WEB-INF/classes/book.xml and access it as:
getResourceAsStream("book.xml")
If you book.xml file is kept in class path then you can simply access it by writing cl.getResourceAsStream("book.xml").
Assuming that the xml directory is not within the classpath context of your application (ie embedded within the application Jar or with the classpath property), then you will need to reference the XML file as a File. The path to which will be relative to the execution location of the program.
If the xml directory is within the classpath context (ie classpath=.\xml;...), then you will need to use a path something like /book.xml.
If the xml directory is relative to the classpath context (ie classpath=.;...), then you will need to use a path something like /xm/book.xml.
If the xml directory is embedded within the application context (packaged within the Jar), then you will need use either /book.xml or /xml/book.xml depending on how the directory is packaged

"URI is not hierarchical" when loading resource

I need the URI of a file (which I put in the resources directory). If I use
MyClass.class.getClassLoader().getResource(resource)
I get
java.lang.IllegalArgumentException: URI is not hierarchical
Otherwise, if I use ClassLoader.getSystemResource(resource) it returns null.
Are you loading the file from inside a jar? If so, the OS is unable to form a java File instance from inside a jar. To be able to load it, try open it as a Stream. "filepath" should start with a "/".
MyClass.class.getClass().getResourceAsStream( filepath );
You should be using
getResourceAsStream(...);
when the resource is bundled as a jar/war or any other single file package for that matter.
See the thing is, a jar is a single file (kind of like a zip file) holding lots of files together. From Os's pov, its a single file and if you want to access a part of the file(your image file) you must use it as a stream.

How to load property file located inside jar file

How to load property files placed in resource folder of an executable jar file. Here my app itself is a jar and it executes on it own. It need to find this property file (placed within itself under resource folder) at runtime depending on the path mentioned in the code. I have used below two methods but it didn't help me. Point here is, both these options are working fine when i execute in eclipse, but doesn't work when I pack it into an executable jar. It throws NullPointerException. Only problem I see here is that jar is not able to pick the property files with given path. Any help would be appreciated.
Method 1: Using Apache Commons Configuration
URL propFileURL = XYZ.class.getClassLoader().getResource("/config.properties");
Configuration propertyConfiguration = null;
propertyConfiguration = new PropertiesConfiguration(propFileURL);
In above case I'm getting ConfigurationException. Class is not able to find file mentioned in given path.
Method 2: Using getResourceAsStream. I know that getResource doesn't work if we are to load files from network on in any other location.
InputStream is =XYZ.class.getClassLoader().getResourceAsStream("/config.properties");
Properties prop = new Properties();
prop.load(is);
In this case, I'm getting nullPointerException.
Let me know if you need more details.
jar content Heirarchy
Properties file - /java-file-io-application/src/main/resources/config.properties
XYZ class - /java-file-io-application/src/main/java/org/bc/xyz/iplugin/utilities/XYZ.java
Looks like you might be building your jar incorrectly. Files from 'src/main/resources' would be expected at the root of the jar file. If your jar file contains the 'src/main/resources' directory, something's off with your build.

How can i find the path of my file in my Java project file?

I put a file inside my Java project file and i want to read it but how can i find the path name with Java.
Here i put it in C driver but i just want to find path by just writing the name of file. Is there a function for it?
FileInputStream fstream1 = new FileInputStream("C:/en-GB.dic");
If the file is inside the jar file generated for your project (or in the classpath used by your project, generally), under the package com.foo.bar, you can load it using
SomeClassOfYourProject.class.getResourceAsStream("/com/foo/bar/en-GB.dic");
If it's not in the classpath, and you launch the application (using java.exe) from the directory c:\baz, and the file is under c:\baz\boom\, the you can load it using
new FileInputStream("boom/en-GB.dic");
Place it in your classpath, If it is a web application WEB-INF/classes/yourfile.ext, if it is a standalone application place it in bin directory of your application (default class directory is bin).
Then you could read by using one of the following ways.
InputStream in = this.getClass().getClassLoader().getResourceAsStream("yourfile.ext");
Or
InputStream in = this.getClass().getResourceAsStream("/yourfile.ext");
You can read online for the differences between above two approaches.

Categories