0

My code has a dependency on abc.jar file. This abc.jar file has a class called Logger, under the package org.apache.log4j, but this is not the Logger from Apache.

I want to use Logger from Apache. I have added a dependency in maven for Apache's Logger. But the problem is that when I want to use Apache's Logger, it automatically picks up the Logger from abc.jar file.

Manoj
  • 644
  • 12
  • 21
  • 1
    Can't you get rid (in the classpath) of the jar you don't want ? – Arnaud Jan 05 '16 at 10:16
  • This is not clear.. You have 2 different classes with same name `org.apache.log4j.Logger` in 2 different JARs ? One is the official from Apache and the other is one you wrote yourself ? – Gaël J Jan 05 '16 at 10:19
  • This doesn't sound like the whole story. Post the *entire* class where you're having the problem, and describe *specifically* what "picks up the wrong class" means. – chrylis -cautiouslyoptimistic- Jan 05 '16 at 10:19
  • 1
    Why would the abc.jar contains a package `org.apache.log4j` if it's not Apache's classes ?!! – Gaël J Jan 05 '16 at 12:47
  • @Gaël I don't know, but it is the name of the package in which the Logger class is present which internally uses `java.util.logging.Logger` class – Manoj Jan 05 '16 at 14:28
  • I think you need to identify this jar that apparently includes a duplicate. – chrylis -cautiouslyoptimistic- Jan 05 '16 at 21:57
  • 1
    @Manoj, you seem to believe that you can use Apache classes without them being packaged in JARs. While technically possible, this is very bad practice and should be avoided at all costs. I suspect that you are actually trying to use Maven declared dependency instead. Be informed, that Maven will obtain Apache classes in form of JAR packages. You therefore have to deal with two different JAR's. – Basilevs Jan 06 '16 at 12:46
  • @Basilevs Thats correct. What I meant to say is - I want to use Logger class but not from org.apache.log4j.logger package inside the abc.jar file. Instead of that I wanted to use Logger from the Apache's log4j.jar – Manoj Jan 06 '16 at 13:39
  • Possible duplicate of [Dealing With Duplicate Fully Qualified Names](http://stackoverflow.com/questions/12823048/dealing-with-duplicate-fully-qualified-names) – Basilevs Jan 08 '16 at 05:58

2 Answers2

0

To load class from specific JAR when both JAR has same Package with same class name.

1) You need to specify the path of the JAR from which you need to use the class.

To do so you can use

URL myURL = new URL("jar:file:" +OfficialApacheJarPath+"!/");
    URL[] urls =  new URL[]{myURL};
    URLClassLoader cl = URLClassLoader.newInstance(urls);
    Class c = cl.loadClass("Logger");
swapnil7
  • 808
  • 1
  • 9
  • 22
-1

To prevent such collision, you should use the full qualified class name.e.g. org.apache.log4j.Logger logger = new org.apache.log4j.Logger();

Using fully qualified package names is usually considered poor style, except when it is necessary to avoid collisions.

pankaj
  • 1
  • 1
  • 2
    OPs problem is that he has two classes with the same full qualified name in his classpath. – Tom Jan 05 '16 at 13:14