This question already has answers here:
java.lang.NoClassDefFoundError: com/google/common/base/Function
(1 answer)
How to solve java.lang.NoClassDefFoundError? Selenium
(1 answer)
Closed 3 years ago.
I am getting an error "Exception in thread "main" java.lang.NoClassDefFoundError", when running a small selenium.
Added external jar file is client-combined-3.141.59.jar
If adding some more jar files like selenium-server-standalone-3.141.59, okio-1.14.1.jar errors are increasing.
package seleniumBasic;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class selenium {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.manage().window().maximize();
}
Probably you did not add the library correctly to your build path. From where did you get that jar and how did you add it to your project?
Anyway, I would suggest to use gradle to add these libraries. Just add the gradle nature to your project and use following example build.gradle file:
plugins {
id 'java-library'
}
dependencies {
implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59'
}
repositories {
jcenter()
}
Related
I am trying to create a test class (JUnit 5) using Intellij Idea but I get the bellow error. When I created the test class it did NOT show the fix button so I am pretty sure the library is in class path.
Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/platform/launcher/TestExecutionListener
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:802)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:700)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:623)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:45)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.launcher.TestExecutionListener
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 13 more
My build.gradle looks as below:
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.7'
}
group 'ict221'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.11
mainClassName = 'boardgame.gui.RunGame'
repositories {
mavenCentral()
}
test {
useJUnitPlatform()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
}
javafx {
modules = [ 'javafx.controls', 'javafx.fxml' ]
version = "11.0.2"
}
Any help is much appreciate it.
As mentionned by #CrazyCode in comments, with Intellij you need to specify the version of JUnit (because Intellij is bundled with old version of JUnit)
Add this :
dependencies {
...
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.6.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.1")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.6.1")
}
try entering these depencies for junit5 in a gradle project
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.0")
testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.7.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.2")
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.7.2")
Below is an example of a build.gradle for a simple application with JDK 17, Gradle 7.5.1 and JUnit Jupiter 5.9.0, which causes IntelliJ IDEA to correctly run unit tests with JUnit.
Notice that there is no need to add test dependencies as it is done automatically by configuring testing section.
build.gradle
plugins {
id 'application'
}
group = 'your.app'
version = '0.0.1'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
}
testing {
suites {
test {
useJUnitJupiter('5.9.0')
}
}
}
application {
mainClass = 'your.app.App'
}
src/main/test/your/app/AppTest.java
package your.app;
import org.junit.jupiter.api.Test;
class AppTest {
#Test
void testFeature() {
}
}
I am new to Jenkins.
I have sample selenium code like below in Java project:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SampTest
{
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.get("http://www.google.com");
System.out.println(driver.getTitle());
driver.quit();
}
}
This works perfect.
I configured like below in Jenkins:
Create a new project with name.
In Advanced options, added Workspace as "D:\UD\ProgrammingSamples\Selenium\SeleniumPractice\src"
In build section, i used Execute Windows batch command like "javac SampTest.java | java SampTest"
(Basically i dont know what to use here. Can someone help me here).
When i build the project now i see an error message saying below:
Started by user anonymous
Building in workspace D:\UD\ProgrammingSamples\Selenium\SeleniumPractice\src
[src] $ cmd /c call C:\Users\user\AppData\Local\Temp\hudson578216989100659838.bat
D:\UD\ProgrammingSamples\Selenium\SeleniumPractice\src>javac SampTest.java | java SampTest
java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" SampTest.java:1: error: package org.openqa.selenium does not exist
import org.openqa.selenium.WebDriver;
^
SampTest.java:2: error: package org.openqa.selenium.firefox does not exist
import org.openqa.selenium.firefox.FirefoxDriver;
^
SampTest.java:8: error: cannot find symbol
WebDriver driver=new FirefoxDriver();
^
symbol: class WebDriver
location: class SampTest
SampTest.java:8: error: cannot find symbol
WebDriver driver=new FirefoxDriver();
^
symbol: class FirefoxDriver
location: class SampTest
4 errors
D:\UD\ProgrammingSamples\Selenium\SeleniumPractice\src>exit 1
Build step 'Execute Windows batch command' marked build as failure
Finished: FAILURE
I wrote a Java example, the code is:
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.util.List;
class JythonExample {
public static void main(String args[]) throws ScriptException {
listEngines();
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine pyEngine = mgr.getEngineByName("python");
try {
pyEngine.eval("print \"Python - Hello, world!\"");
} catch (Exception ex) {
ex.printStackTrace();
}
final PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("print \"Python - Hello, world!\"");
PyObject result = interpreter.eval("2 + 3");
System.out.println(result.toString());
}
public static void listEngines(){
ScriptEngineManager mgr = new ScriptEngineManager();
List<ScriptEngineFactory> factories =
mgr.getEngineFactories();
for (ScriptEngineFactory factory: factories) {
System.out.println("ScriptEngineFactory Info");
String engName = factory.getEngineName();
String engVersion = factory.getEngineVersion();
String langName = factory.getLanguageName();
String langVersion = factory.getLanguageVersion();
System.out.printf("\tScript Engine: %s (%s)\n",
engName, engVersion);
List<String> engNames = factory.getNames();
for(String name: engNames) {
System.out.printf("\tEngine Alias: %s\n", name);
}
System.out.printf("\tLanguage: %s (%s)\n",
langName, langVersion);
}
}
}
In my pom.xml, if I use:
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.0</version>
</dependency>
then I can run java -jar target/jython-example-1.0-SNAPSHOT.jar successfuly, by the way, I used maven-assembly-plugin to build a runnable jar.
if I use:
<dependency>
<groupId>org.python</groupId>
<artifactId>jython</artifactId>
<version>2.7.0</version>
</dependency>
then when I run java -jar target/jython-example-1.0-SNAPSHOT.jar, I'll always get the following error:
ScriptEngineFactory Info
Script Engine: jython (2.7.0)
Engine Alias: python
Engine Alias: jython
Language: python (2.7)
ScriptEngineFactory Info
Script Engine: Oracle Nashorn (1.8.0_31)
Engine Alias: nashorn
Engine Alias: Nashorn
Engine Alias: js
Engine Alias: JS
Engine Alias: JavaScript
Engine Alias: javascript
Engine Alias: ECMAScript
Engine Alias: ecmascript
Language: ECMAScript (ECMA - 262 Edition 5.1)
java.lang.NullPointerException
at me.soulmachine.JythonExample.main(JythonExample.java:21)
Exception in thread "main" ImportError: Cannot import site module and its dependencies: No module named site
Determine if the following attributes are correct:
* sys.path: ['/home/programmer/src/github/JythonExample/JythonExample/target/Lib', '__classpath__', '__pyclasspath__/']
This attribute might be including the wrong directories, such as from CPython
* sys.prefix: /home/programmer/src/github/JythonExample/JythonExample/target
This attribute is set by the system property python.home, although it can
be often automatically determined by the location of the Jython jar file
You can use the -S option or python.import.site=false to not import the site module
It seems the pyEngine is null.
So I wonder what's the difference between jython-standalone-2.7.0.jar and jython-2.7.0.jar
One problem I've just discovered with the same error is that the maven build 2.7.0 does not include the lib folder. This is probably a build error for the release build. I had to move up the b2 build which does properly include the lib folder in the supplied jar.
Problem maven 2.7.0 jar:
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.0</version>
</dependency>
Working maven 2.7.1b2 that includes the lib folder:
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.1b2</version>
</dependency>
NOTE: If you download the jar directly from the Jython site it does correctly include the lib folder. It's just the maven repository version.
I believe the main difference causing your issue is that the jython-standalone jar provides Lib/ (which contains site.py) while the jython jar does not.
https://github.com/scijava/jython-shaded gives a more in-depth description of the issue, as well as other issues, and provides an alternative jar to get around some issues noted in the description.
I don't have experience with scijava:jython-shaded, but I substituted it into your pom (for my setup I also had to change jdk.version to 1.7 and to JythonExample) and your example runs.
I try to run "gradle test", and get error
My test is
class HelperTest extends ro.gd.Test {
Plugin o;
void setUp() {
o = new Plugin();
}
void testGetIdeaDeps() {
def r = o.ideaDeps
asrHaveVal r
}
}
when i run gradle test, it raise:
junit.framework.AssertionFailedError: Exception in constructor: testGetIdeaDeps (java.lang.NoClassDefFoundError: Could not initialize class groovy.lang.GroovySystem
at org.codehaus.groovy.reflection.ClassInfo.isValidWeakMetaClass(ClassInfo.java:221)
at org.codehaus.groovy.reflection.ClassInfo.getMetaClassForClass(ClassInfo.java:191)
at org.codehaus.groovy.reflection.ClassInfo.getMetaClass(ClassInfo.java:236)
at ro.gd.idea.HelperTest.$getStaticMetaClass(HelperTest.groovy)
at ro.Test.<init>(Test.groovy)
at ro.gd.Test.<init>(Test.groovy)
at ro.gd.idea.HelperTest.<init>(HelperTest.groovy)
...
Here is my full code
I fix this question. the reason is "groovy.lang.GroovyRuntimeException: Conflicting module versions", for detail, following is my build.gradle
compile 'org.codehaus.groovy:groovy-all:+'
compile gradleApi()
I guess gradleApi() will auto "compile localGroovy()", and this groovy version is 2.3.6, but latest version is 2.4.3
I find this error message in one test report
the solution is to specify groovy version like following
compile 'org.codehaus.groovy:groovy-all:2.3.6'
and you check your groovy version with 'gradle dependencies|grep groovy'
This error means that class TestUtil is not on the classpath and the compiler can not find it. I came across such error hundred times before and there was missing Jar or wrong written class names, but now I just don't know what is wrong.
In my buildSrc dir I have custom Task and I made test for it:
package com.example.core.tasks;
import spock.lang.Specification
import org.gradle.api.Project
import org.gradle.util.TestUtil
public class GetInfoTaskTest extends Specification {
def "check files"(){
given:
def project = TestUtil.createRootProject()
when:
GetInfoTask getInfo = project.tasks.create("getInfo", GetInfoTask)
then:
getInfo instanceof GetInfoTask.class
}
}
In the build script in the buildSrc dir:
dependencies {
compile localGroovy()
testCompile 'org.spockframework:spock-core:0.7-groovy-1.8'
testCompile gradleApi()
}
Error:
GetInfoTaskTest.groovy: 4: unable to resolve class org.gradle.util.TestUtil
# line 3, column 1.
import org.gradle.util.TestUtil
^
I checked that this TestUtil is not internal. I still don't know why gradle can not find it.
TestUtil is an internal class. You can't use it in your own code.