【问题标题】:error: package com.google.common.base does not exist错误:包 com.google.common.base 不存在
【发布时间】:2015-06-20 01:57:33
【问题描述】:

在java中给出以下代码,编译时你有很多错误:


Main.java:1:错误:com.google.common.base 包不存在 导入 com.google.common.base.Preconditions; ^

Main.java:2:错误:com.google.common.collect 包不存在 导入 com.google.common.collect.Lists; ^

Main.java:3: 错误:包 org.ros.exception 不存在 导入 org.ros.exception.RosRuntimeException; ^

Main.java:4: 错误:包 org.ros.internal.loader 不存在 导入 org.ros.internal.loader.CommandLineLoader; ^

Main.java:5: 错误:包 org.ros.node 不存在 导入 org.ros.node.DefaultNodeMainExecutor; ^

Main.java:6: 错误:包 org.ros.node 不存在 导入 org.ros.node.NodeConfiguration; ^

Main.java:7: 错误:包 org.ros.node 不存在 导入 org.ros.node.NodeMainExecutor;

我通过 IntelliJ 运行它。有谁知道为什么它不起作用?

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.ros.exception.RosRuntimeException;
import org.ros.internal.loader.CommandLineLoader;
import org.ros.node.DefaultNodeMainExecutor;
import org.ros.node.NodeConfiguration;
import org.ros.node.NodeMainExecutor;

// This class will run a publisher and subscriber, and relay data between them.

public class Main {

static private Talker pubNodeMain;

static private Listener subNodeMain;

public static void main(String[] argv) throws Exception {
    // Set up the executor for both of the nodes
    NodeMainExecutor nodeMainExecutor = DefaultNodeMainExecutor.newDefault();

    // Load the publisher
    String[] pubArgv = {"Talker"};
    CommandLineLoader pubLoader = new CommandLineLoader(Lists.newArrayList(pubArgv));
    String nodeClassName = pubLoader.getNodeClassName();
    System.out.println("Loading node class: " + pubLoader.getNodeClassName());
    NodeConfiguration pubNodeConfiguration = pubLoader.build();

    try {
        pubNodeMain = (Talker) pubLoader.loadClass(nodeClassName);
    } catch (ClassNotFoundException e) {
        throw new RosRuntimeException("Unable to locate node: " + nodeClassName, e);
    } catch (InstantiationException e) {
        throw new RosRuntimeException("Unable to instantiate node: " + nodeClassName, e);
    } catch (IllegalAccessException e) {
        throw new RosRuntimeException("Unable to instantiate node: " + nodeClassName, e);
    }

    Preconditions.checkState(pubNodeMain != null);
    nodeMainExecutor.execute(pubNodeMain, pubNodeConfiguration);

    // Load the subscriber
    String[] subArgv = {"Listener"};
    CommandLineLoader subLoader = new CommandLineLoader(Lists.newArrayList(subArgv));
    nodeClassName = subLoader.getNodeClassName();
    System.out.println("Loading node class: " + subLoader.getNodeClassName());
    NodeConfiguration subNodeConfiguration = subLoader.build();

    try {
        subNodeMain = (Listener) subLoader.loadClass(nodeClassName);
    } catch (ClassNotFoundException e) {
        throw new RosRuntimeException("Unable to locate node: " + nodeClassName, e);
    } catch (InstantiationException e) {
        throw new RosRuntimeException("Unable to instantiate node: " + nodeClassName, e);
    } catch (IllegalAccessException e) {
        throw new RosRuntimeException("Unable to instantiate node: " + nodeClassName, e);
    }

    Preconditions.checkState(subNodeMain != null);
    nodeMainExecutor.execute(subNodeMain, subNodeConfiguration);
  }

}

【问题讨论】:

  • 您可能尝试导入包本身而不是这些包下的类。例如,尝试为第一个错误添加import static com.google.com.base.Preconditions.*
  • 广告依赖于 maven 或 gradle,然后 gradlew ideamaven clean build 并在 Intellij 中单击 File -> Invalidate cahces/ Restart

标签: java intellij-idea


【解决方案1】:

您是否使用任何东西作为依赖项管理器?如果您使用 maven 之类的东西,它会负责获取实际的 jar 并将它们放入您的类路径中。

有很多方法可以向类路径添加内容,但基本上必须以一种或另一种方式获取包含要导入的类的 jar,并在编译时引用它们。否则你的本地环境无法知道你正在导入什么。

这与您可以在没有任何外国 jar 的情况下导入的东西不同。 java.util.*; 等软件包随您使用的 jdk 一起提供,这就是为什么您可以导入它们并编译而无需任何进一步的工作。

【讨论】:

    【解决方案2】:
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>11.0.2</version>
    </dependency>
    

    通过在 pom.xml 中添加依赖,我能够解决问题

    【讨论】:

      【解决方案3】:

      任何有类似问题的人,这是一个依赖问题。

      在 IntelliJ 中:

      1. 打开pom.xml 文件。
      2. 在获得焦点的编辑器选项卡上,选择Code | Generate on the main menu,或按Alt+Insert,然后从“生成”弹出窗口中选择“依赖关系”。

      这应该可以解决这个问题。

      【讨论】:

        【解决方案4】:

        将此依赖项添加到您的 Gradle 文件中

        compile "com.google.guava:guava:16+"
        

        【讨论】:

        • 帮我解决了这个问题...包 com.google.common.base 不存在
        【解决方案5】:

        曾经创建为本机 Intellij 模块而不是 Maven 模块的模块遇到此问题。

        我从项目中删除了模块并重新导入为 Maven 模块,因此它根据 pom.xml 在内部解析了所有库。

        【讨论】:

          猜你喜欢
          • 2011-10-03
          • 1970-01-01
          • 2021-03-04
          • 2014-10-02
          • 2016-06-01
          • 2018-06-21
          • 2020-03-13
          • 2018-07-31
          相关资源
          最近更新 更多