23

I'm trying to use Guava in a GWT project without success (a HashMultimap, to be precise). I get a never-ending list of stacktraces for classes:

  • com.google.common.collect.ComparisonChain
  • com.google.common.collect.ForwardingSortedSetMultimap
  • com.google.common.collect.Ordering
  • ...

Each stack trace is along the lines of:

  • line xx: the import javax.annotation cannot be resolved
  • line xx: Nullable cannot be resolved to a type
  • line xx: Nullable cannot be resolved to a type
  • line xx: Nullable cannot be resolved to a type
  • ...

Looking at the code, each file that throws an error includes:

import javax.annotation.Nullable;

and, looking at the guava-src-r07.jar, each mentioned classes uses a @Nullable annotation.

I'm using JDK6 and looking at the JDK6 javadoc and...well, I can't find any such annotation. Can I get these libraries to work with a GWT project and JDK6?

P.S. - What version of Java are you using over there?

Evan Cowden
  • 399
  • 1
  • 3
  • 9
  • 3
    You are absolutely positively 100% NOT supposed to need to go off hunting for a jsr305 jar yourself! guava-gwt.jar is supposed to just work for you out of the box, so please provide details about what exactly you're trying and what is happening; "I get a list of stack traces" -- from what exactly? – Kevin Bourrillion Feb 15 '11 at 20:07
  • @Kevin, When I do the same thing, and try to compile, I get: Compiling module com.restphone.LingoGwt Validating newly compiled units [ERROR] Errors in 'jar:file:/Users/james/lib/guava-r08/guava-r08-gwt.jar!/com/google/common/base/Equivalence.java' [ERROR] Line 20: The import javax.annotation cannot be resolved [ERROR] Line 51: Nullable cannot be resolved to a type [ERROR] Line 51: Nullable cannot be resolved to a type [ERROR] Line 60: Nullable cannot be resolved to a type – James Moore Mar 30 '11 at 19:18
  • 2
    Reported as a bug: http://code.google.com/p/guava-libraries/issues/detail?id=586 – James Moore Mar 31 '11 at 17:15
  • And now looks like it's fixed in guava 09 rc3. http://code.google.com/p/guava-libraries/downloads/list – James Moore Apr 06 '11 at 17:25
  • 1
    this isn't fixed in r09 as of this posting, you still have to add teh jsr305 dependency to Maven manually! –  Sep 20 '11 at 22:57
  • 1
    I had this problem when compiling Caliper, based on Guava r08. I switched to r09 and it didn't work, then switched to 10.0.1 and it automatically downloaded also the JSR305 dependency. – Blaisorblade Nov 16 '11 at 23:43
  • I had something similar: Under sbt on jdk 1.7.0_{21,45} with guava as a dependency among others, compilation failed because javax.annotation.Nonnull couldn't be resolved. This happened when adding or removing seemingly arbitrary dependencies (in particular https://code.google.com/p/compilation-toolbox/ fixed it). When I found this question, I just added http://mvnrepository.com/artifact/com.google.code.findbugs/jsr305 as dependency and voilà - issue resolved. Annoying. – Sebastian Graf Jan 11 '14 at 23:10

3 Answers3

14

Hum... I think it's the jsr305 you're looking for. Take a look at

http://www.findjar.com/jar/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar.html

It must be better here: http://code.google.com/p/guava-libraries/source/browse/#svn/trunk/lib where I see the @Nullable annotation

sly7_7
  • 11,961
  • 3
  • 40
  • 54
12

As written above, the problem appears to be solved when using guava 10.0.1, which has an additional dependency on the JSR305 library.

Alternatively, the ID of the missing library to add to Maven is com.google.code.findbugs:jsr305:1.3.9, so the build configuration should be fixed by adding the following dependency to pom.xml in the appropriate place (warning - I didn't test this):

<dependency>
    <groupId>com.google.code.findbugs</groupId>
    <artifactId>jsr305</artifactId>
    <version>2.0.1</version>
    <scope>provided</scope>
</dependency>

Update: User @ips suggested adding <scope>provided</scope> since the jsr305 jar isn't needed at runtime, and updating to version 2.0.1. I know that the first change makes sense, and I guess that the version update also does. In my experience, using <scope>provided</scope> created problems for Scala, but that's a separate issue.

Blaisorblade
  • 6,438
  • 1
  • 43
  • 76
5

You need to obtain the JSR 305 JAR, but in addition, you need to include the @Nullable annotation source code as food for the GWT compiler.

Assuming your project is under com/example/myproject/ Create a file: com/example/myproject/annotation/javax/annotation/Nullable.java With the following content:

package com.example.myproject.annotation.javax.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import javax.annotation.Nonnull;
import javax.annotation.meta.TypeQualifierNickname;
import javax.annotation.meta.When;

@Documented
@TypeQualifierNickname
@Nonnull(when = When.UNKNOWN)
@Retention(RetentionPolicy.RUNTIME)
public @interface Nullable {

}

Add the line to MyProject.gwt.xml:

<super-source path="annotation"/>

And you're good to go.

Tomer
  • 610
  • 1
  • 7
  • 12
  • thanks, that soved it for me... now lets check if it was worth the truble^^ – Stefan Mar 26 '12 at 11:43
  • I just had to a javax.annotation.Annotation.gwt.xml file with: ` ` and of course inherit this in the my projects .gwt.xml file. Got this trick from [here](https://code.google.com/p/guava-libraries/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Package%20Summary&groupby=&sort=&id=776) – Roger Jul 17 '14 at 19:53