Error: com.google.common.base package does not exist

given the following code in java, when compiling it you have a lot of errors:


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.javahaps: error: package org.ros.exception does not exist import org.ros.exception.RosRuntimeException; ^

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

Main.javacla: error: org.ros.node package 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: org.ros.node package does not exist import org.ros.node.NodeMainExecutor;

I run it through IntelliJ. Does anyone know why this is not working?

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); } } 
+7
java intellij-idea
source share
4 answers
 <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>11.0.2</version> </dependency> 

adding a dependency to pom.xml, I was able to solve the problem

+7
source share

Do you use 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.

There are many ways to add material to your class path, but basically one way or another you need to get jars containing the classes you want to import and reference them at compile time. Or your local environment is not able to find out what you are importing.

This is different from material that you can import without any foreign cans. Packages such as java.util.*; are shipped with the jdk used, so you can import and compile them without further work.

+1
source share

Anyone who has similar problems is a dependency problem.

In IntelliJ:

  • Open the pom.xml file.
  • On the editor tab with focus, select Code | Generate on the main menu Code | Generate on the main menu or press Alt+Insert and select Dependency in the Create pop-up window.

This should solve this problem.

+1
source share

Add this dependency to the gradle file

  compile "com.google.guava:guava:16+" 
+1
source share

All Articles