有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

maven Java或JavaC一直说包未找到或不存在

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

以下是我的问题示例,我尝试编译一个连接到我要运行的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("安卓")) {
                        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

基本测试包中的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运行它就没有问题了。我认为这个问题与我对这个项目的依赖性有关。有没有人经历过这样的情况


共 (1) 个答案

  1. # 1 楼答案

    听起来,无论您在命令行中做什么,都没有包含构建它所需的类。在Eclipse中,您可能有对必要的JAR文件的引用

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

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