Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

manage conflict on java classpath

I expose my context :

I have two Java programs that run on a unique Weblogic Server : program A and program B. These ones are launched by two ksh :

programA.ksh and programB.ksh

Both need C.jar but in different versions (but with exactly the same package and classes) :

  • program A need C-1.0.jar
  • program B need C-2.0.jar

I precise that both programs share the same weblogic classpath.

So, my classpath contains in that order :

.....

C-1.0.jar

C-2.0.jar

.....

How can I do so that each program finds its good library ?

For instance, with my actual configuration, program B will always use C-1.0.jar instead of C-2.0.jar because of the priority position on classpath.

like image 232
Mik378 Avatar asked Nov 18 '11 12:11

Mik378


People also ask

How do I resolve a classpath problem?

You have 3 solutions: add this class in the path of your other compiled classes (respecting the package naming of your directories) add the root directory of this class in your classpath (in your case "C:\java\project\") add this single class into a jar and add this jar to the classpath.

How do you resolve a jar conflict?

You can create a package structure (same as the one in your conflicting jars) in your project folder with a different name & copy respective contents from any of the jars. Use one package from the jar and the other from your project (the new package which you just created) I have done this very recently and it worked.

What is java classpath used for?

Classpath is a parameter in the Java Virtual Machine or the Java compiler that specifies the location of user-defined classes and packages. The parameter may be set either on the command-line, or through an environment variable.

How can we set classpath in java program by itself?

You are required to include all the directories which contain . class and JAR files. PATH environment variable once set, cannot be overridden. The CLASSPATH environment variable can be overridden by using the command line option -cp or -CLASSPATH to both javac and java command.


1 Answers

If you launch two separate instances of the JVM for the two programs, then don't use the same classpath! Isn't that obvious?

Are you perhaps using the CLASSPATH environment variable? That is a very old, outdated practice and you should not do it. use the -classpath command line parameter, that way you can easily use different classpaths for the two programs.

Old answer: Assuming that you're talking about threads rather than processes:

The best solution would be to fix A, B or C so that both A and B can use the same version of C.

Or, if the two versions of C actually have intentionally different behaviour, use a different package or class names for them.

Only if you cannot change A, B or C should you consider the technical solution of writing a wrapper that uses different classloaders for A and B so that they will see different versions of C.

like image 138
Michael Borgwardt Avatar answered Sep 25 '22 15:09

Michael Borgwardt