Rory O'Kane
Rory O'Kane

Reputation: 30418

JAR file library included using Gradle fileTree isn’t being seen by Java code

I created a new Java project in IntelliJ IDEA using its Gradle project template, and I am trying to set up a library so that my project can use it. I saved the library JAR file to ./libs/frink-2014-02-17.jar in the project. In the dependencies section of my build.gradle, I wrote runtime fileTree(dir: 'libs', include: '*.jar'). But my Java code cannot see that library. When I try to build the project, the compiler complains that import frink.parser.Frink; references a package that does not exist.

How can I fix my build errors, so that I can use the Frink library in my Java project?

Details

The library is Frink. Frink is not in the Maven repository, so I downloaded its JAR file, renamed it so it has the version in the filename (following the recommendation of the Gradle docs), and put it in a libs folder at the root of the project. Then I copied the following line of code from Gradle User Guide: File dependencies to try to include all JAR files in that libs directory I created:

runtime fileTree(dir: 'libs', include: '*.jar')

But when I “Make Project”, or run ./gradlew build, I get these errors:

/…/frink-soulver/src/main/java/Main.java:1: package frink.parser does not exist
import frink.parser.Frink;
                   ^     
/…/frink-soulver/src/main/java/Main.java:5: cannot find symbol
symbol  : class Frink    
location: class Main     
        Frink interp = new Frink();
        ^                
/…/frink-soulver/src/main/java/Main.java:5: cannot find symbol
symbol  : class Frink    
location: class Main     
        Frink interp = new Frink();
                           ^
/…/frink-soulver/src/main/java/Main.java:11: package frink.errors does not exist
        } catch (frink.errors.FrinkEvaluationException fee) {
                             ^
4 errors                 
:compileJava FAILED

My IDE highlights the same errors when I open and view Main.java, before I compile it.

My code in Main.java, including the import, is just the Frink sample code. And the Frink online JavaDocs indicate that there indeed should be a frink.parser package in that JAR file. So it’s not that I’m asking for a package that doesn’t exist.

I have tried changing runtime to compile in build.gradle, but that didn’t make a difference.

When I open “Project Structure…” in IDEA and go to the Libraries section, the Frink JAR file is missing from that list. The list contains “Gradle: guava-12.0”, “Gradle: hamcrest-core-1.3”, “Gradle: jsr305-2.0.3”, and “Gradle: junit-4.11”. I don’t want to just click the + button to add a library using IDEA, because I think it would be confusing to have libraries included in two separate ways.

I’ve searched for “Gradle fileTree”, but it looks like I’m using fileTree the same way as in those results.

I’m new to Gradle, IDEA, and setting up Java projects from scratch. So if any of my project setup seems weird or nonstandard, I am open to corrections. For example, if there’s a better way to include Frink than copying its JAR file into a folder in my project, I can do that.

Detailed project contents

My full build.gradle:

apply plugin: 'java'

sourceCompatibility = 1.6
version = '1.0'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'

    /* commenting this out for the question because the error still happens without this
    // http://code.google.com/p/guava-libraries/wiki/UseGuavaInYourBuild
    runtime group: 'com.google.guava', name: 'guava', version: '12.0'
    // http://stackoverflow.com/questions/10007994/why-do-i-need-jsr305-to-use-guava-in-scala/10013226#10013226
    // I'll include this dependency in case I decide I want to use these annotations
    compile group: 'com.google.code.findbugs', name: 'jsr305', version: '2.0.3'
    */

    runtime fileTree(dir: 'libs', include: '*.jar')
}

My project file tree, as generated by tree:

.
├── build
│   ├── classes
│   │   └── main
│   └── dependency-cache
├── build.gradle
├── frink-soulver.iml
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── libs
│   └── frink-2014-02-17.jar
├── out
│   ├── production
│   │   └── frink-soulver
│   └── test
│       └── frink-soulver
└── src
    ├── main
    │   ├── java
    │   │   └── Main.java
    │   └── resources
    └── test
        ├── java
        └── resources

My full Main.java:

import frink.parser.Frink;

public class Main {
    public static void main(String[] args) {
        Frink interp = new Frink();
        interp.setRestrictiveSecurity(true);

        try {
            String results = interp.parseString("2+2");
            System.out.println(results);
        } catch (frink.errors.FrinkEvaluationException fee) {
            // Do whatever you want with the exception
        }
    }
}

Upvotes: 3

Views: 8398

Answers (2)

Anatolii Shuba
Anatolii Shuba

Reputation: 6175

Try to change your dependency rule from the runtime fileTree(dir: 'libs', include: '*.jar') to the compile files('libs/MyLibrary.jar'). This works for me.

I have created issue on Android bug tracker, please feel free to comment it.

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272687

You've specified it as a dependency for runtime, but you need it when you compile...

Upvotes: 2

Related Questions