【问题标题】:Error: Could not find or load main class, but works when .jar is missing from classpath?错误:无法找到或加载主类,但在类路径中缺少 .jar 时有效?
【发布时间】:2018-09-17 00:52:31
【问题描述】:

我正在关注本教程:https://www.youtube.com/watch?v=UlY_6N98WWs&list=PLIBI8eaaUGfSupVFlMBefGodvWQ9ydq51&index=4 并得到了这个错误:

$ java -classpath selenium-server-standalone-3.11.0.jar HelloWorld 
Error: Could not find or load main class HelloWorld

但如果我运行$ java HelloWorld,它就可以正常工作,并说

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

我在 Mac 终端上,我尝试使用 CLASSPATHs 或 . 和空白。我的 $PATH 变量是 /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin 并且似乎没问题。

我尝试查看这些页面的解决方案: 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?

我的课程路径似乎运行良好,但还有其他问题?

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 3.11.0 版下载自: https://www.seleniumhq.org/download/

我在 Mac OS 10.13.3 上运行。

【问题讨论】:

  • 试试这个:java -classpath .:selenium-server-standalone-3.11.0.jar HelloWorld

标签: java selenium terminal javac


【解决方案1】:

显然我需要通过将:. 附加到selenium-server-standalone-3.11.0.jar 来将当前目录附加到类路径。

$ java -classpath selenium-server-standalone-3.11.0.jar:. HelloWorld 

特别感谢 lanoxx 对 https://stackoverflow.com/a/18093929/2423194 的评论帮助了我:

我在尝试与第 3 方一起运行课程时遇到了这个问题 图书馆。我这样调用java:java -cp ../third-party-library.jar com.my.package.MyClass;这不起作用,而是有必要 将本地文件夹也添加到类路径中(用:分隔,比如 这个:java -cp ../third-party-library.jar:. com.my.package.MyClass, 那么它应该可以工作

还要感谢 Sam Orozco 稍后在我打字时对答案发表评论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 2017-07-30
    • 2018-12-16
    相关资源
    最近更新 更多