1

I am working on a Java SE project that has both a library part (a few packages of public classes) intended to be used by programs written by others and an applications part (classes with main() methods) that use that library. So the installation evironment must handle the possibility of there being multiple applications programs simultaneously present and using different versions of the library. Any ideas on how to handle this? How do people avoid JAR hell?

A future version of Java will incorporate the Jigsaw functionality. Enterprise applications can make use of the OSGi component functionality. But what about non Enterprise applications now? Is there anything simpler than OSGi that gets the job done?


Edit, after some preliminary answers: I can handle the build-time dependency problem reasonably well using our version control and build process. Its help with the run-time dependency problem I'm really interested in.

Community
  • 1
  • 1
Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • See also http://stackoverflow.com/questions/139534/classloader-issues-how-to-determine-which-library-versions-jar-files-are-load/139796#139796 – Raedwald Mar 02 '11 at 14:04

5 Answers5

2

Sure: Maven

Sorry, misread the question.

You can use OSGi (Equinox or Felix) in any application, it doesn't have to be an enterprise (server based) application.

That said, you can easily control the visibility of your classes yourself by creating URLClassLoaders as needed. That's how the OSGi implementations do it.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 1
    Maven isn't a run-time tool is it? Not in the same way that OSGi is for constructing an application from plugins, and resolving runtime dependencies based on version information etc. My understanding is that Maven is a development tool rather than a runtime facility like OSGi. Mind you, I never really have understood Maven :-) – Tom Quarendon Mar 01 '11 at 13:17
  • You can use `mvn dependency:copy` to create a copy of all dependencies in some directory. So in your IDE, you use Maven to build the compile time classpath and then the dependency plugin to build a deployable. Or you can use the assembly plugin to create one big JAR. – Aaron Digulla Mar 01 '11 at 13:20
  • Writing your own class loader? That sounds rather complicated. – Raedwald Mar 01 '11 at 13:29
  • No, just call `new URLClassLoader(urls, getClass().getClassLoader())` and add the *additional* JARs that should be visible in this classloader to `urls`. The new classloader will ask the parent classloader for any unknown classes. Now you can load classes from it via `loadclass()` and use reflection to create instances and invoke methods. The setup takes a bit effort but afterwards, it's normal Java code. – Aaron Digulla Mar 01 '11 at 13:54
  • http://stackoverflow.com/questions/1558720/when-should-i-use-a-platform-like-osgi-and-when-it-must-be-avoided suggests OSGi is suitable for mostly for long runing (server)applications. – Raedwald Mar 03 '11 at 13:09
  • @Raedwald: It's most often used that way but that doesn't mean it's limited in any way for desktop apps. – Aaron Digulla Mar 03 '11 at 16:41
1

Apache Ivy is a light-weight solution. It does only dependency management and doesn't try to change your build system to match.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • Apache Ivy seems to does build-time dependency management. Does it also do run-time dependency management? – Raedwald Mar 01 '11 at 13:42
0

Use Maven.

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.

jmj
  • 237,923
  • 42
  • 401
  • 438
  • 1
    While I really like Maven and use it a lot, it should be said that Maven provides a lot more than just dependency management. It also provides a build system and is pretty opinionated about project structure and hierarchies (which can make it hard to convert existing projects). – Joachim Sauer Mar 01 '11 at 13:16
  • @Joachim , Somehow disagree. I find maven's dir structure very good to convert existing project to maven one. also depends on the project size and structure. but its not that hard I feel – jmj Mar 01 '11 at 13:18
  • 1
    it's not the structure that's hard to convert, but if you've got existing (complex) build systems in place, then switching over to Maven "just" to get dependency/library management can be costly. In my opinion that cost can easily pay of in the long run, but the cost *does* exist and can be quite high. – Joachim Sauer Mar 01 '11 at 13:19
  • 1
    Maven is a build tool. The question is not about build. Maven does nothing to address the question. – Tom Anderson Mar 01 '11 at 13:21
  • 1
    @Tom maven't isn't simply build tool, it is project management tool. – jmj Mar 01 '11 at 13:23
0

If all you want is dependency resolution, then Apache Ivy fits the bill. It performs similarly to Maven's dependency resolution, but lacks Maven's complexity since it has much fewer features.

Mike Yockey
  • 4,565
  • 22
  • 41
  • Apache Ivy seems to does build-time dependency management. Does it also do run-time dependency management? – Raedwald Mar 01 '11 at 13:42
0

I don't know that I've ever heard the term "runtime dependency" used in a Java context. Runtime dependencies, as I'm familiar with, are usually handled through application context. The Spring Framework is one such way to manage application context.

Mike Yockey
  • 4,565
  • 22
  • 41