java.lang.NoClassDefFoundError when using MongoDB driver - java

I am trying to connect to a MongoDB database hosted on mlab using the Java driver on a servlet.
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class MongoConnection {
protected void connectToMongo(String loc){
String dbName = "readings";
String collection = "data";
MongoClientURI uri = new MongoClientURI("mongodb://user:pass#ds143109.mlab.com:43109/readings");
MongoClient client = new MongoClient(uri);
MongoDatabase db = client.getDatabase(dbName);
MongoCollection<Document> readings = db.getCollection(collection);
Document doc = Document.parse(loc);
readings.insertOne(doc);
client.close();
}
}
The problem is I am getting the following error:
java.lang.NoClassDefFoundError: com/mongodb/MongoClientURI
I looked at one answer (How to resolve ClassNotFoundException: com.mongodb.connection.BufferProvider?) that highlighted to me that I need other jars, I have since downloaded them however I am still getting this error.
I am using Eclipse and adding the three jars to the build path, navigating through the menu by right clicking on the project then following Build Path -> Configure build path -> Java build path -> libraries -> add external JARs.
Is this the right way to do it? Is there something else I am supposed to do as well/instead?

You have java.lang.NoClassDefFoundError - that means your class is missed during runtime (not during build/compile time). So you should open your "Run Configurations" dialog for the project (project context menu -> "Run As" -> "Run Configurations...") and make sure you have bson-xxx.jar, mongodb-driver-xxx.jar, and mongodb-driver-core-xxx.jar somehow listed in Classpath tab. And yes, like Xavier Bouclet said - if you run it under application server - this jars should be added to your server's classpath.

You have to make sure that the mongodb jars are exported to server if your call to the database are made from the servlet.
Check how you deploy your app on your local server dans make sure the jars are there.

I was facing similar issue with my Mule 4 project.
Failed to invoke lifecycle phase "initialise" on object:
That was pointing to:
java.lang.NoClassDefFoundError: com/mongodb/MongoClientURI
So I had to updated POM file in plugin section (mule-mave-plug, idk what would it be in java project):
<sharedLibraries>
<sharedLibrary>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-legacy</artifactId>
</sharedLibrary>
</sharedLibraries>

Related

chrome Webdriver can't be resolved to a type error eclipse and java

