The code shows compilation with the -cp trigger but not running. Apparently, it cannot find the HashMultimap. Classpath problem?
$ javac -cp google-collect-1.0.jar MultiThing.java
$ java -cp google-collect-1.0.jar MultiThing
Exception in thread "main" java.lang.NoClassDefFoundError: MultiThing
Caused by: java.lang.ClassNotFoundException: MultiThing
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
Could not find the main class: MultiThing. Program will exit.
$ cat MultiThing.java
import java.io.*;
import java.util.*;
import com.google.common.annotations.*;
import com.google.common.collect.*;
public class MultiThing {
public static void main(String[] args) {
Multimap<String, String> wordToFiles = HashMultimap.create();
wordToFiles.put("first", "HELLO");
wordToFiles.put("first", "HALLO");
for (String thing : wordToFiles.get("first")){
System.out.println(thing);
}
}
}
$ ls
google-collect-1.0.jar MultiThing.class com MultiThing.java
Packages in Java are not hierarchically related as far as imports and compilation are concerned – for example, you can’t import
com.google.collections.*by importingcom.*.The packages in the collections library you mention are:
com.google.common.core.*com.google.common.annotations.*com.google.common.collect.*Try importing those packages explicitly. If you’re using an IDE like Eclipse, it can sort out all your import statements for you.
In response to update:
-cp overrides your classpath. You’ll need to include the current directory to keep the class you’ve written on the classpath, so assuming you’re running in the directory with your class, set the classpath as follows
java -cp .:google-collect-1.0.jar MultiThing