gpt4 book ai didi

Java 或 JavaC 不断提示找不到包或包不存在

转载 作者:行者123 更新时间:2023-12-02 10:37:01 29 4
gpt4 key购买 nike

当我尝试在 Maven 项目中运行 Java 文件时遇到了障碍。我已经在运行 Java 类的地方进行了设置,该类在使用 run exec 运行 Maven 中的其余测试之前进行了一些设置。我可以在 IDE 工具(Eclipse)中正常运行程序,但是当我从 CMD 或 Git Bash 运行程序时,我不断收到错误,指出找不到 ClassDef 或包/导入不存在(即使它在 IDE 中运行良好)并在 Mvn 命令上测试类。我尝试使用 Javac - cp 。看看新的设置是否会消除错误,但我遇到了类似的错误。我四处寻找了几个不同的答案,他们要么不这样做无法解决问题或使我的项目处于更糟糕的状态。现在,我不确定问题的确切原因是什么,也不确定解决此问题的最佳解决方案。

这是我的问题的一个示例,我尝试编译连接到我想要运行的 Java 文件的 java 文件之一,该文件将运行 Maven:

 $ javac -d build/classes -sourcepath src -cp target/classes src/AppiumDriverSetUp_Lib/XMLMaker.java
src\AppiumDriverSetUp_Lib\XMLMaker.java:28: error: package io.appium.java_client does not exist
import io.appium.java_client.AppiumDriver;
^
src\AppiumDriverSetUp_Lib\XMLMaker.java:29: error: package io.appium.java_client does not exist
import io.appium.java_client.MobileElement;
^
src\AppiumDriverSetUp_Lib\XMLMaker.java:49: error: cannot find symbol
public void setupDriverXMLFile(List <AppiumDriver<MobileElement>> driverList) {
^
symbol: class AppiumDriver
location: class XMLMaker
src\AppiumDriverSetUp_Lib\XMLMaker.java:49: error: cannot find symbol
public void setupDriverXMLFile(List <AppiumDriver<MobileElement>> driverList) {
^
symbol: class MobileElement
location: class XMLMaker
src\AppiumDriverSetUp_Lib\XMLMaker.java:67: error: cannot find symbol
for(AppiumDriver<MobileElement> driver: driverList) {
^
symbol: class AppiumDriver
location: class XMLMaker
src\AppiumDriverSetUp_Lib\XMLMaker.java:67: error: cannot find symbol
for(AppiumDriver<MobileElement> driver: driverList) {
^
symbol: class MobileElement
location: class XMLMaker
6 errors

AppiumDriverSetUp_Lib 包中的 XMLMaker 文件:

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;

public class XMLMaker {



public DocumentBuilderFactory docDriverSetup;

public DocumentBuilder driverSetup;

public int connectedDevices = 0;

public Document doc;


TransformerFactory transformerFactory = TransformerFactory.newInstance();

Transformer transformer;


public void setupDriverXMLFile(List <AppiumDriver<MobileElement>> driverList) {

System.out.println("List size: "+ driverList.size());
try {

docDriverSetup = DocumentBuilderFactory.newInstance();

driverSetup = docDriverSetup.newDocumentBuilder();

doc = driverSetup.newDocument();

transformer = transformerFactory.newTransformer();

transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://testng.org/testng-1.0.dtd");

Element suiteElement = doc.createElement("suite");
suiteElement.setAttribute("name", "All-tests");

for(AppiumDriver<MobileElement> driver: driverList) {

Element rootElement = doc.createElement("test");
suiteElement.appendChild(rootElement);
rootElement.setAttribute("name", (String) driver.getCapabilities().getCapability("deviceId"));

Element deviceNameEle = doc.createElement("parameter");
deviceNameEle.setAttribute("name", "deviceName");
deviceNameEle.setAttribute("value", (String) driver.getCapabilities().getCapability("deviceId"));
rootElement.appendChild(deviceNameEle);

Element platformEle = doc.createElement("parameter");
platformEle.setAttribute("name", "platform");
platformEle.setAttribute("value", driver.getPlatformName()+"");
rootElement.appendChild(platformEle);

Element udidEle = doc.createElement("parameter");
udidEle.setAttribute("name", "udid");
udidEle.setAttribute("value", (String)driver.getCapabilities().getCapability("udid"));
rootElement.appendChild(udidEle);

Element urlPort = doc.createElement("parameter");
urlPort.setAttribute("name", "URL");
urlPort.setAttribute("value", (String)driver.getCapabilities().getCapability("appiumURL"));
rootElement.appendChild(urlPort);

Element devicePort = doc.createElement("parameter");

if((driver.getPlatformName()+"").
toLowerCase().contains("android")) {
devicePort.setAttribute("name", "port");
devicePort.setAttribute("value", driver.getCapabilities().getCapability("systemPort")+"");

}
if ((driver.getPlatformName()+"").
toLowerCase().contains("ios")) {
devicePort.setAttribute("name", "port");
devicePort.setAttribute("value", (String)driver.getCapabilities().getCapability("wdaLocalPort"));
}

rootElement.appendChild(devicePort);

Element packages = doc.createElement("packages");
rootElement.appendChild(packages);
Element packageName = doc.createElement("package");
packageName.setAttribute("name", "BaseTest");
packages.appendChild(packageName);


connectedDevices++;
}
suiteElement.setAttribute("parallel", "tests");
suiteElement.setAttribute("thread-count", connectedDevices+"");
doc.appendChild(suiteElement);

} catch(ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}

}


public void createDriverFile() throws TransformerConfigurationException, InterruptedException, IOException {

DOMSource source = new DOMSource(doc);

FileOutputStream streamNewFile = new FileOutputStream("./drivers.xml");
PrintWriter pw = new PrintWriter(streamNewFile);
StreamResult result = new StreamResult(pw);


try {

transformer.transform(source, result);
result.getWriter().close();

System.out.println("File Updated");



} catch (TransformerException e) {
e.printStackTrace();
System.out.println("Error updating the file");
} catch (IOException e) {
e.printStackTrace();
System.out.println("Unable to close output stream");
}

}

}

这是我尝试编译 Java 文件以开始测试时出现的问题:

$ javac -d build/classes -sourcepath src -cp target/classes src/BaseTest/DriverTest.java
src\BaseTest\DriverTest.java:29: error: cannot access AppiumDriver
makeFile.setupDriverXMLFile(createDrivers.getActiveList());
^
class file for io.appium.java_client.AppiumDriver not found
1 error

BaseTest 包中的 DriverTest:

import AppiumDriverSetUp_Lib.AppiumDriverSetup;
import AppiumDriverSetUp_Lib.XMLMaker;

public class DriverTest {

public AppiumDriverSetup createDrivers = new AppiumDriverSetup();

public XMLMaker makeFile = new XMLMaker();

public static void main(String [] args) throws TransformerConfigurationException, IOException, InterruptedException {
System.out.println("Setting up drivers:");

DriverTest startTest = new DriverTest();
startTest.driverFileSetup();
startTest.runSuite();
}

public void driverFileSetup() throws IOException, TransformerConfigurationException, InterruptedException {
createDrivers.makeList();
makeFile.setupDriverXMLFile(createDrivers.getActiveList());
makeFile.createDriverFile();
}

public void runSuite() throws IOException, InterruptedException {
Runtime rt = Runtime.getRuntime();
String cmd = "cmd /c mvn test -Dplat="+System.getProperty("plat");
Process p = rt.exec(cmd);
InputStream input = p.getInputStream();
testFeed(input, System.out);
p.waitFor();
}

public void testFeed(InputStream in, OutputStream out) throws IOException {
while (true) {
int c = in.read();
if (c == -1) {
break;
}
out.write((char)c);
}
}

}

在我的 XMLMaker 类中,它已经具有 AppiumDriver 和 MobileElement 的正确导入,作为我的 IDE 中的正确导入选项之一。如果我用 mvn 运行它就没有问题。我认为这个问题与我对该项目的依赖关系有关。有人经历过这样的案例吗?

最佳答案

听起来您在命令行中所做的任何事情都不完全包含构建它所需的类。在 Eclipse 中,您可能拥有对必要 JAR 文件的引用。

我的建议 - 如果您使用 Maven 来实现构建自动化(您应该这样做),则应该完整地使用它,构建过程将自动为您完成所有这些工作。

在你的情况下,我想只要确保 javac 知道在哪里可以找到 XMLMaker 的 jar 文件,并且可以找到必要的依赖项。

关于Java 或 JavaC 不断提示找不到包或包不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53204279/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com