1

I'm developing for a project which is compiled for Java 7.

In this project I use OkHttpClient v3.12.13, which is compatible for Java 7 (as stated here). However these lines produces an error in the Eclipse editor:

private static final OkHttpClient GLOBAL_OK_HTTP_CLIENT = new OkHttpClient.Builder()
    .readTimeout(10, TimeUnit.SECONDS)
    .build();

The error is this:

The type java.time.Duration cannot be resolved. It is indirectly referenced from required .class files

This error is only shown by the editor. Indeed, when I compile my project using CLI (outside Eclipse) I don't get any error for that implicit reference to the java.time package.

I'm aware that in Java 7 there is no java.time package. Indeed, I'm not using it in my code, it's just implicitly referenced by the OkHttp library (as the error message says).

Is there a way to fix this error on Eclipse?

This is how I configured compiler compliance on Eclipse:

Eclipse compiler compliance

Note that I can't upgrade to newer Java versions.

Ricky Sixx
  • 581
  • 1
  • 10
  • 25
  • The *java.time* API is only available in JAVA8+ – Jens Feb 05 '21 at 17:24
  • @Jens And indeed I don't use that package. It's only implicitly referenced by OkHttpClient, so Eclipse shows an error. Is there a way to prevent that error? – Ricky Sixx Feb 05 '21 at 17:25
  • 1
    Use an older version of the library which reference the *java.time* API or the better solution is to use an actual version of java. Java7 is outdated since many years – Jens Feb 05 '21 at 17:27
  • @Jens 1. It's an Eclipse problem, not of the library, because when I compile my project the compiler does not throw any error, even if the library implicitly references `java.time` package. 2. I can't upgrade to newer Java version. – Ricky Sixx Feb 05 '21 at 17:32
  • Look at the Build Path in the Project Properties and check that it really is using the version of the libs which don't use java.time. Eclipse is just reporting problems caused by the build path libraries. – greg-449 Feb 05 '21 at 17:45
  • 1
    If you're stuck with Java 7 (for now), use the [**ThreeTen-Backport**](https://www.threeten.org/threetenbp/). – Andreas Feb 05 '21 at 17:54
  • OkHttp has these in the API, but assumes it's safe if you don't call them. If it's working fine outside eclipse, then you should raise a bug with Eclipse. – Yuri Schimke Feb 05 '21 at 18:25

2 Answers2

2

The java.time classes are built into to Java 8 and later. You are deploying your app to Java 7. That version of Java lacks the java.time.Duration class invoked by your code calling a library.

You claim that library supports Java 7. If that is true, the library must have a switch to avoid using classes not available in Java 7.

You have improperly configured your IDE to allow programming in a later version of than the version on which you deploy. That is a problem you should fix. If deploying on Java 7, then compile for Java 7. See Question, how do I get eclipse to use a different compiler version for Java?.

Of course, the best solution is to migrate to a modern version of Java. Java 7 reached end-of-life in 2015-04. I highly recommend moving to a long-term support (LTS) version. Currently that would be Java 8 and Java 11.

If it were your own code using the java.time classes, I would suggest adding the ThreeTen-Backport library to your project. That library provides most of the java.time functionality with nearly identical API for use with Java 6 and Java 7. But I am guessing that would not satisfy your library in question, though you might want to verify.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thank you for your answer. I have checked the compiler compliace settings as you suggested, but they are fine (code is compiled for Java 1.7). I have also tried to import the ThreeTen-Backport's JAR in my project, but the error is still present. So I think I should switch to another HTTP client which does not have these problems for Java 7... – Ricky Sixx Feb 08 '21 at 17:28
0

Same issue I was facing, Changing the version of selenium in pom.xml worked for me. Changed version from 4.1.0 to 3.141.59

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 05 '23 at 13:03