在java中给出以下代码,在编译时你有很多错误:
Main.java:1:错误:包com.google.common.base不存在 import com.google.common.base.Preconditions; ^
Main.java:2:错误:包com.google.common.collect不存在 导入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);
}
}
答案 0 :(得分:11)
<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
答案 1 :(得分:6)
将此依赖项添加到您的gradle文件
compile "com.google.guava:guava:16+"
答案 2 :(得分:1)
您是否正在使用任何依赖管理器?如果你使用像maven这样的东西,它将负责获取实际的罐子并将它们放在你的类路径中。
有许多方法可以向classpath添加内容,但基本上以某种方式必须获取包含要导入的类的jar并在编译时引用它们。否则您的本地环境无法知道您要导入的内容。
这与您可以在没有任何外国罐子的情况下导入的东西不同。像java.util.*;
这样的软件包附带了你使用的jdk,这就是你可以导入它们并在没有任何进一步工作的情况下编译的原因。
答案 3 :(得分:1)
任何有类似问题的人,都是依赖问题。
在IntelliJ中:
pom.xml
文件。Code | Generate on the main menu
,或按Alt+Insert
,然后从生成弹出窗口中选择依赖关系。这应该解决这个问题。