Okhttp3 com.square does not exist - java

I am using Netbeans and I've created java project with Maven. I added this dependency.
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.2</version>
</dependency>
It worked, I could import com.squareup.okhttp.*. After seeing some code on the web I realized that many people are using the version 3+. I tried to change the package to:
updated
I've typed groupid wrongly in the question "com.squareup.okhttp" but in my code it was right "com.squareup.okhttp3".
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.0</version>
But I couldn't import com.squareup.okhttp3 ( package com.squareup does not exist). Why? I am new to the Java language itself and all the IDEs and tools that support it.

The import is just okhttp3: "import okhttp3" without com.square.

The correct coordinates for okhttp3 are:
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.1</version>
</dependency>
Note that the groupId has changed from com.squareup.okhttp to com.squareup.okhttp3.
So, if you update your pom.xml, replacing what you had for okhttp with what I posted above then you'll be able to resolve the okhttp classes.
For future reference you can find the okhttp artifacts on Maven Central.

Okhttp3 does not exist is due to a bug in Intellij IDE; however, there is a get-around:
Place your 'com.squareup.okhttp3' dependency block at the end of the 'dependencies' list in the pom.xml file.
'import okhttp3...' is the right thing to to in your Java file.
pom.xml: 
...
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.5.0</version>
</dependency>
</dependencies>
Java file:
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

Related

The package com.fasterxml.jackson.databind is not accessible Java (268436910)

I have recently explored handling JSON data with the org.json library and all went well.
Now I started a bigger Maven project, for which I intend to use the Jackson libraries in stead.
Sadly, it does not seem to work for me. I wanted to try out the ObjectMapper class, that VScode autocompleted for me, which also automatically adds the required import:
import com.fasterxml.jackson.databind.ObjectMapper;
However, I also immediately get an error on that line:
"The type com.fasterxml.jackson.databind.ObjectMapper is not accessible Java (16778666)"
I have added the necessary dependencies to my pom.xml file like so:
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.0</version>
</dependency>
</dependencies>
Am I missing something? Are there any other steps that I should have taken?
The only dependency needed for ObjectMapper is
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.0</version>
</dependency>
This error occurs, because Jackson library is not included in your project's classpath. Probably your project is using the Java Platform Module System (JPMS); then log would also contain:
... declared in the "unnamed module" ... module does not read it.
If this is the case, add a requires directive to module-info.java file to specify that this module requires the jackson-databind library:
module correct.module.name {
requires com.fasterxml.jackson.databind;
}
After adding this requires directive recompile the project again.
This is not a Maven problem. You probably have created a package-info.java for your project and so all your dependencies ended up on the module path but your package-info.java is missing the corresponding declarations.
You have to add a line like this:
requires com.fasterxml.jackson.databind;
See also: How to fix 'Package is declared in module, but module does not read it' error in IntelliJ JavaFX?

Unable to import com.auth0.jwt

I unable to import Java jwt 3.10.0. in my spring project. Actually I was trying before version 3.15.0 but it was giving me an error in pom.xml
"Dependency 'com.auth0:java-jwt:3.15.0' not found"
anyway I then move with version 3.10.0 which is accepted in pom.xml, but it doesn't importing in project, although I've many updated Maven, restart Intellj.
Grateful if anyone can advise.
Dependency in pom.xml
<!-- https://mvnrepository.com/artifact/com.auth0/java-jwt -->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.10.0</version>
</dependency>

How to import Apache Commons Net using Maven

When I run import org.apache.commons.net.ftp.{FTP, FTPClient} I receive the error: object apache is not a member of package org. I am using Maven, and I tried adding
<dependency>
<groupId>org.apache.commons.net</groupId>
<artifactId>commons-net</artifactId>
</dependency>
and/or
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
as well as various other similar packages to my pom.xml file, but nothing is working. This seems to be a very simple error yet I cannot figure out what I'm missing. This is my first time using Maven, am I misunderstanding how dependencies work? I am using Scala 2.11.6 if that matters.
Update: I was finally able to run the file I wanted through this command:
mvn exec:java -Dexec.mainClass="com.havas.plugins.test"

