【问题标题】:My Java code only works when ran from IDE, not terminal我的 Java 代码仅在从 IDE 而非终端运行时才有效
【发布时间】:2020-08-14 17:20:10
【问题描述】:

我可以在 IDE 中正常运行我的 Java 程序,但是当我在命令行中尝试这第一步时:

  1. javac Main.java Test.java 然后我收到一系列错误。

错误是说我的所有导入都不存在

Main.java:3: error: package org.openqa.selenium does not exist
import org.openqa.selenium.By;
                          ^
Main.java:4: error: package org.openqa.selenium does not exist
import org.openqa.selenium.Platform;
                          ^
Main.java:5: error: package org.openqa.selenium does not exist
import org.openqa.selenium.WebDriver;
                          ^
Main.java:6: error: package org.openqa.selenium does not exist
import org.openqa.selenium.WebElement;
                          ^
Main.java:7: error: package org.openqa.selenium.remote does not exist
import org.openqa.selenium.remote.DesiredCapabilities;
                                 ^
Main.java:8: error: package org.openqa.selenium.remote does not exist
import org.openqa.selenium.remote.RemoteWebDriver;
                                 ^
Main.java:16: error: cannot find symbol
        DesiredCapabilities caps = new DesiredCapabilities();
        ^
  symbol:   class DesiredCapabilities
  location: class Main
Main.java:16: error: cannot find symbol
        DesiredCapabilities caps = new DesiredCapabilities();
                                       ^
  symbol:   class DesiredCapabilities
  location: class Main
Main.java:25: error: cannot find symbol
        WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
        ^
  symbol:   class WebDriver
  location: class Main
Main.java:25: error: cannot find symbol
        WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
                               ^
  symbol:   class RemoteWebDriver
  location: class Main
Main.java:27: error: cannot find symbol
        WebElement element = driver.findElement(By.name("q"));
        ^
  symbol:   class WebElement
  location: class Main
Main.java:27: error: cannot find symbol
        WebElement element = driver.findElement(By.name("q"));
                                                ^
  symbol:   variable By
  location: class Main
12 errors

我做错了什么?如何让我的代码正确找到这些导入?

编辑:我查看了其他答案,但它们对我不起作用。我所有的 jar 文件都位于这里C:\Users\NROLL97\Documents\jars。这是我尝试过的一个示例:

javac -cp "C:\Users\NROLL97\Documents\jars\*.jar:." Main.java

【问题讨论】:

标签: java command-line javac


【解决方案1】:

对于任何可能遇到此问题的人... 需要这样的东西: java -cp ".\target\classes;lib\*;" org.testng.TestNG ParallelTestXML.xml

.\target\classes 是类文件(不是 .java 文件)的路径,lib\*lib 文件夹中我所有 jars 的路径。

现在将找到所有必要的依赖项

【讨论】:

    【解决方案2】:

    好的 我想向您推荐的第一件事是关于如何编译源代码,而不是编写要与任何其他类名一起编译的每个类名,只需使用 * 示例:javac *.java(按回车键)

    此命令将一次编译所有 java 源文件,而不是单独编写它们的名称

    好的,让我们来解决您的问题 您的编译器找不到 selenium 包或说包含与 selenium 类相关的所有类的 .jar 文件

    下载包含 selenium 相关类的 .jar 文件,并将其保存在您保存 main.java 和 Test.java 文件的同一位置

    然后试着编译一下

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 1970-01-01
      • 2013-03-22
      相关资源
      最近更新 更多