7

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.

Mik378
  • 21,881
  • 15
  • 82
  • 180
  • There's something wrong with your terminology. Processes don't "run on the JVM" The JVM *is a* (single) process. Are you talking about threads? – Michael Borgwardt Nov 18 '11 at 12:19
  • Yes, you're right, in fact it's two java launchers wrapped in shell script. I reedit the post with the correction – Mik378 Nov 18 '11 at 12:21
  • Your question is still not really clear. If you launch java twice (one for A and one for B), then A and B don't run on the same VM. Each program has its own VM. Is it what you're doing? – JB Nizet Nov 18 '11 at 12:25
  • I have just corrected my question – Mik378 Nov 18 '11 at 12:27
  • It's still unclear. Do you have one weblogic server running or two? What kind of programs are A and B? Web apps deployed as war files? EARs? An enterprise application's jars should NOT be in the server's classpath. They should be in the war or ear of the application. App servers are able to deploy two apps using conflicting jars if those jars are deployed as part of the app. – JB Nizet Nov 18 '11 at 12:30
  • In fact, there are two KSH that launch Java program. But both share the same weblogic classpath. – Mik378 Nov 18 '11 at 12:36

4 Answers4

5

Basically you cannot do this (simply). Take a look at http://en.wikipedia.org/wiki/Java_Classloader#JAR_hell, where they explain that the standard Java classloader cannot do this.

You can either run the two processes on different VMs, or go into a lot of classloader hell...

Hiery Nomus
  • 17,429
  • 2
  • 41
  • 37
5

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.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • I agree with your answer :) I noticed that I was not clear in my question so I had reedited it. – Mik378 Nov 18 '11 at 12:32
1

You will have to make sure that only one library is present on classpath.

Simplest way is to create 2 lib directories with correct dependencies and reference all jars from there in your startup script for respective process.

This simple shell script will do this for you automatically:

MY_CLASSPATH=.

for i in /path/to/A/lib/*.jar
do
  MY_CLASSPATH=$MY_CLASSPATH:$i
done

java -cp $MY_CLASSPATH my.main
Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
0

I would assume that these are Java web apps if they're running on WebLogic, they ought to be in WAR files. There won't be any collision if each one puts their respective JAR versions in the WEB-INF/lib of their WAR file.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • There are not Java WebApps, these are just two batches purely in J2SE. Weblogic just supports a specific Persistence framework called Jrisk (prioritary) – Mik378 Nov 18 '11 at 13:01
  • Do they deploy on a WebLogic server? Or just use some JARs? (Do you mean "proprietary"?) In that case, you set the CLASSPATH using the -classpath option in the script that runs the job. – duffymo Nov 18 '11 at 21:41