Getting NoClassDef Error for classes imported through Maven - java

I have a Maven project in Eclipse created through m2e. It obtains client libraries from a project on GitHub called alternator. The classes seem to be imported fine, in my project, but on running them, it shows a NoClassDefFound error. Here's the code and the error:
Code:
public class Main{
private AlternatorDBClient client;
private DynamoDBMapper mapper;
private AlternatorDB db;
public static void main(String args[]) throws Exception{
new Main().run();
}
public void run() throws Exception {
this.client = new AlternatorDBClient();
this.mapper = new DynamoDBMapper(this.client);
this.db = new AlternatorDB().start();
}
}
Error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/michelboudreau/alternator/AlternatorDBClient
at Main.run(Main.java:17)
at Main.main(Main.java:13)
Caused by: java.lang.ClassNotFoundException: com.michelboudreau.alternator.AlternatorDBClient
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more

The AlternatorDBClient class is importing one or more classes which are not on your class path:
import com.amazonaws.*;
import com.amazonaws.handlers.HandlerChainFactory;
import com.amazonaws.http.ExecutionContext;
import com.amazonaws.http.HttpResponseHandler;
import com.amazonaws.http.JsonErrorResponseHandler;
import com.amazonaws.http.JsonResponseHandler;
import com.amazonaws.services.dynamodb.AmazonDynamoDB;
import com.amazonaws.services.dynamodb.model.*;
import com.amazonaws.services.dynamodb.model.transform.*;
import com.amazonaws.transform.JsonErrorUnmarshaller;
import com.amazonaws.transform.JsonUnmarshallerContext;
import com.amazonaws.transform.Unmarshaller;
import com.amazonaws.util.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
You are somehow missing:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
</dependency>
as a dependency in your project pom. As AlexR stated, please post your pom.

Related

After import of project, Cannot instantiate class error while running the test configuration

