【问题标题】:Java or JavaC Keeps Saying Package Not Found or Doesn't ExistJava 或 JavaC 一直说找不到或不存在包
【发布时间】:2018-11-08 08:53:13
【问题描述】:

尝试在 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 运行它也没问题。我认为问题与我对该项目的依赖关系有关。有没有人经历过这样的案例?

【问题讨论】:

  • 你能用 XMLMaker.java 文件编辑你的问题吗?或者你也可以检查你在这个类中指定的包名可能是这个问题的原因。
  • @GauravRai1512 当然,我编辑问题以包含 XMLMaker 的 Java 文件,它在包中:AppiumDriverSetUp_Lib 和驱动程序测试,Whixh 在包中:BaseTest。我将添加未找到 bash/cmd 状态类的导入。

标签: java maven import jar javac


【解决方案1】:

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

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

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

【讨论】:

  • 让 Maven 完成所有工作会更容易,但问题是我希望在 XMLMaker 中调用的 drivers.xml 根据连接的移动设备是动态的。当我试图在运行测试套件(@BeforeSuite)之前将其设置为运行时,驱动程序 xml 中所做的任何更改以便在同一运行时运行测试时,会话结束后没有得到更新。在做了一些研究之后,Maven 对drivers.xml 进行了任何更改的原始值进行了一种静态引用。这种方法,它允许在我最后关闭 PrintWriter 时更新 drivers.xml。
  • 找到了一个解决方案,使用 Maven 运行我的 Java 文件 DriverTest。我可以运行: mvn exec:java -Dexec.mainClass="BaseTest.DriverTest" 它运行 DriverTest,然后运行 ​​Runtime exec: mvn test after setup.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-20
  • 1970-01-01
  • 2014-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多