4

I'm currently thinking about adding a small part of JSR-310, javax.time.Duration to our library.

This works perfectly fine currently.

But JSR-310 is planned to be integrated in Java 8! When executing our application on Java 8, what will happen if there is both a javax.time.Duration class in the standard library and the same class shipping with our jar file?

Will one of the classes be silently ignored? (Which one?) Will there be an error when a Java 8 VM tries to load the class from our library?

Are there any compatibility issues I need to be aware of?

Epicurist
  • 903
  • 11
  • 17
soc
  • 27,983
  • 20
  • 111
  • 215
  • How do you add this class? Do you copy the source code? –  Sep 05 '12 at 11:37
  • Yes. Just including the class in `javax/time/Duration.java` in our source tree. (Just copying it from the compiled JSR-310 jar should also work, but I don't think it matters much.) – soc Sep 05 '12 at 11:39

2 Answers2

6

The JVM follows the classpath to determine which class to load. If there is more than one, the later classes are silently ignored.

For class included in the JVM itself, these are part of the bootclasspath which is searched before the class path.

Unless there is a breaking change in the API, you shouldn't notice the difference and your extra JAR will effectively be ignored with Java 8.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • He doesn't say "extra" JAR. But we should recommend that he does put the "back-ported" classes into an extra JAR so that it can be left out of installations that don't need it, and ultimately dropped when he stops supporting Java before version 8. – Stephen C Sep 05 '12 at 11:48
  • @StephenC Good point, I had assumed there was a standard release of the library and this library can be included or discarded as required. Releasing your own version is likely to be more work and complication than its worth. – Peter Lawrey Sep 05 '12 at 11:51
1

This issue can now be clarified.

The JDK1.8 classes will be in the java.time namespace, and cannot be overridden (as they are deeply in the core of Java).

A backport project is available that allows a very similar API to be used on JDK1.7. That uses the org.threeten.bp namespace. The idea is that moving forwards to JDK1.8 will simply require a package rename for the most part.

The javax.time namespace is no longer in use.

JodaStephen
  • 60,927
  • 15
  • 95
  • 117