Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use two versions of jar in my java project

In my java project, I need to use neo4j-1.9.3 that depends on lucene-3.6.2, and ElasticSearch which depends on lucene-4.4.0. I know that if I want to use two versions of lucene directly, I can use ClassLoader to load different classes from the lucenes.

But the problem is that I won't use lucene's apis directly now. Is there any way that lucene-3.6.2 can be loaded when neo4j's apis are running, and lucene-4.4.0 can be loaded while running elasticsearch's apis. The two versions of lucene conflict now, and I need to run neo4j and elasticsearch in one project. How could I solve the dependency problem?

Thanks in advance.

like image 701
GameBoy Avatar asked Oct 16 '13 04:10

GameBoy


People also ask

Can we use multiple versions of same jar and run the application?

If this is a desktop application, you should have no problem, because you can include just the correct jar in the app. If this is a web app that needs to access both versions, and you can't separate it into two web apps, then -yes- you would need to use classloaders to separate the two namespaces from each other.

Can you create a multi Release jar for different version of Java?

One of the new features that Java 9 brings us is the capability to build Multi-Release JARs (MRJAR). As the JDK Enhancement Proposal says, this allows us to have different Java release-specific versions of a class in the same JAR.

Can a jar include another jar?

Adding jars to a single jar is done using the jar command. Suppose you have jarA, jarB and jarC. For your deployment you would need a manifest file too. The manifest would specify the external jars' full path.

Can a jar have two main classes?

Jar files can contain only one Main-Class attribute in the manifest, which means a jar can contain only one mainClassName.


1 Answers

You could still use a ClassLoader solution, but that is a nasty business where it is easy to make mistakes.

You could also use different JVM's.

But the solution I would personally choose is either (or both) of the following:

  • Verify that Neo4J breaks with Lucene 4.x. Maybe you will get lucky and there won't be any problems using the latest version.
  • Split them up into two applications where one of them, say Neo4J, is accessed by the other application via web service (most likely REST) calls. Neo4J has a built-in REST API anyway.
like image 53
Vidya Avatar answered Oct 08 '22 21:10

Vidya