The import com.maxmind.geoip2 cannot be resolved - java

I added to my web project geoip2 using maven but in the code when im trying to use it, the Import is not working. I can see the jar in maven dependencies but i can't use it. I need some help.

I know it's not a complete answer to your problem, but it's too long for a comment.
I've built a sample project using geoip2. POM is as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>geoip</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>geoip</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.maxmind.geoip2</groupId>
<artifactId>geoip2</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</project>
And the Java code:
package com.test;
import com.maxmind.geoip2.WebServiceClient;
import com.maxmind.geoip2.model.CountryResponse;
public class App {
public static void main(String[] args) throws Exception {
int userId = 0;
String licenseKey = "key";
WebServiceClient client = new WebServiceClient.Builder(userId, licenseKey).build();
CountryResponse response = client.country();
System.out.println(response.getCountry().getName());
}
}
I can't reproduce your import problem. It works just fine. You can download this test here: geoip2-test.zip
Check if you can build your project from command line with: mvn clean install
If so, then maybe it's a misconfiguration problem in your project or IDE.
It's Eclipse? Try Maven -> Update Project. Sometimes this works for me when I add new dependencies that are not immediately detected.

if you use intellij, then first close project and then import project and use auto import for the SBT project.then com.maxmind.geoip2 is resolved.

Related

Eclipse Okhttp Error - java.lang.NoSuchFieldError: Companion

I try to use Okhttp and Scarlet library but when I run project this exception throws.
how can I fix it? I search about it and I found out that the version of https is not the same in different library I added to my maven.in my maven dependency i see okhttp-4.9.1.jar and I do NOT know how i can change the version of that.
please help me ...
Exception in thread "main" java.lang.NoSuchFieldError: Companion
at okhttp3.internal.Util.<clinit>(Util.kt:71)
at okhttp3.OkHttpClient.<clinit>(OkHttpClient.kt:1073)
at crypto.Main.main(Main.java:16)
my pom.xml file :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cripto</groupId>
<artifactId>crypto</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.tinder.scarlet</groupId>
<artifactId>websocket-okhttp</artifactId>
<version>0.1.12</version>
</dependency>
<dependency>
<groupId>com.tinder.scarlet</groupId>
<artifactId>scarlet</artifactId>
<version>0.1.12</version>
</dependency>
<dependency>
<groupId>com.tinder.scarlet</groupId>
<artifactId>message-adapter-gson</artifactId>
<version>0.1.12</version>
</dependency>
</dependencies>
</project>
public static String Url = "wss://stream.binance.com:9443" ;
public static void main(String[] args) {
// TODO Auto-generated method stub
OkHttpClient client = new OkHttpClient() ;
System.out.println(client) ;
Scarlet scarlet = new Scarlet.Builder()
.webSocketFactory(OkHttpClientUtils.newWebSocketFactory(client,Url))
.addMessageAdapterFactory(new GsonMessageAdapter.Factory())
//.addStreamAdapterFactory(new RxJava2StreamAdapterFactory())
.build() ;
}
To view where the okhttp dependency is coming from please run the below command this will display the dependency in a tree format
mvn dependency:tree
Once you figure out where the dependency is populated from, you can exclude(How to exclude dependency in a Maven plugin?) okhttp from there, and add your required version.

Trying configure Websocket endpoint with Java 11, OpenJSK, Apache Tomcat/8.5.41

