views:

166

answers:

3

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

API for MultiMap.

+1  A: 

You typically get the 3rd party package as a jar file (java archive), and then add it to your classpath during compilation and while executing the Java process.

The syntax for doing this is slightly different in Unix and in Windows. If you work with an IDE like Eclipse, there are different ways to add a jar to a build.

If you're using Google collections, you should have a jar in the zip file you downloaded. Something like google-collect*.jar

Update: Looks like OP revised the question

Uri
+1  A: 

In addition to what was said about adding the JAR to your classpath: I haven't used Google Collections, but I highly doubt they put their classes in a package called com.

You should be aware that for nested packages, import level1.* will not import any of the classes in package level1.level2.

So for your example, you will need to change import com.* to import com.google-collections.whateverpackageyouneed.*. Modify as per the Google Collections API.

danben
+6  A: 

Packages in Java are not hierarchically related as far as imports and compilation are concerned - for example, you can't importcom.google.collections.* by importing com.*.

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

Brabster