Search code examples
javascalaopencvintellij-ideasbt

How to add external jar files in build.sbt in intellij idea?


I was trying to use openCV in Intellij Idea (scala), for that I've downloaded openCV from their official website - and after installing openCV I got opencv-480.jar file in build/bin. For installation I have done (in Ubuntu):

$ sudo apt update && sudo apt install -y cmake g++ wget unzip
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/4.x.zip
$ unzip opencv.zip
$ mkdir -p build && cd build
$ cmake ../opencv-4.x
$ make -j4
$ sudo make install

Then I've opened my Intellij Idea and performed following steps:

  1. Go to (file -> project structure -> modules)
  2. Clicked on + sign
  3. Selected (library -> java)
  4. Selected that opencv-480.jar
  5. Clicked on apply and then ok

Above steps added jar file. Then I added library path by doing:

  1. Go to (file -> project structure -> libraries)
  2. Clicked on recently added library (opencv-480)
  3. Then in right panel, I clicked on + sign
  4. Selected lib folder which I got after installation of openCV

After all above, now I'm able to access and use openCV in my scala project.

But the problem is whenever I clean the project and run in sbt-shell, it removes that jar file from project structure. Now I want to add that external jar file and setting of lib folder path via build.sbt - but I'm not able to find any help on this.

Guide me how can I add external jar file and use it as dependency in build.sbt.


Solution

  • I've solved above issue. What I've done is:

    1. Made a opencv_lib folder in root.
    2. Put opencv-480.jar file in this opencv_lib folder.
    3. Made another native_libs folder inside opencv_lib folder.
    4. Put files of lib folder (which I got after installing opencv) into that native_libs folder.

    Then at last added some lines in build.sbt, my project's build.sbt is:

    ThisBuild / version := "0.1.0-SNAPSHOT"
    
    ThisBuild / scalaVersion := "2.13.4"
    
    lazy val root = (project in file("."))
      .settings(
        name := "OpenCV-Check"
      )
    
    run / fork := true
    
    unmanagedBase := baseDirectory.value / "opencv_lib"
    
    val openCVNativeLibPath = file("opencv_lib/native_libs")
    
    run / javaOptions += s"-Djava.library.path=${openCVNativeLibPath.getAbsolutePath}"
    

    Then I build the project and it worked for me.

    https://www.scala-sbt.org/release/docs/Library-Dependencies.html

    Above linked helped me. Thanks to Gael J for that link.