Im trying to do some automation projects with chrome, java and selenium and having problems importing the chrome driver.
package main;
import org.openqa.selenium.*;
public class SitePoster {
public static void main(String[] args) {
//System.setProperty("webdriver.chrome.driver", "./pathtodriver");
WebDriver driver = new ChromeDriver();
//Getting error saying "ChromeDriver can't be resolved to a type"
}
}
chrome Webdriver can't be resolved to a type error eclipse and java:-
I was getting same problem with selenium -java 3.141.59 on macOs.Check below steps to resolve issue:-
Create a new Java Project >class >Create Class name
Right click on Project >Property > Build path > Libraries
Add external Jars to Classpath download from selenium client & web
driver language binding.
Apply and Close.
Hope you got your Issue fixed. Kindly reply back in case of any issue.
Even after installing the required jar files I was facing the same error message and then I updated the compiler compliance level in the Java compiler to 1.8(depends) and then error disappeared. It solved my issue.
You can follow below steps:
Select the Java project(created one)
Build Path
Configure build path
Java compiler
Change the compiler compliance level(I selected 1.8)
Apply and Close.
The solution for me was the POM specified an older version of ChromeDriverManager which was how chromeDriver was obtained in my build. If you are specifying a location using setProperty this will of course not apply to you. But it helps to check with the version of the dependencies in your POM
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.6.1</version>
<scope>test</scope>
</dependency>
That did it!
Add Selenium Jar Files to ClassPath instead of ModulePath
ChromeDriver is one extra level nested in org.openqa.selenium package. Try with import statement
org.openqa.selenium.chrome.*;
OR
import org.openqa.selenium.chrome.ChromeDriver;
The error says it all :
"ChromeDriver can't be resolved to a type"
While working with Selenium 3.x you have to mention the Key-Value pair through System.setProperty() line mandatory as follows :
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
Additionally, as per best practices instead of import org.openqa.selenium.*; you have to mention the individual packages for import as follows :
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
I was getting the exact same error. It did resolve for me after performing the below steps.
Right click on Project>Buildpath>configure build path
Libraries>Add external Jars.
Add the external Jars from the 'libs' folder first, then add the 'Client component' ones.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class test1 {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\nsukumar\\Documents\\selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://google.co.in");
}
}
Try to edit external jars in the ModulePath & Classpath.
See on the picture link -->
You need to attach jar files. I was getting the exactly the same issue.
you need to install "Selenium 3.4.0" jar files.
Which contains lib folder of .jar files and "client-combined-3.4.0-nodeps" files.
After you attach these jar files refresh the code by giving run command.
Adding Selenium jar files from the build path > add external libraries fix the issue for me.
For Linux user go to terminal check google version with below command
google-chrome --version
OP:- Google Chrome 69.0.3497.100
Now, Upgrade chrome using below command
sudo apt --only-upgrade install google-chrome-stable
After this check the google chrome version using above same command
google-chrome --version
OP:- Google Chrome 78.0.3904.87
Now you need to download the same version google driver. you can find it from the below link.
https://chromedriver.storage.googleapis.com/index.html?path=78.0.3904.70/
Download google driver and extract it and save the path and use it in the code, Now run the code.
if you are not able to add this statement
import org.openqa.selenium.chrome.ChromeDriver;
the probable reason is you are using maven and you have not included the below
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.141.59</version>
</dependency>
Even if we put the below statement, problem still comes
System.setProperty("webdriver.chrome.driver", "C:\\Software\\chromedriver_win32\\chromedriver.exe");
So the solution is goto maven repository site and take the chrome dependency from there to solve this problem
This type of error occurs when you have added the external jars in the ModulePath. To resolve this issue, you can remove the external jars from the node "Modulepath". Select the node "Classpath" then add the external jars. Review that all the jars are under the node "Classpath"
When adding external jars, make sure you add the jars in the lib folder first before adding the ones in the main selenium-java folder. This made the difference for me.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class testdemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","C:\\Users\\victo\\OneDrive\\Documents\\driverchrome\\ChromeDriver.exe");
WebDriver driver=new ChromeDriver();
driver.get(" https:google.com");
I faced this error when using Latest Eclipse 2021-03 and Selenium 3
To resolve this, add your Selenium standalone jar file in Classpath instead of Module path. also do not forgot to delete the existing Jar files in modulepath, apply and then apply&close.
Even after installing the required jar files I was facing the same error message and then I updated the compiler compliance level in the Java compiler to 1.8(depends) and then error disappeared. It solved my issue.
You can follow below steps:
Select the Java project(created one)
Build Path
Configure build path
Java compiler
Change the compiler compliance level(I selected 1.8)
Apply and Close.
This answer helped me as well. I changed my compliance level from 15 to 1.8.
Ran in to this problem today and it turns out I simply forgot to add the .jar files from the 'lib' folder. Hope this helps someone.

Cant init context with JMS

