I was following this tutorial: https://www.youtube.com/watch?v=UlY_6N98WWs&list=PLIBI8eaaUGfSupVFlMBefGodvWQ9ydq51&index=4 And got this error:
$ java -classpath selenium-server-standalone-3.11.0.jar HelloWorld
Error: Could not find or load main class HelloWorld
But if I run $ java HelloWorld
, it works just fine, and says
Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/firefox/FirefoxDriver at
HelloWorld.main(HelloWorld.java:13) Caused by:
java.lang.ClassNotFoundException:
org.openqa.selenium.firefox.FirefoxDriver at
java.net.URLClassLoader.findClass(URLClassLoader.java:381) at
java.lang.ClassLoader.loadClass(ClassLoader.java:424) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.j
I’m on Mac terminal, and I tried with CLASSPATH
s of .
and blank. My $PATH
variable is /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
and seems to be fine.
I tried looking at these pages for the solution: https://docs.oracle.com/javase/tutorial/essential/environment/paths.html https://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html What does "Could not find or load main class" mean?
And it seems like my class path is working fine, but something else is glitchy?
HelloWorld.java
/**
* Import FirefoxDriver and By from selenium jar
*/
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class HelloWorld{
public static void main(String args[]) throws Exception{
// Create a Firefox browser instance
FirefoxDriver driver = new FirefoxDriver();
// Navigate to google home page
driver.get("https://www.google.co.in/");
// Type hello world in the search field
driver.findElement(By.name("q")).sendKeys("Hello World");
// Wait for 10 seconds
Thread.sleep(10*1000);
// Close the browser instance
driver.quit();
}
}
Selenium Standalone Server version 3.11.0 downloaded from: https://www.seleniumhq.org/download/
I'm running on Mac OS 10.13.3.
Apparently I needed to append the current directory to the class path by appending :.
to selenium-server-standalone-3.11.0.jar
.
$ java -classpath selenium-server-standalone-3.11.0.jar:. HelloWorld
Special thanks to lanoxx's comment on https://stackoverflow.com/a/18093929/2423194 that helped me:
I had this problem when I was trying to run a Class with a 3rd party library. I invoked java like this: java -cp ../third-party-library.jar com.my.package.MyClass; this does not work, instead it is necessary to add the local folder to the class path as well (separated by :, like this: java -cp ../third-party-library.jar:. com.my.package.MyClass, then it should work
and also thanks to Sam Orozco for commenting the answer later while I was typing this up.