A maven project in eclipse written in Java to automate web application testing.
The project is imported as an existing maven project in Eclipse. The same imported project is working on my co-worker's system while facing the error on another system. Tried to verify other posts and tried to fix the issue, but still no luck.
[RemoteTestNG] detected TestNG version 6.8.0
org.testng.TestNGException:
Cannot instantiate class com.odlproject.tests.SmokeTest
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:186)
at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
at org.testng.TestRunner.initMethods(TestRunner.java:409)
at org.testng.TestRunner.init(TestRunner.java:235)
at org.testng.TestRunner.init(TestRunner.java:205)
at org.testng.TestRunner.<init>(TestRunner.java:160)
at org.testng.remote.support.RemoteTestNG6_5$1.newTestRunner(RemoteTestNG6_5.java:27)
at org.testng.remote.support.RemoteTestNG6_5$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_5.java:63)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:561)
at org.testng.SuiteRunner.init(SuiteRunner.java:157)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1273)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1260)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1114)
at org.testng.TestNG.run(TestNG.java:1031)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
... 21 more
Caused by: java.lang.NoClassDefFoundError: il/co/topq/difido/model/execution/Node
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at il.co.topq.difido.ReportManager.createReporterInstances(ReportManager.java:307)
at il.co.topq.difido.ReportManager.<init>(ReportManager.java:38)
at il.co.topq.difido.ReportManager.getInstance(ReportManager.java:46)
at com.selenium.commons.AbstractTestClass.<init>(AbstractTestClass.java:12)
at com.odlproject.tests.SmokeTest.<init>(SmokeTest.java:42)
... 26 more
Caused by: java.lang.ClassNotFoundException: il.co.topq.difido.model.execution.Node
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 33 more
This is test.java file
package com.odlproject.tests;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.WebDriver;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import com.odlproject.pages.DataSet;
import com.odlproject.pages.DeviceConfiurationPage;
import com.odlproject.pages.DrugLibrariesPage;
import com.odlproject.pages.HomePage;
import com.odlproject.pages.LoginPage;
import com.odlproject.pages.MasterDrugLibrary;
import com.odlproject.pages.ProfilesPage;
import com.selenium.commons.AbstractTestClass;
import com.selenium.commons.CommonCode;
import com.selenium.commons.Configuration;
import com.selenium.commons.Screenshot;
#Listeners(Screenshot.class)
public class SmokeTest extends AbstractTestClass {
public WebDriver driver = Configuration.browser();
public LoginPage login;
public HomePage home;
public MasterDrugLibrary master;
public DrugLibrariesPage dlp;
public CommonCode common;
public DeviceConfiurationPage dc;
public ProfilesPage profile;
public DataSet ds;
public SmokeTest() {
ds = new DataSet();
login = new LoginPage();
home = new HomePage();
master = new MasterDrugLibrary();
dlp = new DrugLibrariesPage();
common = new CommonCode();
dc = new DeviceConfiurationPage();
profile = new ProfilesPage();
}
#BeforeSuite(alwaysRun = true)
public void invokeBrowser() {
driver.get(Configuration.LoginURL());
//driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
}
#BeforeMethod(alwaysRun = true)
public void navigtoHome() {
driver.manage().deleteAllCookies();
driver.get(Configuration.LoginURL());
login.loginToAPP(Configuration.username, Configuration.password);
// home.validateHomePage();
// common.waitMethod(3);
}
#AfterSuite(alwaysRun = true)
public void closeBrowser() {
driver.quit();
}
#Test(testName = "logout", description = "logout", groups = { "sanity", "1" }, priority = 1)
public void logout_LoginBack() {
home.logout();
login.loginToAPP(Configuration.username, Configuration.password);
}
If there are any additional details required, please do comment, and I will add here.
Thank you.

Unable to execute JasperReports: ClassNotFoundException [duplicate]

This question already has an answer here:
NoClassDefFoundError: com/itextpdf/text/DocumentException when exporting jasper to pdf
(1 answer)
Closed 5 years ago.
Trying to execute JasperReports 6.3.1 simple report from Eclipse:
package test.jaspertest;
import java.util.HashMap;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
public class JasperTest {
public static void main(String[] args) {
System.out.println("Start");
try {
JasperReport jasperReport = JasperCompileManager.compileReport("reports/simple.xml");
JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperReport,
new HashMap(),
new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint, "reports/simple.pdf");
} catch (JRException e) {
e.printStackTrace();
}
}
}
Have this out message:
Apr 11, 2017 1:59:41 PM net.sf.jasperreports.extensions.DefaultExtensionsRegistry loadRegistries
WARNING: Extension resource jar:file:/C:/Portable%20Programs/eclipse/packages/jasperreports/dist/jasperreports-javaflow-6.3.1.jar!/jasperreports_extension.properties was found to be a duplicate of jar:file:/C:/Portable%20Programs/eclipse/packages/jasperreports/dist/jasperreports-6.3.1.jar!/jasperreports_extension.properties in classloader sun.misc.Launcher$AppClassLoader#1d16e93
Start
Exception in thread "main" java.lang.NoClassDefFoundError: com/lowagie/text/DocumentException
at net.sf.jasperreports.engine.JasperExportManager.exportToPdfFile(JasperExportManager.java:150)
at net.sf.jasperreports.engine.JasperExportManager.exportReportToPdfFile(JasperExportManager.java:503)
at test.jaspertest.JasperTest.main(JasperTest.java:24)
Caused by: java.lang.ClassNotFoundException: com.lowagie.text.DocumentException
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
Java version 1.8.0.121. I'm not very familiar with Java so please can anyone help me with this issue? Thanks in advance!
You miss the itext-2.1.7.jar in your classpath. add it and the error will go away.
The itext.jar is a dependecy which is needed by jarperReport

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