i have problems with setting up JMS. I tried using Active MQ and OpenJMS but the same error occurs. Iam clueless on how to solve it:
When i execute this:
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Hashtable;
public class Main {
public static void main(String[] args) throws NamingException {
Hashtable properties = new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"org.exolab.jms.jndi.InitialContextFactory");
properties.put(Context.PROVIDER_URL, "tcp://localhost:3035/");
Context context = new InitialContext(properties);
}
}
I get this error:
Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: org.exolab.jms.jndi.InitialContextFactory [Root exception is java.lang.ClassNotFoundException: org.exolab.jms.jndi.InitialContextFactory]
After some googling i think the problem is that a jar is not in the right classpath, but all the answers already assume that i know which jar and where to store it, but this is not the case here.
Which Jar are they talking about? Where can i get it? Where do i need to store it?
Iam using IntelliJ btw. Could you point me in the right direction?
ActiveMQ is working (http://localhost:8161/ shows webpage of ActiveMQ)
Here are my steps on how i created the project:
File -> New Project -> Java -> SDK1.8 -> Next -> Next -> Project Name: Test -> Right click src folder -> New -> Java Class -> Name: Main -> Paste code from above -> Run -> Error
EDIT: for all the people having the same errors: OpenJMS comes with a libfolder. Install these libraries and it should work
Here is a good explanation on how to add external jars to your project in intellij, and regarding to
Which Jar are they talking about? Where can i get it?
here is a useful web site where you can search jar files using criterias as the name of a class or a package.
I think the jar you are needing is Openjms, here is a link to download it.

How do I install TestFX?

I'd like to put together a set of jUnit tests under Eclipse (Neon) on Windows to automate the testing of JavaFX GUIs. It seems that TestFX is the bee's knees for this kind of thing, but having looked around the internet quite a bit, I'm still not sure how to install TestFX without using Maven or Gradle.
I am not familiar with Maven and Gradle, and trying to follow the simple instructions to install TestFX via Maven was unsuccessful. This was done under Eclipse Mars, after which my simple GUI program threw up a compile error about not being able to find or load main class and a run-time error that Selection does not contain a main type. (The simple GUI program ran without errors previously.) After this, I downloaded the latest Eclipse Neon and tried to start afresh.
This is what I did:
Download and install Eclipse Neon from eclipse.org.
Create a Java Project called TestProject (execution environment JavaSE-1.8).
Grant access to javafx/** (right-click on project -- properties -- Java Build Path -- Libraries -- JRE System Library -- Access Rules -- add).
Create MyClass with minimal contents:
package test;
import javafx.application.Application;
import javafx.stage.Stage;
public class MyClass extends Application {
#Override
public void start(Stage stage) throws Exception {
stage.setTitle("Hello World");
stage.show();
}
}
Copy the file testfx-core-4.0.0-20150226.214553-8.jar from the testfx repository (linked from the Via direct download section of How to use TestFX in your project) into my eclipse project, sitting at the same level as the JRE System Library;
Add the jar file to the build path (right-click on project -- properties -- Java Build Path -- Libraries -- Add JARS) - this automatically created a directory called Referenced Libraries and copied the jar file into it; and
Create a JUnit test (right-click on source folder -- New -- JUnit Test Case), filling in the appropriate package and Class under test fields, and giving the test case a name (MyTest).
Then I tried to extend the class MyTest to use testfx:
class MyTest extends GuiTest {} as advised in this link; and (separately)
class MyTest extends ApplicationTest{} as advised in this link.
Here's the code in the second case:
package test;
import org.junit.Test;
import org.testfx.framework.junit.ApplicationTest;
public class MyTest extends ApplicationTest { }
In each case, eclipse complains that the superclass GuiTest / ApplicationTest cannot be resolved to a type.
I suspect that the problem is that I haven't properly installed testfx. Can anyone help?
There are multiple dependencies; the following jar files must be added to the Referenced Libraries (after copying the jar files into the project, right-click on the project -- Properties -- Java Build Path -- Libraries -- Add JARs...).
testfx-core-4.0.4-alpha.jar, source: http://mavensearch.io/repo/org.testfx/testfx-junit/4.0.4-alpha
testfx-junit-4.0.4-alpha.jar, source: http://mavensearch.io/repo/org.testfx/testfx-core/4.0.4-alpha
guava-18.0.jar, source: http://mavensearch.io/repo/com.google.guava/guava/18.0
hamcrest-core-1.3.jar, source: http://mavensearch.io/repo/org.hamcrest/hamcrest-all/1.3
The above is independent of Gradle and Maven. Alternatively, the files can also be pulled using Gradle:
Create a Gradle project in Eclipse.
In the dependencies block in build.gradle, insert the following lines (source):
testCompile "org.testfx:testfx-core:4.0.+"
testCompile "org.testfx:testfx-junit:4.0.+"
Right-click the project -- Gradle -- Refresh Gradle Project
This places the required files into the Project and External Dependencies folder.
Useful tutorial for TestFX 4: https://www.youtube.com/watch?v=NG03nNpSmgU.

The type org.apache.xmlrpc.common.XmlRpcHttpRequestConfigImpl cannot be resolved. It is indirectly referenced from required .class files

I have physically copy xmlrpc-client-3.1.3.jar to./lib,then right click on it to select "Build Path>Add Path".
On Menu Bar,select Project >Properties>Java Build Path.
On "Libraries" tab ,xmlrpc-client-3.1.3.jar found and
"Order and Export" tab has checked on xmlrpc-client-3.1.3.jar-XXX/lib
By the time,I code as below
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
public class someClass{
....
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://somedomain.com/index.php/api/xmlrpc/"));
}
I get the error
The type org.apache.xmlrpc.common.XmlRpcHttpRequestConfigImpl cannot
be resolved. It is indirectly referenced from required .class files
I use eclipse on project development
I had same issue but now I have managed to resolve it. Your just missing the xmlrpc client common jar file.
Just download the following package
xmlrpc-client-common-3.1.3.jar
Add it to your project using same procedures you did for adding xmlrpc-client-3.1.3.jar
CHEERS
And you can download it here. Using xmlrpc client jar alone will give you error; so just download the xmlrpc common, add jar as external, clean your project and your ready to go. happy codings,
-cheers

java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException with eclipse and spring

I have a simple XML file that I have parsed to JSON. All is fine and dandy, I have a Java class that is stand alone (i.e. it has a public static void main (String args[])....)
This has a private constructor (because I need to call it with Strings either a filename or the actual data). So I have two methods that return an instance of the object. I know a bit of Java as you can tell.
OK. When I run the code in Eclipse that runs the main method my file is loaded and decoded as required. It also works for a raw String that I run via JUnit.
So I know the following facts -
the parsing of a static String works and decodes perfectly
if I provide a file it is loaded and decoded correctly.
Now the issue:
As soon as I run it in Spring framework I can write to standard out the entire file content that I have run via the stand alone code.
But before it can run anything at all I get the below error -
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:920)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
The stand alone code is run in Eclipse, and the Spring is run pointing to that code using Tomcat 7.
Why is it not finding the ParsException correctly?
The imports in the calling Spring controller are
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.commons.lang.StringUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
Is their a way of altering the build order, and would that fix it?
Looks like you have missed including the json-simple-.jar in your classpath. Include the same and it should get solved.
Hope this helps !
Add json-simple-1.1.1.jar file in your build path. On Eclipse, right click on project-> build path -> configure build path -> java build path -> add external jars -> select the jar file.
Add build path entry into deployment assembly. On the same properties window, Select "deployment assembly" option. Then add "Java Build Path" entries. This should show you the build path entry you just made in step one.
you are missing a jar deployment.
it is related for Tomcat7. A few jar needed to be exported ( forgot which ones) other web server doesn't need but Tomcat.
Could you please add this jar json-simple-1.1.1.jar in your classpath and try it out ?
This jar can be downloaded at http://code.google.com/p/json-simple/downloads/detail?name=json-simple-1.1.1.jar
The solution that I used in the end was to change to sourceforge.net json parser, a couple of code tweaks and my JUnit tests still worked and Tomcat did not complain. This may not be the best solution but it worked.
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
I have had a similar error and spent 3 4 hours removing and adding new jar files. What my problem was that i added jar file directly from the download location ie THE ZIP files ..!!! after adding a proper extracted jar file , My problem seems to be resolved. Hope this helps someone..!!
IF you are making an AI in Spring as I was when I had this error:
You also need to add the Jar to your AI "jlib" folder, for instance "...\AI\Skirmish\MyAI\0.1\jlib".
It's not enough to add it in Eclipse or Netbeans and build the project. Spring must have the jar too.
I know it is a late answer it but can help other people with the same error.
Use the below java library for process JSONArray and JSONObject
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20200518</version>
</dependency>
Just add maven dependency of JSON simple in your pom.xml. No need to configure any classpath.
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>

Categories