错误:包 com.google.common.base 不存在

新手上路,请多包涵

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


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

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

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

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

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

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

Main.java:7: 错误: 包 org.ros.node 不存在 import 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);
  }

}

原文由 greenity 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 6.1k
2 个回答
<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>11.0.2</version>
</dependency>

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

原文由 pinkulani 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

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

原文由 Makvin 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题