Im trying to configure Websockets on my tomcat server but I'm not able to set the endpoint. But I keep getting error
error: cannot find symbol #ServerEndpoint("/websocketendpoint")
I installed the javax.websocket api but im still missing javax.websocket.server package. I've tried installing every java websocket package I could find and still doesnt work.
Is it supposed to be a part of my tomcat server, java 11 or openJDK, or do I just have to get from somewhere online? I've not been able to find this package till now. All other anotations work when I compile with websocket-api.jar
import javax.naming.*;
import java.io.*;
import java.util.*;
import org.json.*;
import javax.naming.*;
import javax.websocket.*;
#ServerEndpoint("/websocketendpoint")
public class OffisEndpoint{
private Session session;
#OnOpen
public void onOpen(Session session){
this.session = session;
}
#OnMessage
public void onMessage(String message){
try{
if(this.session != null && this.session.isOpen()){
this.session.getBasicRemote().sendText("From Server"+ message);
}
}catch(Exception e){
}
}
#OnClose
public void onClose(){
System.out.println("Close Connection ...");
}
#OnError
public void onError(Throwable e){
e.printStackTrace();
}
}
You're not going to be able to live without a build tool very long as you move forward. For this code I used maven to get going. Maven assumes a particular directory structure so your environment would look like:
pom.xml
src/
main/
java/
com/
example/
websocket/
OffisEndpoint.java
The first line in OffisEndpoint.java will now be package com.example.websocket; to indicate that the file now lives in a Java package.
The contents of pom.xml are:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>websocket</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
You'll need to install Maven to be able to use it. See the Install instructions for more detail.
Once you get Maven installed, change to the directory where your pom.xml file lives and run mvn clean package. This will download files that are needed one time. The next time you build it will not take very long.
The build creates target/websocket-1.0.0.war. This can be deployed to a running Tomcat by copying it to the webapps directory.
You will access this websocket endpoint through http://localhost:8080/websocket/websocketendpoint. The extra path element is because your code is deploy the the websocket web application.

Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: linux, architecture: x86_64

I am trying setup TensorFlow Java application in Eclipse Oxygen (OS: Ubuntu Linux 16.x). I installed Tensorflow and followed the process mentioned in official documentation for Java (Maven Project) installation. I downloaded libtensorflow-1.3.0.jar, jni files and included in the build path. When I execute the program I get this following error
Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: linux, architecture: x86_64. See https://github.com/tensorflow/tensorflow/tree/master/tensorflow/java/README.md for possible solutions (such as building the library from source). Additional information on attempts to find the native library can be obtained by adding org.tensorflow.NativeLibrary.DEBUG=1 to the system properties of the JVM.
at org.tensorflow.NativeLibrary.load(NativeLibrary.java:66)
at org.tensorflow.TensorFlow.init(TensorFlow.java:36)
at org.tensorflow.TensorFlow.<clinit>(TensorFlow.java:40)
at com.tensorflow.malwaredetection.App.main(App.java:13)
App.java
package com.tensorflow.malwaredetection;
import org.tensorflow.TensorFlow;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!"+ TensorFlow.version() );
}
}
POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TensorFlow</groupId>
<artifactId>MalwareDetection</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MalwareDetection</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<exec.mainClass>App</exec.mainClass>
<!-- The sample code requires at least JDK 1.7. -->
<!-- The maven compiler plugin defaults to a lower version -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>libtensorflow</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
</project>
I got tired of this error and tried to do this in an old-fashioned way. Created App.java in a separate folder and included jar, jni files in the same directory. When I execute this from command line, I get different error
dev#ubuntu:~/Downloads$ javac -cp libtensorflow-1.3.0.jar Test1.java
dev#ubuntu:~/Downloads$ java -cp libtensorflow-1.3.0.jar:. -Djava.library.path=./jni Test1
Error: Could not find or load main class Test1
dev#ubuntu:~/Downloads$
I think you need to include jni library dependency in your pom.
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>libtensorflow_jni</artifactId>
<version>1.1.0</version>
</dependency>
When you have tensor flow dependency in your pom.xml file as below
<dependencies>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow</artifactId>
<version>1.14.0</version>
</dependency>
It will download required 3 libraries
tensorflow-1.14.0.jar
libtensorflow-1.14.0.jar
libtensorflow_jni-1.14.0.jar
So you have to manually extract the 3rd jar(libtensorflow_jni) and get your OS compatible file like for windows you have to copy the tensorflow_jni.dll file and paste in your project root directory
that will solve your problem.
For Linking error the JNI library which you have downloaded is
not compatible with OS you are running,
is not in PATH () or
with lesser probability TensorFlow bu which you should be able to confirm from TF triage.
For second could not find class
your PATH must be appended with "." so that it can find class in current folder from where it is running
Less probability is if class name/package is not same.
If you can post code for class will check

