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?

7
  • 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? Feb 15, 2011 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 Mar 30, 2011 at 19:18
  • 2
    Reported as a bug: code.google.com/p/guava-libraries/issues/detail?id=586 Mar 31, 2011 at 17:15
  • 1
    this isn't fixed in r09 as of this posting, you still have to add teh jsr305 dependency to Maven manually!
    – user177800
    Sep 20, 2011 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. Nov 16, 2011 at 23:43

3 Answers 3

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

2
  • 7
    +1: It's still an annoying question: What's the future-proof way of integrating JSR-305 content into today's projects, given the lack of apparent progress?
    – andersoj
    Nov 26, 2010 at 14:34
  • See the other answer for a partial solution. Nov 16, 2011 at 23:57
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.

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.

2
  • thanks, that soved it for me... now lets check if it was worth the truble^^
    – Stefan
    Mar 26, 2012 at 11:43
  • I just had to a javax.annotation.Annotation.gwt.xml file with: <module rename-to='Annotation'> <source path=''/> </module> and of course inherit this in the my projects .gwt.xml file. Got this trick from here
    – Roger
    Jul 17, 2014 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.