Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
877 views
in Technique[技术] by (71.8m points)

eclipse - The type java.time.Duration cannot be resolved when using OkHttpClient v3.12.x on Java 7

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.

question from:https://stackoverflow.com/questions/66067782/the-type-java-time-duration-cannot-be-resolved-when-using-okhttpclient-v3-12-x-o

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...