How to download source code from Maven Central for a project

I want to download the source code for the project XBee-api v9.3 from Maven Central so that I can modify the code for my application. I am using Netbeans and I have managed to produce a POM that validates and compiles successfully.
I have then run the command
mvn dependency:resources
from the same directory as the POM to force download of the source code.
I have a few problems:
When I look inside the project with Netbeans, I see the class files (which I think may actually be jars). When I open up any class, I only get the method headers, but not the source code.
When I open a class file, there is the option in the top right of the window to "Attach Sources...". When I select "Download", I get a message in the lower left of the window saying "Downloading source jar from known Maven Repositories for local repository file" but nothing seems to be happening.
When I open up the folder either with explorer or Netbeans files view, the folder is empty except for the POM. If I use Projects view in Netbeans, I can see a project structure and what appears to be the generated source files, but no source code.
I can't find answers on Google.
My questions are:
What am I doing wrong?
How do I download the source code?
Any help would be much appreciated.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rapplogic</groupId>
<artifactId>xbee-api</artifactId>
<packaging>jar</packaging>
<version>0.9.3</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>A java library for communicating with XBee radios</description>
<url>https://github.com/andrewrapp/xbee-api/</url>
<licenses>
<license>
<name>GPL license, Version 3.0</name>
<url>https://www.gnu.org/licenses/gpl-3.0.html</url>
</license>
</licenses>
<developers>
<developer>
<name>Andrew Rapp</name>
<email>andrew.rapp+github.com#gmail.com</email>
</developer>
</developers>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<!-- https://mvnrepository.com/artifact/com.rapplogic/xbee-api -->
<dependencies>
<dependency>
<groupId>com.rapplogic</groupId>
<artifactId>xbee-api</artifactId>
<version>0.9.3</version>
</dependency>
</dependencies>
</project>
The GitHub project page is here:
https://github.com/andrewrapp/xbee-api
(referenced in the POM under the url element)
...so you can just download the source from the Github page: https://github.com/andrewrapp/xbee-api/archive/master.zip

Deploy a Java EE project as jar with Maven and perform the DI

I've just developed a sample Java EE 7 application.
The code is as follows:
#Stateless
#LocalBean
public class Foo {
#Inject
private Boo boo; // Internal resource
#Asynchronous
public void doFoo(Collection<Object> c) {
boo.doSomething(c);
}
}
With the aim to deploy the project as jar file, I'm using the following Maven configuration:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sample</groupId>
<artifactId>ejb-foo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>ejb-foo</finalName>
</build>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
</project>
Unfortunately, Maven returns me this warning:
Classpath entry org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER will not be exported or published. Runtime ClassNotFoundExceptions may result. ejb-foo P/ejb-foo Classpath Dependency Validator Message
How can I fix this error?
Note:
The idea is to import that jar into another Java project and then to instance the Foo class as EJB:
import myjavaeeproject.Foo;
public OtherClass {
#EJB
private Foo foo;
public void doMagic(List<String> list) {
foo.doFoo(list);
}
}
Update:
I've fixed the error as shown here.
When I deploy (as war) the target project (that implements OtherClass, annotated as WebServlet) on JBoss, I've an error:
POST_MODULE: JBAS018733: Failed to process phase POST_MODULE of deployment
It depends on the EJB injection.
What am I doing wrong?
As per my examples in comment, it is because eclise think "the library exists at the server and it is not right to export this with your projects"
I don't have much idea about your code, but seems to be ok.
If this is a J2EE application, I would expect the target to be a war or ear. I don't think that a J2EE container will understand a jar deployment.

Categories