20

I have a local jar file named mylib.jar. I want to used it as a dependency in my Gradle Java project.

This is what I tried:

I created a libs/ folder under project root. I put the jar file under libs/ folder.

MyProject
 ->libs/mylib.jar
 ->build.gradle
 ->src/...

In my build.gradle:

apply plugin: 'java-library'

group 'com.my.app'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()

    flatDir {
        dirs 'libs'
    }
 }

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    api files('libs/mylib.jar')
}

But I can't access the public classes defined in mylib.jar in my project code. Why?

===== More information =====

The content of my jar:

mylib.jar
    > com.my.jar.package
      >ClassFromJar.class

Here is how I use the jar:

// Compilation error: Cannot resolve symobl 'ClassFromJar'
import com.my.jar.package.ClassFromJar;

public class MyEntryPoint {
    // Compilation error: Cannot resolve symbol 'ClassFromJar'
    ClassFromJar instance = new ClassFromJar();
}
Leem.fin
  • 40,781
  • 83
  • 202
  • 354
  • What do you mean when you say *"can't access the public classes"*? Does the compilation via Gradle fail? Do you use an IDE? – Lukas Körfer Sep 23 '19 at 10:24
  • I mean those public classes declared in jar can't be resolved by compiler. – Leem.fin Sep 23 '19 at 12:22
  • 1
    But how do you compile them? How do you use Gradle? – Lukas Körfer Sep 23 '19 at 12:23
  • 1
    Your setup works for me. There must be something bad in your local setup that is not mentioned in your question. Is the JAR file ok? What exact Gradle command fails with which error? How does your “project code” look? Can you update your question with a [mcve] that shows the error? – Chriki Sep 23 '19 at 21:40
  • At compile time, I get `Can not find symbol` error which complains the class from Jar can't be resolved. The jar is fine because I can see the content of the jar , it contains the class file of the class I am using. – Leem.fin Sep 24 '19 at 19:13
  • I updated my question with more information. – Leem.fin Sep 24 '19 at 19:32
  • Regarding the content of your `jar` file: Is the package part a single folder or multiple folders? Also, could you please add the output of running `gradle build` to your question? – Lukas Körfer Sep 26 '19 at 11:23
  • 1
    @Leem.fin What IDE are you using? Does your IDE's compiler fail to resolve them or does Gradle's command-line compiler fail to resolve them? Those are two VERY different things and you haven't answered that yet. – Michael Ziluck Sep 26 '19 at 19:37
  • @MichaelZiluck I am using IntellJ Community edition. I will check what you are asking tonight and get back to you. Thanks. – user842225 Oct 15 '19 at 08:17

4 Answers4

14

Similar answers suggesting

  1. Local dir

Add next to your module gradle (Not the app gradle file):

repositories {
   flatDir {
       dirs 'libs'
   }
}
  1. Relative path:
dependencies {
    implementation files('libs/mylib.jar')
}
  1. Use compile fileTree:
compile fileTree(dir: 'libs', include: 'mylib.jar')
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • I am not in Android project. I have only one gradle file in my Gradle project, that's `myProject/build.gradle`, so, do I miss something if you say "Add next to your module gradle (Not the app gradle file)" ? – Leem.fin Sep 23 '19 at 08:41
  • 1
    And I actually have the `flatDir` thing in my build.gradle. – Leem.fin Sep 23 '19 at 08:44
  • @leem.fin see https://riptutorial.com/gradle/example/8349/add-a-local-jar-file-dependency – Ori Marko Sep 23 '19 at 08:53
  • 2
    Thanks but problem is I have followed all those guidelines, but my project just can't resolve the public class from jar. It is not that I'm not following it. – Leem.fin Sep 23 '19 at 12:21
  • @leem.fin can you add class(or main parts) and how you try to use it? – Ori Marko Sep 23 '19 at 12:49
  • I updated my question with how I try to use the jar. – Leem.fin Sep 24 '19 at 19:26
  • @leem.fin maybe it's something as file/folder permission or case sensitive issue or you need to run clean or missing other dependency jar? – Ori Marko Sep 25 '19 at 04:33
2

A flatDir repository is only required for *.aar(which is an Android specific library format, completely irrelevant to the given context). implementation and api affect the visibility, but these are also Android DSL specific). In a common Java module, it can be referenced alike this:

dependencies {
    compile fileTree(include: ["*.jar"], dir: "libs")
}

And make sure to drop the *.jar into the correct one libs directory, inside the module.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
0

I thinks you should use in dependency declaration compile name: 'mylib' in this case flatDir will search for mylib.jar.

You could try following build.gradle (it works in my project):

plugins {
    id 'java'
}
apply plugin: 'java-library'

group 'dependency'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile  name: 'mylib'
}
i.bondarenko
  • 3,442
  • 3
  • 11
  • 21
0

Just in case:

1 - compiler must have access to lib directory and jar example:

javac -cp .;lib\* *.java

2 - ALSO import must be mentioned in java file example in your java add

import static org.lwjgl.glfw.GLFW.*;
Chronoslog
  • 107
  • 8