NetBeans中Maven项目引入OkHttp3报错:com.squareup包不存在
Hey Diego, let's work through this issue step by step—since you're new to Java, Maven, and NetBeans, I'll keep things clear and actionable.
First, Let's Confirm the Core Issue: OkHttp 3's Package Structure Change
A common pitfall when upgrading from OkHttp 2.x to 3.x is the package name change:
- OkHttp 2.x used
com.squareup.okhttp.* - OkHttp 3.x simplifies this to just
okhttp3.*(nocom.squareupprefix!)
That's likely why you're seeing the "package com.squareup does not exist" error—you're trying to import a package that no longer exists in OkHttp 3.
Step-by-Step Fixes
1. Verify Your Maven Dependency is Correct
Double-check your pom.xml has the right OkHttp 3 dependency (you mentioned you fixed the groupId, but let's confirm):
<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>3.9.0</version> </dependency>
2. Force Maven to Refresh Dependencies
NetBeans might be holding onto cached old dependencies. Do this:
- Right-click your project in the Projects window
- Hover over
Maven→ SelectClean and Build - Alternatively, open the terminal in your project root and run:
Themvn clean install -U-Uflag forces Maven to update all dependencies, bypassing cached versions.
3. Update Your Import Statements
Replace any old OkHttp 2 imports with OkHttp 3's correct paths. For example:
- ❌ Wrong:
import com.squareup.okhttp3.OkHttpClient; - ✅ Correct:
import okhttp3.OkHttpClient;
Common OkHttp 3 classes you might need:
okhttp3.OkHttpClientokhttp3.Requestokhttp3.Responseokhttp3.MediaType
4. Reload the Project in NetBeans
Sometimes IDEs don't sync dependency changes automatically:
- Right-click your project → Select
Reload Project - Check the Dependencies folder in the Projects window—you should see
okhttp-3.9.0listed (not the old 2.7.2 version)
5. Clear NetBeans Cache (If All Else Fails)
If the error persists, NetBeans' cache might be corrupted:
- Close NetBeans completely
- Find the cache directory:
- On Windows:
C:\Users\<YourUsername>\AppData\Roaming\NetBeans\<NetBeansVersion>\var\cache - On macOS/Linux:
~/.netbeans/<NetBeansVersion>/var/cache
- On Windows:
- Delete the entire
cachefolder - Reopen NetBeans and load your project again
内容的提问来源于stack exchange,提问作者Diego Alves

