java: package okhttp3 does not exist

Answered

I must be fundamentally misunderstanding how to add a maven dependency, but I've tried just several tutorials and I keep running into this same error. I'd love some education on what I'm doing wrong.

Step 1: Create Project

File -> New -> Project -> Maven -> Project SDK: corretto-11 -> Finish

 

Step 2: Add Maven Dependency

Open POM.xml -> paste snippits -> Reimport All Maven Projects

<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.5.0</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

 

Step 3: Create Java code

Right Click on /src/main/java -> Create package -> Create Main java class -> Paste in code

package com.http.tryout;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Main {
public static void main(String[] args) {
// write your code here
System.out.println("hello");
}

public void getStats() throws Exception {

OkHttpClient client = new OkHttpClient().newBuilder()
.build();
Request request = new Request.Builder()
.url("https://testurl.com")
.method("GET", null)
.addHeader("Requester-ID", "Meow")
.build();
Response response = client.newCall(request).execute();
}
}

Step 4: Adjust compile settings

Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler -> Target bytecode version = 11

 

Step 5: Build Project

Error:(3, 15) java: package okhttp3 does not exist

 

0
18 comments
Avatar
Permanently deleted user

I figured out the problem. Intellij isn't able to read my .m2 folder. I temporarily fixed the problem by overriding the folder location to elsewhere.

0

Hello Jackson,

Is it possible to share idea.log ("Help | Show Log in...") after reproducing the issue?

0
Avatar
Permanently deleted user

Same for me with dependency

 

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>3.14.8</version>
</dependency>
0

@Arnezelasko could you please provide idea.log ("Help | Show Log in...") after reproducing the issue?

0
Avatar
Permanently deleted user

Hi Petr, i have the problem at work and dont know what data exactly the log contains, maybe data from my work projects so i prefer not to send it around. Can you maybe say which part from the logs you need, then i could extract it.

0

@Arnezelasko anything related to maven, or any strange exceptions.

0
Avatar
Permanently deleted user

I had to restart IntelliJ to make it pick up the OkHttpClient dependency.

1

@..., this seems to be an interesting bug. Weirdly the exact same thing happened to me, specifically with the okhttp3 dependency. Restarting worked, but I'm not sure why it happened in the first place. 

My dependency is

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.0</version>
</dependency>
0

@Darshit have you tried Reload action in Maven tool window for the IDE to download and add a dependency to the module? If after reload the dependency was still missing, could you please upload all your logs (Help | Collect Logs and Diagnostic Data action) at https://uploads.jetbrains.com to check? Thanks.

0

Andrey Dernov the Reload action tool is disabled and blacked out in this case. Clicking did no effect.

Unfortunately, it seems IntelliJ overwrote the logs and Icouldn't find any meaningful ones from that day.

0

>Andrey Dernov the Reload action tool is disabled and blacked out in this case. Clicking did no effect.

It might be that your project is not opened as a Maven project. You should link the Maven project, please see Link and unlink a Maven project.

0

It might be that your project is not opened as a Maven project. You should link the Maven project, please see Link and unlink a Maven project.

I am aware of using Maven in IntelliJ, so I don't think that this is a problem. I did add other dependencies before adding `okhttp3` and it worked correctly. It didn't work specifically for this library, and thus I googled this problem and arrived at this ticket.

0

>the Reload action tool is disabled and blacked out in this case. Clicking did no effect.

is it possible to see on a screenshot how does it look like? The idea.log (Help | Show Log in ... action) file would also be useful. Thanks.

0

It's like the following but blacked out and unclickable. Obviously restarting worked for me so I cannot reproduce it again. 

 

I have added the relavant idea.log from the day at Upload id: 2020_12_21_7BjhuAkpQNCekWGc (file: idea.diag.log)

0

Please note that you can always use this action from the Maven tool window at any time.

Unfortunately nothing suspicious in logs related to Maven reload. If problem reappears please file a bug at https://youtrack.jetbrains.com/issues/IDEA with steps to reproduce and expected/actual result you see to better understand what situation happens for you. Thank you.

0
Avatar
Permanently deleted user

This problem showed up for me on a Gradle project, and a way forward presented itself.

  1. Set Shared indexes for Maven Libraries to:  "Don't download, use local indexes."
  2. Close the project
  3. Delete the project's .idea folder
  4. Re-import the project

Result: Unit test with the okhttp3 import compiles.

The Share Index idea.log entry, prior to turning the feature off as specified above:

2021-06-05 15:41:58,327 [3462094]   INFO - oad.SharedIndexDownloadService - Fetching shared index: SharedIndexResult(request='MavenLibraryIndexRequest(MavenPackageId(com.squareup.okhttp3:okhttp:3.14.9))', url='https://index-cdn.jetbrains.com/v2/data/mvn/com.squareup.okhttp3:okhttp:3.11.0/com_squareup_okhttp3_okhttp_3_11_0-linux-80b54ec6e63ce223a70faaeffdd4dbd630eb2b35990459887f13d6f1a1d67cd7.ijx.xz', weakHash=3fec3c81d449, weakBaseVersionHash=3404703181c5, sha256='80b54ec6e63ce223a70faaeffdd4dbd630eb2b35990459887f13d6f1a1d67cd7') ([MavenSuggestion(MavenPackageId(com.squareup.okhttp3:okhttp:3.14.9))]) 

Environment:

IntelliJ IDEA 2021.1.2 (Ultimate Edition)
Build #IU-211.7442.40, built on May 31, 2021
Licensed to Tom Sheehan
Subscription is active until December 7, 2021.
Runtime version: 11.0.11+9-b1341.57 amd64
VM: Dynamic Code Evolution 64-Bit Server VM by JetBrains s.r.o.
Windows 10 10.0
GC: ParNew, ConcurrentMarkSweep
Memory: 1979M
Cores: 12
Registry: debugger.watches.in.variables=false
Non-Bundled Plugins: CheckStyle-IDEA (5.52.0), org.sonarlint.idea (4.16.0.31683), com.sonatype.nexus-iq (3.3.1-01), com.intellij.selenium (211.7036.8), org.asciidoctor.intellij.asciidoc (0.32.47)
Kotlin: 211-1.4.32-release-IJ7442.2
gradlew -v
------------------------------------------------------------
Gradle 7.0.2
------------------------------------------------------------
Build time: 2021-05-14 12:02:31 UTC
Revision: 1ef1b260d39daacbf9357f9d8594a8a743e2152e

Kotlin: 1.4.31
Groovy: 3.0.7
Ant: Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM: 11.0.10 (AdoptOpenJDK 11.0.10+9)
OS: Windows 10 10.0 amd64

 

0

Same problem here with oktthp3. In my case the error message was "module okhttp3 not found"

Clicking on the error link it redirected me to this module-info.java line:

requires okhttp3;

The module was there (I could see it correctly imported as a External Library), the pom.xml was correct (it was literally pushed from other working machine some seconds earlier)

Instead of closing&opening IntelliJ, I tried to replace okhttp3 by okhttp4 (this was marked in red, of course, as this module does not exist) and then replacing by okhttp3 again. Surprisingly, it worked (I can run the app w/o any okhttp3 related errors).

The idea.log doesn't show anything suspicious. Weird bug.

0

Juanan Pereira
Could you please share your project sample for further analysis?
Also, it would be great if you could provide your IDE log files (Help > Collect Logs and Diagnostic Data)?
You can upload both the zipped project folder and log archive here: https://uploads.jetbrains.com, and just provide the uploaded file ID.

0

Please sign in to leave a comment.