1

I would like to get deeper understanding on how Java deals with different versions of Classes/Packages/etc., but couldn't find any resources or at least the best way to google for it. The problem is as follows.

Imagine we have some external package com.external.package that contains a definition of SomeInterface.

Now I write a java class MyClass that implements SomeInterface, and using com.external.package v1.0.0. Next I package a (lean) jar containing MyClass.

Now I plug this jar in another program that is looking for implementations of SomeInterface, but in it's dependencies, it is using com.external.package v2.0.0.

Is the reason I get Failed to find any class that implements SomeInterface that versions of SomeInterface don't match in the program and in the jar that contains a class extending it?

Basically the question I would like to find an answer for is what info do jars store regarding external dependencies? Does it store the exact versions of them and if they don't match at the runtime it complains? But why does it even allow running the program with references to same dependency, but different versions?

eddyP23
  • 4,745
  • 4
  • 29
  • 63
  • 1
    Java has no concept of "package versioning" – a_horse_with_no_name Sep 08 '17 at 07:05
  • Could you please give a broader explanation answering my questions? – eddyP23 Sep 08 '17 at 07:06
  • Classes and packages don't have versions. The reason you get that "Failed to find any class that implements SomeInterface" is unknown to us, because we have no idea of what your code is doing, and this does not look like a standard compiler/runtime error. So it must come from your code, or from one of the libraries you're using. – JB Nizet Sep 08 '17 at 07:06
  • JARs don't store versioning information either. A Maven/Gradle configuration will, and it just bundles all the libraries. – OneCricketeer Sep 08 '17 at 07:06
  • In terms of Maven you can say that they are having versions for different builds. But particularly for the packages, Java doesn't have anything called versions. The interface that you are trying to access is probably from Different maven/ build (rather than package) version. – procrastinator Sep 08 '17 at 07:10
  • So does the only info about external packages it stores in a jar is the name of the class? And if it matches at runtime, it is happy? – eddyP23 Sep 08 '17 at 07:11
  • "external packages" are listed in the import statements, sure. If it can't import off the classpath, it'll say so and throw an exception – OneCricketeer Sep 08 '17 at 07:18

2 Answers2

2

Is the reason I get Failed to find any class that implements SomeInterface that versions of SomeInterface don't match in the program and in the jar that contains a class extending it?

There is no "versioning" happening here. Simply, the error states no such class exists on the classpath. For example, you didn't put a -cp in your java command to add that extra JAR/class file.

Other reasons this could happen is that an API marks a class as deprecated in v1, then decides to remove it from v2. In which case, you best try to compile and test your code against the proper library versions before you package your own code. If you made an uber JAR, the classes should get shaded, and you probably wouldn't have missing classes.

Maven projects do have the concept of transitive, versioned dependencies, but you've not said anything about that

OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
  • It's not an uber jar. I used maven to package it, could that have anything to do with it? – eddyP23 Sep 08 '17 at 07:29
  • Sure. You didn't properly list `` on some other library for a needed version and you didn't shade it **or**, you are missing the other library on the classpath, as I said, which is external to Maven – OneCricketeer Sep 08 '17 at 08:10
1

Seeing that the original question has found an answer already, it seems somewhat relevant to mention that Java Packages and JARs could be used for specifying package version information as discussed in the following documentation: https://docs.oracle.com/javase/8/docs/technotes/guides/versioning/spec/versioning2.html#wp89936

Also, the Oracle Java Tutorials discuss them and further concepts around deployment of programs as JAR Files as documented here: https://docs.oracle.com/javase/tutorial/deployment/jar/index.html