【问题标题】:NoClassDefFoundError in JavaFx with Command line Parameters with Selenium Code?JavaFx中的NoClassDefFoundError与Selenium代码的命令行参数?
【发布时间】:2013-11-29 17:34:52
【问题描述】:

我正在尝试做一个可以接收命令行参数的 JavaFX 应用程序。我可以接收参数。但是当我实例化包含 Selenium(测试/WebBrowser 自动化库)成员的“A”类(来自 Main.java)时,我遇到了问题。

// Main.java
package myPackage;

import java.util.Map;
import javafx.application.Application;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        if (getParameters().getRaw().isEmpty() == false) {
            System.out.println("The application was called by Command Line.");
            Map<String, String> params = getParameters().getNamed();
            System.out.println(params.get("a"));            
            A myA = new A();
        } else {
            System.out.println("The application was called executing the jar file.");
            A myA = new A();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }

}

// A.java
package myPackage;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.chrome.ChromeDriver;

public class A {

    ChromeDriver driver;

    A() {
        System.out.println("New A Instance.");
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver_win32\\chromedriver.exe");
        this.driver = new ChromeDriver();
    }

    public boolean isElementPresent(By by) {
        try {
            this.driver.findElement(by);
            return true;
        } catch (NoSuchElementException ex) {
            return false;
        }
    }
}

但是当我尝试执行它时: java -cp MyProject.jar myPackage.Main --a="value1"

The application was called by Command Line.
value1
Exception in Application start method
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:483)
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)
        at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:483)
        at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894)
        at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
        at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
        at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/NoSuchElementException
        at myPackage.Main.start(Main.java:16)
        at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837)
        at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:331)
        at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:297)
        at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:294)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
        at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
        ... 1 more
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.NoSuchElementException
        at java.net.URLClassLoader$1.run(URLClassLoader.java:359)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:348)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:347)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 12 more
Exception running application myPackage.Main

如您所见,应用程序获取参数并打印它(上面此输出中的第一行和第二行)...

我读到了NoClassDefFoundError: org/openqa/selenium/NoSuchElementException 这一行,但是添加了 selenium 库 (2.37.0),并且还添加了 libs 依赖项。在 Netbeans 中的 Libraries &gt; Compile 选项卡中

如果我使用 Netbeans Project properties &gt; run &gt; parameters 运行应用程序并插入一个参数,一切都很好。当我通过命令行执行相同操作时,问题就来了...

我已经尝试了很多东西......但我无法理解,为什么?

【问题讨论】:

  • 您可能需要在执行应用程序时将库添加到类路径中。您在 Netbeans 中执行的操作仅在从 Netbeans 启动时生效。

标签: java selenium javafx-2 javafx javafx-8


【解决方案1】:

@KnutArneVedaa 感谢您的上述评论。我是 PHP 程序员,对 Java 几乎一无所知。但是您的 Insight 澄清了 CLASSPATH 就像 PHP 中的 PATH 一样,只是一个环境变量,它帮助我解决了我的问题。

我只是明确地将类路径目录定义为我的当前目录(项目目录),因为 '.'是默认值,是Optional,然后我用-jar标志来指定jar文件lol...

java -cp '.' -jar MyProject.jar myPackage.Main --a="value1"

有效!传递参数和 ChromeDriver 实例化。

再次感谢您;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-17
    • 2012-02-25
    • 2013-03-05
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 2014-01-01
    • 2020-04-11
    相关资源
    最近更新 更多