NoClassDefFoundError: com/mongodb/MongoClientURI [duplicate] - java

This question already has answers here:
Why am I getting a NoClassDefFoundError in Java?
(31 answers)
Closed 2 years ago.
package com.sm.mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.types.ObjectId;
public class JavaMongoConnection {
public static void main(String[] args) {
//System.setProperty("jdk.tls.trustNameService", "true");
MongoClientURI uri = new MongoClientURI(
"mongodb+srv://admin:admin123#cluster0-bkruu.mongodb.net/test?retryWrites=true&w=majority");
MongoClient mongoClient = new MongoClient(uri);
MongoDatabase database = mongoClient.getDatabase("test");
}
}
This is my code, and whenever I run this code, I get the error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/MongoClientURI
I looked at java.lang.NoClassDefFoundError when using MongoDB driver and I tried their solution where I went to Run Configurations --> Dependencies and looked under Classpath Entries to make sure that I have bson-xxx.jar, mongodb-driver-xxx.jar, and mongodb-driver-core-xxx.jar listed. I have these listed and despite this, I keep getting the same error as in that stackoverflow post.
Any help would be appreciated.

com/mongodb/MongoClientURI class is present in mongo-java-driver jar. https://mongodb.github.io/mongo-java-driver/
Including the above jar in classpath should fix the issue

Related

JDBC problems connectiong to sql server [duplicate]