import errors in maven project

Getting import can not be resolved errors in maven project even though all the required dependencies have been added to the pom.xml.
Here are the imports which are getting errored:
import org.apache.hadoop.fs.CreateFlag;
import org.apache.hadoop.fs.FileContext;
import org.apache.hadoop.fs.Options;
import org.apache.hadoop.fs.Options.CreateOpts;
And these are the dependencies that are there in pom.xml
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-auth</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
Can someone please point out what could possibly be going wrong?
Thanks in advance!!
You can try out different solutions:
- Try another version of the Hadoop libraries
- Delete the complete .m2 repository in your user folder and redownload the libraries
- Check if your project is configured as a Maven project in your IDE
Good luck!
I tried all the possible options like removing old .m2 directory, update project, checked settings.xml etc but none of them seemed to help. So I finally installed the eclipse again and it worked! Seems like there was something wrong with the IDE but not sure what exactly was wrong.
Thanks everyone for responding with the possible options.

Jena NoClassDefFoundError with Maven

I have a Java Maven project that I developed a while ago and that doesn't work anymore. It uses a parent pom together with another Maven project in which I think the Jena version was changed and it also uses an external library that uses Jena. The Maven dependency is:
<dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>jena</artifactId>
<version>2.6.4</version>
</dependency>
When I execute my tests I get the following errors:
java.lang.NoClassDefFoundError: Could not initialize class
com.hp.hpl.jena.query.ARQ
java.lang.NoClassDefFoundError: org/apache/jena/iri/IRIFactory
at org.openjena.riot.system.PrefixMap.add(PrefixMap.java:54)
at com.hp.hpl.jena.sparql.util.MappingRegistry.addPrefixMapping(MappingRegistry.java:33)
at com.hp.hpl.jena.query.ARQ.init(ARQ.java:449) [...]
The errors are not thrown by my code directly but by the library I include. Can I prevent this by downgrading the Jena version in the parent pom or what can I do here?
P.S.: I now have a minimal code example that reproduces the error (java.lang.NoClassDefFoundError: org/apache/jena/iri/IRIFactory):
import org.junit.Test;
import com.hp.hpl.jena.query.ARQ;
public class DependencyTest
{
#Test
public void testARQ()
{
ARQ a = new ARQ();
}
}
And I guess it comes from this dependency:
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.9.1-incubating-SNAPSHOT</version>
</dependency>
I know there is probably a factory instead of a constructor but I guess this still shows where the problem is.
P.S.: I noticed that I had the dependencies "jena", "arq" and "jena-arq":
<dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>arq</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.9.1-incubating-SNAPSHOT</version>
</dependency>
dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>jena</artifactId>
<version>2.6.4</version>
</dependency>
So I thought maybe I have too much overlapping dependencies and commented out "jena" and "arq". But I still get the error
java.lang.NoClassDefFoundError: Could not initialize class com.hp.hpl.jena.query.ARQ
at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.<init> [...]
I also tried out forcing a non-snapshot-version 2.9.0-incubating, but then I still get the NoClassDefFoundError with and without using the "jena" and "arq"-dependencies.
P.P.S.:
I still get the same error even when I use the following dependencies:
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.9.0-incubating</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>2.7.0-incubating</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-iri</artifactId>
<version>2.7.0-incubating</version>
</dependency>
You can search for the missing class using the Maven Central search application
http://search.maven.org/#search|ga|1|fc%3A%22com.hp.hpl.jena.query.ARQ%22
It demonstrates that the dependency you appear to be missing is:
<dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>arq</artifactId>
<version>2.6.0</version>
</dependency>
Doesn't appear to be a version 2.6.4, but you're probabily best advised to go for a more modern version (This project was recently donated to apache)
Instructions for using Apache Jena with Maven are here:
incubator.apache.org/jena/download/maven.html
Specifying ARQ 2.9.0 as a dependency in your project pom.xml will pull in the other Jena components that you need.
I finally resolved this error by excluding the "jena"-Dependency brought in as a transitive dependency from some library.

Categories