Java 错误:包 com.google.common.base 不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29627656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: package com.google.common.base does not exist
提问by greenity
given the following code in java, when compiling it u have a lot of errors :
在java中给出以下代码,编译时你有很多错误:
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;
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;
I run it via IntelliJ. Does anyone know why it does not work?
我通过 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);
}
}
采纳答案by pinkulani
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>11.0.2</version>
</dependency>
by adding dependency in pom.xml I was able to solve the problem
通过在 pom.xml 中添加依赖项,我能够解决问题
回答by Carlos Bribiescas
Are you using anything as a dependency manager? If you use something like maven, it will take care of getting the actual jars and putting them in your classpath.
你使用任何东西作为依赖管理器吗?如果你使用像 maven 这样的东西,它会负责获取实际的 jars 并将它们放在你的类路径中。
There are many ways to add stuff to you classpath, but basically in one way or another something has to get the jars that contain the classes you want to import and reference them while compiling. Or else your local environment has no way to know what you're importing.
有很多方法可以向您的类路径添加内容,但基本上以一种或另一种方式必须获取包含您要导入的类的 jar 并在编译时引用它们。否则您的本地环境无法知道您正在导入什么。
This is different from stuff you can import without any foreign jars. Packages such as java.util.*;
come with the jdk you use which is why you can import them and compile without any further work.
这与您可以在没有任何外国罐子的情况下导入的东西不同。诸如java.util.*;
您使用的 jdk 附带的包,这就是为什么您可以导入它们并进行编译而无需任何进一步工作。
回答by Metin Dagcilar
Anybody having similar issues, this is a dependency issue.
任何有类似问题的人,这是一个依赖问题。
In IntelliJ:
在 IntelliJ 中:
- Open the
pom.xml
file. - With the editor tab having the focus, choose
Code | Generate on the main menu
, or pressAlt+Insert
, and choose Dependency from the Generate pop-up window.
- 打开
pom.xml
文件。 - 当编辑器选项卡具有焦点时,选择
Code | Generate on the main menu
,或按Alt+Insert
,然后从“生成”弹出窗口中选择“依赖关系”。
That should fix this problem.
那应该可以解决这个问题。
回答by Makvin
Add this dependency to your gradle file
将此依赖项添加到您的 gradle 文件中
compile "com.google.guava:guava:16+"