2015-04-14 522 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+"