Java 或 JavaC 一直说找不到或不存在包
Java or JavaC Keeps Saying Package Not Found or Doesn't Exist
我在尝试 运行 Maven 项目中的 Java 文件时遇到了障碍。我在 运行 a Java Class 的位置设置了它,在使用 运行 exec 到 运行 其余测试之前做了一些设置在行家。我可以在 IDE 工具 (Eclipse) 中 运行 编程,但是当我从 CMD 或 Git Bash 编程 运行 时,我不断收到错误提示ClassDef not found 或 package/import 不存在,(即使它在 IDE 和 Mvn 命令上工作正常以测试 classes。我尝试使用Javac - cp .看看新的设置是否会消除错误,但我得到了类似的错误。我四处寻找了几个不同的答案,他们要么没有解决问题,要么把我的项目处于更糟糕的状态。现在,我不确定问题的确切原因是什么,也不确定解决此问题的最佳解决方案。
这是我的问题的一个例子,我试图编译连接到 Java 文件的 java 文件之一,我想要 运行,这将 运行 专家:
$ 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 class 中,它已经正确导入了 AppiumDriver 和 MobileElement,作为我的 IDE 中正确的导入选项之一。如果我用 mvn 运行 没问题。我认为问题与我对该项目的依赖关系有关。有没有人经历过这样的情况?
听起来您在命令行中所做的任何事情都没有完全包含构建它所需的 类。在 Eclipse 中,您可能会引用必要的 JAR 文件。
我的建议 - 如果您使用 Maven 来实现构建自动化(您应该这样做),那么应该完整地使用它,构建过程将自动为您完成所有这些工作。
在你的情况下,我想只要确保 javac 知道在哪里可以找到 XMLMaker 的 jar 文件,并且可以找到必要的依赖项。
我在尝试 运行 Maven 项目中的 Java 文件时遇到了障碍。我在 运行 a Java Class 的位置设置了它,在使用 运行 exec 到 运行 其余测试之前做了一些设置在行家。我可以在 IDE 工具 (Eclipse) 中 运行 编程,但是当我从 CMD 或 Git Bash 编程 运行 时,我不断收到错误提示ClassDef not found 或 package/import 不存在,(即使它在 IDE 和 Mvn 命令上工作正常以测试 classes。我尝试使用Javac - cp .看看新的设置是否会消除错误,但我得到了类似的错误。我四处寻找了几个不同的答案,他们要么没有解决问题,要么把我的项目处于更糟糕的状态。现在,我不确定问题的确切原因是什么,也不确定解决此问题的最佳解决方案。
这是我的问题的一个例子,我试图编译连接到 Java 文件的 java 文件之一,我想要 运行,这将 运行 专家:
$ 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 class 中,它已经正确导入了 AppiumDriver 和 MobileElement,作为我的 IDE 中正确的导入选项之一。如果我用 mvn 运行 没问题。我认为问题与我对该项目的依赖关系有关。有没有人经历过这样的情况?
听起来您在命令行中所做的任何事情都没有完全包含构建它所需的 类。在 Eclipse 中,您可能会引用必要的 JAR 文件。
我的建议 - 如果您使用 Maven 来实现构建自动化(您应该这样做),那么应该完整地使用它,构建过程将自动为您完成所有这些工作。
在你的情况下,我想只要确保 javac 知道在哪里可以找到 XMLMaker 的 jar 文件,并且可以找到必要的依赖项。