尝试在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运行它也没问题。我认为问题与该项目的依赖关系有关。有人遇到过这样的情况吗?
答案 0 :(得分:1)
听起来,您在命令行中所做的任何事情都没有完全包括构建它所需的类。在Eclipse中,您可能会引用必要的JAR文件。
我的建议-如果您正在使用Maven实现构建自动化(应该这样做),则应完全使用它,其中构建过程将自动为您完成所有这些工作。
在您的情况下,我想只是确保javac知道在哪里可以找到XMLMaker的jar文件,并且可以找到必要的依赖项。