英文:
My Java code only works when ran from IDE, not terminal
问题
以下是翻译好的内容:
我可以在集成开发环境(IDE)中正常运行我的Java程序,但当我尝试在命令行中执行这个第一步时:
- 当我执行
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
英文:
I can run my Java program fine in the IDE but when I try this very first step in the command line:
javac Main.java Test.java
then I get a series of errors.
The errors are saying all of my imports do not exist
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
What am i Doing wrong? How can I get my code to find these imports correctly?
EDIT: I have looked at the other answers and they are not working for me. All of my jar files are located here C:\Users\NROLL97\Documents\jars
. Here is an example of what I've tried:
javac -cp "C:\Users\NROLL97\Documents\jars\*.jar:." Main.java
答案1
得分: 1
对于可能遇到这个问题的其他人...
需要类似这样的内容:
java -cp ".\target\classes;lib\*;" org.testng.TestNG ParallelTestXML.xml
.\target\classes
是类文件的路径(而不是 .java 文件),lib\*
是 lib 文件夹中所有 jars
的路径。
现在所有必要的依赖项都会被找到。
英文:
For anyone else who might come across this...
Something like this is needed:
java -cp ".\target\classes;lib\*;" org.testng.TestNG ParallelTestXML.xml
.\target\classes
is the path to the class files (not the .java files) and lib\*
is the path to all of my jars
in the lib
folder.
Now all necessary dependencies will be found
答案2
得分: 0
好的,首先我想推荐的是关于如何编译您的源代码,而不是为每个要与任何其他类一起编译的类名编写代码,只需使用 *
例如:javac *.java(按下回车按钮)
这个命令会一次性编译所有的Java源文件,而不是逐个编写它们的名称。
好的,现在来看看您的问题
您的编译器找不到selenium包,或者说包含了所有与selenium类相关的类的.jar文件
下载包含selenium相关类的.jar文件,将其保存在与您保存main.java和Test.java文件的相同位置
然后尝试编译它
英文:
Okay
First thing I wana recommend you is about how you are compiling your source code, well instead of writing each and every class name which you want to compile with any another class name just use *
Example : javac *.java (hit enter button)
This command will compile all the java source file at a time instead of writting their names individually
Okay lets come to your problem
Your compiler can't find selenium package or say .jar file which contains all the classes which are related to selenium classes
Download the .jar file which contains selenium related class in it and save it in the same location where you have saved your main.java and Test.java file
And then try to compile it
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论