package photo;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.github.sarxos.webcam.Webcam;
public class capture {
public static void main(String arg[]) throws IOException {
Webcam webcam = Webcam.getDefault();
webcam.open();
BufferedImage image = webcam.getImage();
ImageIO.write(image, "JPG", new File("test.jpg"));
}
}
I have written this java code to capture photo through webcam, but this code is throwing an error/exception please tell how can I fix it.?
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.github.sarxos.webcam.Webcam.<clinit>(Webcam.java:97)
at photo.capture.main(capture.java:12)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
Download SLF4J and put the slf4j-api-1.7.2.jar file onto your class path.

Getting ClassNotFoundException while using Class.forName("com.actitime5.Testcases."+vriable);

I've been messing around with ClassLoaders in java recently, trying to run my automation script code which uses dynamic loading of classes and geting ClassNotFoundException.
##MyCode
package com.actitime5.frameworkengine;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import com.gmail5.Testcases.LoginLogout;
import org.junit.runner.JUnitCore;
public class Init {
public static void main(String args[]) throws Exception
{
Init in=new Init();
String url=in.getConfigValue("url");
ExcelLibrary lib=new ExcelLibrary();
JUnitCore core = new JUnitCore();
int numberofTestcases=lib.getRowCont("Scenario");
System.out.println(numberofTestcases);
for(int i=1;i<=numberofTestcases;i++)
{String status=lib.getExcelData("Scenario", i, 1);
if(status.equals("yes"))
{String scriptnam=lib.getExcelData("Scenario", i, 0);
System.out.println("Sheet Name"+" "+scriptnam);
Class scriptToRun= Class.forName("com.gmail5.Testcases."+scriptnam);
core.run(scriptToRun);
}
}}
##Errors that i am getting :
1
Sheet Name LoginLogout
Exception in thread "main" java.lang.ClassNotFoundException:com.gmail5.Testcases.LoginLogout
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.gmail5.frameworkengine.Init.main(Init.java:24)
The class which I am trying to load is available in package: com.gmail5.Testcases.*;,but still it throws exception
I think your class name has trailing whitespace. Try trim()ming it off before attempting to load the class.
You can confirm this by replacing
System.out.println("Sheet Name"+" "+scriptnam);
with
System.out.println("Sheet Name"+" '"+scriptnam+"'");
I expect this will print out something like the following:
Sheet Name 'LoginLogout '
When I clicked 'edit' to edit your question, I could see there was trailing whitespace after the class name in the stack trace and in your Sheet Name message. (I didn't change anything in your question: I just used the Edit function to see the raw text of your question, and then clicked Cancel.)

Error Invoking Maven

I am trying to invoke maven from a Java class.
My code:
import java.io.File;
import java.util.Arrays;
import javax.xml.ws.spi.Invoker;
import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.DefaultInvoker;
import org.apache.maven.shared.invoker.InvocationRequest;
import org.apache.maven.shared.invoker.MavenInvocationException;
public class MavenProjectBuilder implements ProjectBuilder{
public void BuildProject(String buildPath, String bugID) {
InvocationRequest request = new DefaultInvocationRequest();
request.setPomFile(new File(buildPath));
request.setGoals(Arrays.asList( "clean", "install" ));
DefaultInvoker invoker = new DefaultInvoker();
try {
invoker.execute(request);
} catch (MavenInvocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Which returns the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/plexus/util/cli/StreamConsumer
at autoEvoSuite.MavenProjectBuilder.BuildProject(MavenProjectBuilder.java:21)
at autoEvoSuite.Main.<init>(Main.java:110)
at tests.GeneralTest.main(GeneralTest.java:18)
Caused by: java.lang.ClassNotFoundException: org.codehaus.plexus.util.cli.StreamConsumer
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
I did some research and found out it could be a missing dependency, but shouldn't Maven automatically download all missing ones?
Please check if the below dependency is present in your POM.xml, until the dependency is defined in you pom file that jar will not be downloaded to your local repository.
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.0.17</version>
</dependency>

Categories