2015-04-14 521 views
7

给出的Java下面的代码,编译时u有很多误区:错误:包com.google.common.base不存在


Main.java:1: error: package com.google.common.base does not exist import com.google.common.base.Preconditions; ^

Main.java:2: error: package com.google.common.collect does not exist import com.google.common.collect.Lists; ^

Main.java:3: error: package org.ros.exception does not exist import org.ros.exception.RosRuntimeException; ^

Main.java:4: error: package org.ros.internal.loader does not exist import org.ros.internal.loader.CommandLineLoader; ^

Main.java:5: error: package org.ros.node does not exist import org.ros.node.DefaultNodeMainExecutor; ^

Main.java:6: error: package org.ros.node does not exist import org.ros.node.NodeConfiguration; ^

Main.java:7: error: package org.ros.node does not exist 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); 
    } 

} 
+0

你可能尝试导入这些包下的包,而不是自己的班。例如,尝试为第一个错误添加'import static com.google.com.base.Preconditions。*'。 – chesh

+0

对maven或gradle的广告依赖,然后'gradlew idea'或'maven clean build'并在Intellij中点击File - > Invalidate cahces/Restart –

回答

7
<dependency> 
    <groupId>com.google.guava</groupId> 
    <artifactId>guava</artifactId> 
    <version>11.0.2</version> 
</dependency> 

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

1

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

有很多方法可以将东西添加到classpath中,但基本上可以通过某种方式获得包含要导入的类的jar包,并在编译时引用它们。否则,您的本地环境无法知道您要导入的内容。

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

1

得到任何类似的问题,这是一个依赖问题。

在的IntelliJ:

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

这应该解决这个问题。

1

加入这个依赖于你的gradle这个文件

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