Classloader loading wrong log.properties file - java

I have EAR which contains multiple jars out of which two of the jar contains "log.properties"
For eg: In abc.jar, "log.properties" is in com.abc.test
and in xyz.jar, "log.properties" is in com.xyz.test
In both the package we have logger implementation which load "log.properties" as
this.getClass().getClassLoader().getResourceAsStream("log.properties");
Due to classloader which loads the other jar first it pickup his "log.properties"
I want to avoid this problem
Any suggestions ?

Use ClassLoader.getResources(String name) and write code to filter out URLs that are not in the same package as the class. Place that code in a utility or resource manager class and use it everywhere in your project.

Related

Can I have my application use my own implementation of a class instead of the class from a external jar

I have a situation where a external jar is loading a class which has a static block to initialise some configurations , i want to stop that happening . the only way i see is to extract that jar and remove that class file and write my own implementaion of that class. can this be done or is there any way i can stop that class from loading and load my own implementation
JAR file is a file format based on the popular ZIP file format and is used for aggregating many files into one. A JAR file is essentially a zip file that contains an optional META-INF directory.
See: https://docs.oracle.com/en/java/javase/11/docs/specs/jar/jar.html
So it is possible to replace a class file. But I would prefer the way vanje suggested in the comment of your question.

Why does Class.class.getResouceAsStream(String) always return null?

I tried to obtain a .png image from inside the classpath of my .jar file, so that wouldn't depend on a separate resource folder next to the .jar. I tried doing that with the help of the Class.class.getResourceAsStream(String) method. However, even after ensuring that the path to the .png image is valid, the method still returns null. The path to the file is:
source/org/linear/blank.png
The package also contains two additional files related to my project: the Application class and the Console interface.
As Class class is loaded by the bootstrap classloader, and the Java documentation says that
If this object was loaded by the bootstrap class loader, the method
delegates to ClassLoader.getSystemResourceAsStream
And this method looks for resources in the resources folder.
So to make your code work you have two choices:
1) Use one of your classes, for example, Application, instead of Class. In this case the resource path is resolved relatively to the location of the Application class:
Application.class.getResourceAsStream("/blank.png")
2) Put your image in the resources folder, in this case getSystemResourceAsStream will find it too:
Class.class.getResourceAsStream("/blank.png")

Loading .class files inside a jar that is archived within a jar

I want to load classes that are inside a jar, that is located within another jar. Following is a hierarchy tree illustrating the location of the .class.
A.jar
|---A.class
|---B.jar
|---InterfaceA.class
So I have a class 'A'. This class implements an interface, 'Interface B' which is inside B.jar. A third class named 'C' loads at runtime an instance of A.class. However, I end up with a java.lang.ClassNotFoundException. Any idea on how to fix this issue? I've seen plenty of posts that explains how to dynamically load classes from jar files, however I haven't seen anything that explains how to load classes from a jar file that is within another jar file.
Thanks for your time

Class conflict: two jar files with the same classes

I have two jar files with similar Util class names, but different method signatures.
In jar1, I have a main method which must use the method in Util class in jar1. The JVM is linking to Util class in jar2.
How to resolve this class conflict?
If both jar files are loaded into the same classloader then there is no way to determine which class will get loaded. The only way to handle this is to isolate them so only one of them is loaded into the classloader you are using.
You can set up a classloader and only load the jar you want to get the class from, but it is probably much easier to just make sure classes are unique on your path.
Normally one avoids that situation by using appropriate package names, such that they are different.
In extreme situations, where you dont have the choice to change the jar files,
there is the option "bootclasspath" where you can specify classes that gets loaded first.

When to use custom class loading code instead of defining class path in manifest.mf file?

Lets say we want to package our Java application into a jar file (including the dependencies). The dependencies are copied into one separate directory (lets say libs/). I have read that are two approaches to make the java launcher find these classes:
1) Implement a custom class loader code (loader/launcher pattern) that will load necessary classes before the main code of the application is executed (it is described here: http://vladimirvivien.wordpress.com/2011/10/07/86/)
2) Add the libs/ directory to the "Class-Path:" header in meta-inf/manifest.fm
Which solution is better and why?

Categories

Resources