Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is classpath hell and is/was it really a problem for Java?

Tags:

java

history

What is classpath hell and is/was it really a problem for Java?

like image 759
Jonathan Allen Avatar asked Dec 16 '08 23:12

Jonathan Allen


People also ask

What is classpath hell?

Classpath hell is an unfortunate consequence of dynamic linking of the kind carried out by Java. Your program is not a fixed entity but rather the exact set of classes loaded by a JVM in a particular instance.

What is a hell JAR?

JAR hell is a term similar to DLL hell used to describe all the various ways in which the classloading process can end up not working. Three ways JAR hell can occur are: Accidental presence of two different versions of a library installed on a system. This will not be considered an error by the system.

How do I know if a JAR is classpath?

A pragmatic way: Class. forName("com. myclass") where com. myclass is a class that is inside (and only inside) your target jar; if that throws a ClassNotFoundException , then the jar is not on you current classpath.

What is JAR issue?

JAR hell is a class loading problem where the wrong version of a class is loaded into the JVM, because the same class exists in multiple JAR files given in the classpath.


2 Answers

Classpath hell is an unfortunate consequence of dynamic linking of the kind carried out by Java.

Your program is not a fixed entity but rather the exact set of classes loaded by a JVM in a particular instance.

It is very possible to be in situations where the same command line on different platforms or even on the same one would result in completely different results because of the resolution rules.

There could be differences in standard libraries (very common). Libraries could be hidden by one another (an an older version may even be used instead of a newer one). The directory structure could mess resolution. A different version of the same class may appear in multiple libraries and the first one encountered will be used, etc. Since Java, by specification, uses a first-encountered policy, unknown ordering dependencies can lead to problems. Of course, since this is the command line and it is part of the spec, there are no real warnings.

It is very much still a problem. For example on Mac OS the horrible support from Apple means that you machine ends up with several JVMs and several JREs, and you can never easily poart things from place to place. If you have multiple libraries that were compiled against specific but different versions of other libraries, you coulld have problems, etc.

However, this problem is not inherent in Java. I remember my share of DLL hell situations while programming windows in the 90s. Any situation where you have to count on something in the file system to assemble your program rather than having a single well defined executable is a problem.

However, the benefits of this model are still great, so I'm willing to tolerate this hell. There are also steps in the right direction on the side of Sun. For example, Java6 allows you to simply specify a directory with jars rather than have to enumerate them.

BTW: Classpaths are also a problem if you are using an environment that uses a non-default classloader. For example, I have had a lot of problems running things like Hibernate or Digester under Eclipse because the classloaders were incompatible.

like image 75
Uri Avatar answered Sep 19 '22 19:09

Uri


Classpath/jar-hell has a couple of escape hatches if they make sense for your project:

  • OSGi
  • JarJarLinks
  • NetBeans Module System - Not sure if this is usable outside of NetBeans
  • Others?
like image 36
Dave Ray Avatar answered Sep 20 '22 19:09

Dave Ray