Using guava with j2objc

738 Views Asked by At

I'm trying to use guava with j2objc but I'm getting the error:

j2objc TestJava.java 
translating TestJava.java
error: TestJava.java:1: The import com.google.common cannot be resolved
error: TestJava.java:10: Lists cannot be resolved
Translated 0 files: 2 errors, 0 warnings

while running:

j2objc Test.java

where Test.java contains:

import com.google.common.collect.Lists;
import java.util.ArrayList;

public class Test {
    public static void TestMethod() {
        ArrayList<String> objects = Lists.newArrayList();
        objects.add(0, "Hello world");
        System.out.println(objects);            
    }
}

I've downloaded the lastest release 0.9.5 and added it to .profile:

export PATH=$HOME/bin/j2objc-0.9.5:$PATH

What else do I need to do in order to use guava?

Thanks!

1

There are 1 best solutions below

0
On BEST ANSWER

The j2objc translator uses a Java compiler as its front-end, so the same classpath and sourcepath needs to be used when translating as when compiling to Java classes. Here you'll get a similar error if you ran "javac Test.java", because the guava jar needs to be included in the classpath. The j2objc distribution includes lib/guava-jdk5.jar, so run "javac -classpath /lib/guava-jdk5.jar Test.java", and fix any problems with the directory for the guava jar if necessary. Once javac can compile it, substitute "j2objc" for "javac" using the same arguments, and it should translate fine.

You don't always have to use javac first, but whenever there are translation errors reported, it's a quick test to see whether the issue is related to a missing source or class path.

One difference from Java is that when you link the application, the -lguava flag is needed to include that library.