This question already has answers here:
Cannot load driver class: com.microsoft.jdbc.sqlserver.SQLServerDriver
(3 answers)
Closed 4 years ago.
i'm trying to establishing a connection from java to sql server. I'm using jdk 8 1.8 and sql server 2014, and this is my code:
package test.prova;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Provaconn {
public static void main(String[] args) throws Exception {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection m_Connection = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://srvdatiorim14.saga.locale;DatabaseName=mydbname", "myuser", "mypw");
Statement m_Statement = m_Connection.createStatement();
String query = "SELECT * FROM trasco_proprieta";
ResultSet m_ResultSet = m_Statement.executeQuery(query);
while (m_ResultSet.next()) {
System.out.println(m_ResultSet.getString(1) + ", " + m_ResultSet.getString(2) + ", "
+ m_ResultSet.getString(3));
}
}
but i get the following error:
Exception in thread "main" java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at test.prova.Provaconn.main(Provaconn.java:10)
i've already searched for a solution in various topic, i've tried lot of various adjustment, but still i got this error.
I've also imported the jdbc driver jar, from both maven dependency and external library (i tried different version of it as you can see):
any suggestion on how to solve this? T
Thank,
Serph
EDIT - SOLVED
after changing the Class.forName into te one suggest in the answer, i also modified the url which was wrong. Changed it from
jdbc:microsoft:sqlserver://...
to
jdbc:sqlserver//...
if you use this jar file: http://www.java2s.com/Code/Jar/s/Downloadsqljdbc420jar.htm
you must change:
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
to
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

java.lang.NullPointerException: Cannot invoke method addURL() on null object [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
Below are the first few lines of my groovy file. When i try to run it from the server, i get the null Pointer Exception: Cannot invoke method addURL() on null object
I am not sure how the code needs to be changed here to fix the above error:
package groovy;
def localFile = new File("/tmp/sqljdbc4.jar");
this.class.classLoader.rootLoader.addURL(localFile.toURI().toURL());
import java.sql.Connection;
import java.sql.DriverManager;
import com.microsoft.sqlserver.jdbc.*;
import groovy.sql.Sql;
Connection ret = null;
try {
SQLServerDataSource ds = new SQLServerDataSource()
......
Try using the class name instead of this for example if the class is named Groove use the line Groove.class.classLoader.rootLoader.addURL(localFile.toURI().toURL());

ERROR:: No suitable driver found for jdbc:.. localhost 1572//project [duplicate]

This question already has answers here:
How to fix: "No suitable driver found for jdbc:mysql://localhost/dbname" error when using pools? [duplicate]
(21 answers)
SQLException: No suitable driver found for jdbc:derby://localhost:1527
(19 answers)
Closed 6 years ago.
I have been looking forever how to connect to a database using java.
I Just used netbeans to create the data base, the url is
jdbc:derby://localhost:1527/project
However im using the terminal to compile and execute the java code instead of netbeans but I still have the error message that the no suitable driver found etc.. Here is the code. If anyone can help.
Thank you very much, it is really appreciated. I know that my question have been posted by I have tried everything and it dosnt work.
import java.sql.*;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
import com.mysql.jdbc.Driver;
public class Test {
public static void main (String[] args){
String host="jdbc:derby://localhost:1527/project";
String name="groupe3";
String password= "password";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn= DriverManager.getConnection(host,name,password);
Statement insert=conn.createStatement();
insert.executeUpdate("INSERT INTO Login VALUES('apple',2)");
insert.close();
}
catch(SQLException err){
System.out.println(err.getMessage());}
catch (ClassNotFoundException e) {
e.printStackTrace(); }
}
}
It looks like you've created a derby database, and are trying to use a mysql database driver.
https://db.apache.org/derby/docs/10.4/devguide/cdevdvlp40653.html

importing javax packages in android? [duplicate]

This question already has answers here:
javax.* cannot be imported in my Android app?
(5 answers)
Closed 7 years ago.
I am facing problem with some packages like Javax, when i am trying to import the following packages in my android applications!
Javax.naming.Naming.Exception;
Javax.naming.directory.DirContext;
Javax.naming.directory.InitialDirContext;
private DirContext connectLDAP()
throws NamingException
{
Properties localProperties = new Properties();
localProperties.setProperty("java.naming.factory.initial", LDAP_PROVIDER);
localProperties.setProperty("java.naming.batchsize", "0");
localProperties.setProperty("java.naming.provider.url", this.params.getLdapURL());
localProperties.setProperty("java.naming.factory.url.pkgs", "com.sun.jndi.url");
localProperties.setProperty("java.naming.referral", REFERRALS_IGNORE);
localProperties.setProperty("java.naming.security.authentication", "none");
return new InitialDirContext(localProperties);
}
help me to resolve this problem.
The classes that you are trying to use are not part of the Android SDK. You will need to find some other solution for whatever problem it is that you are trying to solve.

How to solve Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE?

I'm trying to upload a file using Selenium web driver. Above error receive. I know code is ok. Most of post saying it is because "dependencies". How can I solve it? I checked there was no same name jar files. Here I have show some screen shot of my jar files. How can I solve this dependencies problem?
Java code:
import java.io.File;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class FusekiStart {
public static void main(String[] args) {
/* Selenium config */
String PROXY = "localhost:3030";
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
.setFtpProxy(PROXY)
.setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(cap);
driver.get("http://localhost:3030/control-panel.tpl");
driver.findElement(By.xpath("//input[#value='Select']")).click();
driver.findElement(By.xpath("//input[#type='file']")).sendKeys("C:\\Users\\user1\\Desktop\\Ontology.owl");
driver.findElement(By.xpath("//input[#value='Upload']")).click();
}
}
Error message:
Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init> (DefaultHttpRequestWriterFactory.java:52)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<init>(DefaultHttpRequestWriterFactory.java:56)
at org.apache.http.impl.io.DefaultHttpRequestWriterFactory.<clinit>(DefaultHttpRequestWriterFactory.java:46)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:72)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<init>(ManagedHttpClientConnectionFactory.java:84)
at org.apache.http.impl.conn.ManagedHttpClientConnectionFactory.<clinit>(ManagedHttpClientConnectionFactory.java:59)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.<init>(PoolingHttpClientConnectionManager.java:487)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:147)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:136)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:112)
at org.openqa.selenium.remote.internal.HttpClientFactory.getClientConnectionManager(HttpClientFactory.java:68)
at org.openqa.selenium.remote.internal.HttpClientFactory.<init>(HttpClientFactory.java:55)
at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:99)
at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:82)
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:93)
at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:246)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:114)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:193)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:186)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:104)
at uom.brts.statisticalmt.FusekiStart.main(FusekiStart.java:39)
Check this link:
Source Code at Grep
You are missing BasicLineFormatter class from org.apache.http.message package.
I would say not this will fix your problem but can you see by including a httpcore 4.2.2 jar file is what your are using currently. Can you try to get the higher one from the Link and replace it with current httpcore 4.2.2 and see what difference it makes ?
I faced the same problem. After a lot of debugging, I found out that there is a CLASSPATH conflict. A jar file already existing in the project
httpclient-osgi-4.2.1.jar
was conflicting with selenium jar files. Fortunately for me, the httpclient-osgi-4.2.1.jar file is not required in the project anymore, so I could safely remove it. After that the problem is gone.
The error message is pathetic. It doesn't help in any way to point out the real problem.

Categories