weblogic classloading binary resource - java

I'm trying to use a binary resource in my application. I need it for crypt/decript passwords. The file is located in the "classes" folder of my ear, just like properties files that the application access without problems.
Anyway, I am loading the files as follows.
ClassLoader.class.getResourceAsStrem(/file.xxxx);
But in the case of the binary file the resultant InputStream is null.
The question is, why it works with properties files and does not with a binary file? What can i do for load a binary resource?
Best regards.

Ensure that the file is available on the class path.
Use the context class loader instead - this will ensure that the code trying to look up the file can reside in a shared library as well.
Something on the lines of:
Thread.currentThread().getContextClassLoader().getResourceAsStream(..)
Hope that helps.

Instead of ClassLoader use the class name where you are currently in.
YourClass.class.getResourceAsStrem(/file.xxxx);

Related

Find resource in separate folder to class file

I'm trying to find a txt file in a separate folder to the class
Class file
C://workspace/project/src/pkg/Class.java
txt file
C://workspace/project/doc/pkg/myFile.txt
I'm trying to find the text file without having to hard code the C://workspace/project/ bit
Is this possible?
Currently I can use a classpath:/pkg/myFile.txt when the file is in the same package as Class.java using a resource loader
You could inclide the doc folder as a source folder. That way you can keep your resources separate from the code, but still have access to it using a classloader.
Of course that will only work for you when your resource can be part of the jar. If not, you may want to consider using a properties where you can configure the complete path to the resource.
Try getClass().getResource() or getClass().getClassLoader().getResource() if it is in the same jar-file

getting null pointer when reading the properties file

when reading the properties file, iam getting nullpointer exception.
faceConfig.load(ReadPropertyFile.class.getClassLoader().getResourceAsStream("/resources/faces.properties"));
below is the path of properties file facedetections/src/main/resources/faces.properties
i tried in different combinations as my class file that reads in below path /facedetections/src/main/java/com/facial/facedetection/utils/ReadPropertyFile.java
combinations are ../../../../../resource/faces.properties , /resource/faces.properties and
../../../resource/faces.properties
Please suggest what is the correct path i can provide for this.
Edit :
I extracted the war file and providing its path below.
looks your property file under class folder not in resource.
as your screen shot cant find resource folder under class folder.
So just use
ReadPropertyFile.class.getClassLoader().getResourceAsStream("faces.properties")
The path is relative to the point where the object (.class) files are located. Are you sure you have configured your build/test tool to copy the resource file into that structure? Exactly where? That's what counts, not the position of the sources.
Additionally, my understanding is that getResourceAsStream() of most ClassLoaders do not support .. notation.
The position where your resource file is currently located is out of reach of the ClassLoader. If you move your file to /facedetections/src/main/java/resources/faces.properties, then you will be able to use the current code
getResourceAsStream("resources/faces.properties")
I'm making assumptions about your environment. In particular, this is entirely dependent on classloaders. If this doesn't help, please provide object file location, not sources (unless it is the same, but state it).
Since it is unlikely that you get a NPE when the file is not found I assume that faceConfig is null when you execute that line.

How to use ClassLoader.getResources() in jar file

Problem statement:
I have a jar file with a set of configuration files in a package mycompany/configuration/file/.
I don't know the file names.
My intention is to load the file names at runtime from the jar file in which they are packaged and then use it in my application.
As far as I understood:
When I use the ClassLoader.getResources("mycompany/configuration/file/") I should be getting all the configuration files as URLs.
However, this is what is happening:
I get one URL object with URL like jar:file:/C:/myJarName.jar!mycompany/configuration/file/
Could you please let me know what I am doing wrong ?
For what you are trying to do I don't think it is possible.
getResource and getResources are about finding named resources within the classloader's classpath, not listing values beneath a directory or folder.
So for example ClassLoader.getResources("mycompany/configuration/file/Config.cfg") would return all the files named Config.cfg that existed in the mycompany/configuration/file path within the class loader's class path (I find this especially useful for loading version information personally).
In your case I think you might almost have half a solution. The URL you are getting back contains the source jar file (jar:file:/C:/myJarName.jar). You could use this information to crack open the jar file a read a listing of the entries, filtering those entries whose name starts with "mycompany/configuration/file/".
From there, you could then fall back on the getResource method to load a reference to each one (now that you have the name and path)

ClassLoader loading the wrong instance of a file

So I see there has already been a post very similar to this issue, however I am in a situation where I have no power to specify the location of this file within my jar and so am hoping someone is aware of a solution to get around this.
So I currently use the following snippet to obtain a file as an input stream, the file 'plugin.xml' is located at the root of the jar and I cannot change this location as another piece of software (dynatrace) creates this file and determines its location.
the standard snippet:
InputStream is = JmxPlugin.class.getResourceAsStream("/plugin.xml");
Now I am aware that the issue is that the ClassLoader is picking up the first file which matches the name 'Plugin.xml' along the classpath (one which isn't in my jar, yay).
Can anyone think of a way to ensure I pick up the correct file without having to move it? The relative path of my class in the jar is com/something/jmx/JmxPlugin.class.
(Id rather not have to unpack the jar in memory).
Many thanks for any contributions,
I'm not absolutely sure, but seems like Thread.currentThread().getContextClassLoader().getResourceAsStream("/plugin.xml") may solve your issue. If not, you'll have to create your own ClassLoader and resolve the issue there.
The simplies way is to move your jar in classpath to be the first containing Plugin.xml,
Another approach is to use getResource() to locate your jar file:
URL myJar=JmxPlugin.class.getResource("/"+JmxPlugin.class.getName().replace(".","/")+".class");
then use this URL to open jar file and extract Plugin.xml.

Load Java class file on the fly

In one of my ongoing application I want to load java class file to the application when it runs. This particular java file is generated by the user and I don't know the name of that file. but I know the structure of that file (methods, variables , etc.) and location of that file. I know how to load java file using ClassLoader but for this instance it doesn't useful since I don't know the file name. so how do I load java file to my application and access it's method (These user generated java files are stored in one folder so it is ok to load all the java files at once)
Thanks!
Use an URLClassLoader.
If you know the directory the file is in, you can search it to find the class and use the ClassLoader to load it.
It appears you need to create your own ClassLoader.
Try reading this:
http://onjava.com/pub/a/onjava/2005/01/26/classloading.html
http://www.ibm.com/developerworks/java/library/j-dyn0429/

Categories

Resources