I'm trying to execute my testng.xml file from main method using the code below. But getting a NoClassDefFoundError. Any idea what may have caused this issue?
public static void main( String[] args )
{
TestNG testng = new TestNG();
List<String> suites = Lists.newArrayList();
suites.add("testng.xml");
testng.setTestSuites(suites);
testng.run();
}
Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/TestNG
at com.automation.app.App.main(App.java:21)
Caused by: java.lang.ClassNotFoundException: org.testng.TestNG
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
The issue here is that you are missing the dependent libraries on your class path when you are executing the jar because the jar only has the classes from you project.
What you need here is either
build an executable jar with all the dependencies
or
specify the dependencies on command line
e.g.
java -cp your.jar:lib/*
where libs/* is a path to your dependency JARs
I would recommend option one as you can run that jar any where if it has all the dependencies included in jar.
I was able to fix this issue by using "maven-assembly-plugin" and "maven-dependency-plugin", Then creating the manifest.mf file in resources/meta-inf folder and adding the following information.
Manifest-Version: 1.0
Class-Path: .
Main-Class: {PROVIDE THE MAIN CLASS HERE}
Its related to the Testng Library.
Add library:
Right click project -> Build path -> Configure build path ->Java Build Path -> Library -> Add library-> select TestNG -> Apply and close.
It worked for me.
Related
I have a Spring Boot Application that is working.
I did
mvn clean package
and the .jar file is my target folder.
Then I try to execute the following command:
java -jar .\target\my-application-0.0.1-SNAPSHOT.jar
and I get the following Error:
Exception in thread "main" java.lang.ClassNotFoundException: C:\Users\test\example-application\example-app/src/main/test/ExampleApplication
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:467)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:46)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65)
Researching on stackoverflow just recommend an issue that I already included ...
It was recommended to include this plugin in order to include the maven jars into the project .... spring-boot-maven-plugin
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
What else could be the reason for it not working?
The class name in the exception is a file name and not a class name. That makes me suspect that you have specified the main class incorrectly. Have you specified the name of the class in the spring-boot-maven-plugin with
<configuration>
<mainClass>foo.bar.ExampleApplication</mainClass>
</configuration>
or as a Main-Class in the manifest? Note that you should use dot notation for packages...
I had a similar issue to this a few weeks ago. Spring looks for the source for most classes to be in the /src/main folder.
It looks like you're missing the file from that folder. In my case, I had placed it in just /src instead of /src/main
I am facing and issue when trying to read data form Mongo DB.
I have Spark 1.6.3 which has Scala 2.10.5
I am using the Mongo DB Connector Version 1.1 and package 2.10
Following is the dependencies i had used in my Mavan
<dependency>
<groupId>org.mongodb.spark</groupId>
<artifactId>mongo-spark-connector_2.10</artifactId>
<version>1.1.0</version>
</dependency>
Getting error like
java.lang.NoClassDefFoundError: com/mongodb/spark/rdd/api/java/JavaMongoRDD
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.apache.spark.util.Utils$.classForName(Utils.scala:175)
at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:708)
at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:181)
at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:206)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:121)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
Caused by: java.lang.ClassNotFoundException: com.mongodb.spark.rdd.api.java.JavaMongoRDD
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
But was able to find the class existing in the reference
Thanks in Advance
Add mongo-spark-connector_2.10's jar to your run time class path
It can be done it followting ways,
Add mongo-spark-connector jar to Spark lib folder
spark-submit with --jars=/path/mongo-spark-connector.jar
spark.driver.extraClassPath or it's alias --driver-class-path to set extra classpaths on the node running the driver.
spark.executor.extraClassPath to set extra class path on the Worker nodes.
If you add Driver class path then you will have need to add executor class paths to make jar available to executor on worker nodes
Try to use the latest version jar 2.2.8 which shall have the class
mongo-spark-connector_2.10:2.2.8
The reason for this issue is,
When i am trying to build using Maven , i am not using Fat Jar build using tools.
Instead of Maven build i tried the following to build a FAT Jar using Eclipse
File -> Export -> Runnable Jar -> Select Class , Jar file Name and choose Option 1 in Library handling. Then click finish.
This is a old type of Jar , but it fixed the issue for me.
Thanks all for the assistance.
package springdemo4;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloSpringApp {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Coach theCoach = context.getBean("myCoach",Coach.class);
System.out.println(theCoach.getDailyFortune());
System.out.println(theCoach.getDailyWorkout());
context.close();
}
}
I ran it, then it said:
Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for C:\Users\ngoch\eclipse-workspace\springdemo4\libs\spring-context-indexer-5.1.9.RELEASE-sources.jar
Caused by: java.lang.module.InvalidModuleDescriptorException: Provider class org.springframework.context.index.processor.CandidateComponentsIndexer not in module
Please,help
I had the same issue with Eclipse IDE. I mistakenly added the spring jar files under Modulepath. It should be under Classpath (Properties > Java Build Path > Classpath > addJARs ).
You need to add the libraries under classpath and not under modulepath.
Spring libraries should be under classpath
Below steps worked for me:
Go to project properties -> Libraries -> add your user libraries under classpath. Remove your librarey reference from Modulepath.
Project properties from eclipse
I cannot find the issue here:
I created a jar using a maven plugin, but the dog won't hunt.
The class:
package com.foo.baitshop;
public class Design {
public static void main(String args[]) throws IOException {
int argLength = args.length;
}
}
I unzipped the jar to peek inside and the META-INF folder is in the root and contains the manifest (MANIFEST.MF).
It looks like this:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: lwhite
Build-Jdk: 9.0.4
Main-Class: com.foo.baitshop.Design;
To Run, i execute the following in the directory holding the jar:
java -jar baitshop2-1.0-SNAPSHOT-jar-with-dependencies.jar
There is no classpath set externally in this environment.
The error message is:
fm-mltp140:target lwhite$ java -jar baitshop2-1.0-SNAPSHOT-jar-with-dependencies.jar
Error: Could not find or load main class com.foo.baitshop.Design;
Caused by: java.lang.ClassNotFoundException: com/foo/baitshop/Design;
Any help would be greatly appreciated.
I think it's the semi-colon:
Main-Class: com.foo.baitshop.Design;
It needs to be removed to be:
Main-Class: com.foo.baitshop.Design
If Maven is generating this, the Maven configuration needs to be debugged to prevent adding the semi-colon. Likely there is a semi-colon in the configuration somewhere.
The associated doc from Oracle.
I'm having a runtime issue occurring with SnakeYAML and maven. For some reason, I'm getting a java.lang.NoClassDefFoundError with Yaml when I run my .jar.
Here is the stack trace:
Exception in thread "main" java.lang.NoClassDefFoundError: org/yaml/snakeyaml/Yaml
at my.package.TimberServer.loadConfiguration(TimberServer.java:56)
at my.package.TimberServer.<init>(TimberServer.java:38)
at my.package.Main.main(Main.java:17)
Caused by: java.lang.ClassNotFoundException: org.yaml.snakeyaml.Yaml
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 3 more
Here is the section in my pom.xml in which I declare the dependency (link to central repository):
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.14</version>
</dependency>
I don't declare any repository for it.
Here is the code that calls (and imports) the Yaml class:
import org.yaml.snakeyaml.Yaml;
// everything else is imported, yes
public class TimberServer {
public void loadConfiguration() throws IOException {
Yaml yaml = new Yaml(); // Error occurs HERE.
Map config = (Map) yaml.load(FileUtils.readFileToString(this.config));
Map<String, String> serverConfig = (Map<String, String>) config.get("server");
}
}
I get no errors when compiling with maven, which is the most confusing part to me - does Java not check for existent classes? I'm programming with an IDE (IntelliJ), which does not give any errors or warnings with the lines of code.
Are you including SnakeYaml in the JAR you're trying to run? If not, you'll need to 1) use Maven's uber-jar plugin to include it, 2) add the YAML JAR to your classpath when running the JAR or 3) use Maven's exec plugin to run your JAR so it can pick up the dependencies.
After help from #Seelenvirtuose in the comments, I came across this solution:
I was running mvn package alone - with no dependency plugin, which I learned is a necessity for packaging. However, in my specific case, the Uber jar plugin did not want to work right and I had to use the Assembly plugin.
Note: the resultant .jar from the Assembly plugin will be named target/yourProject-jar-with-dependencies.jar. I overlooked this.