Re: [sbt] How to use jar in a scala sbt project?

2,457 views
Skip to first unread message

Seth Tisue

unread,
Jul 8, 2012, 10:05:21 AM7/8/12
to simple-b...@googlegroups.com
On Sun, Jul 8, 2012 at 1:35 AM, Feng Zhou <fe...@zhou.es> wrote:
> I'd like to use a jar file in my scala sbt project. The sbt documentation on
> library managements sounds like it's simple to do so: just put the jars into
> the lib directory; but it didn't work for me.
>
> I assumed the lib directory it refers to is a directory named "lib" under
> the base directory, as in my_project/lib, is this correct? If not, can you
> please let me know where to put the jars and whether I need to modify any
> sbt definition to use jars?

"Just put the jars into the lib directory" should work. Demonstration
transcript:

% mkdir foo
% cd foo
% mkdir -p src/main/scala
% cat > src/main/scala/S.scala
object S {
import org.junit.Assert._
}
% sbt compile
[info] Set current project to default-285204 (in build file:/Users/tisue/foo/)
[info] Updating {file:/Users/tisue/foo/}default-285204...
[info] Resolving org.scala-lang#scala-library;2.9.1 ...
[info] Done updating.
[info] Compiling 1 Scala source to
/Users/tisue/foo/target/scala-2.9.1/classes...
[error] /Users/tisue/foo/src/main/scala/S.scala:2: object junit is not
a member of package org
[error] import org.junit.Assert._
[error] ^
[error] one error found
[error] {file:/Users/tisue/foo/}default-285204/compile:compile:
Compilation failed
[error] Total time: 2 s, completed Jul 8, 2012 10:02:16 AM
% mkdir lib
% curl -O 'http://repo1.maven.org/maven2/junit/junit/4.10/junit-4.10.jar'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 247k 100 247k 0 0 91257 0 0:00:02 0:00:02 --:--:-- 106k
% mv junit-4.10.jar lib
% sbt compile
[info] Set current project to default-285204 (in build file:/Users/tisue/foo/)
[info] Compiling 1 Scala source to
/Users/tisue/foo/target/scala-2.9.1/classes...
[success] Total time: 2 s, completed Jul 8, 2012 10:03:45 AM
%

--
Seth Tisue | Northwestern University | http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/

Feng Zhou

unread,
Jul 9, 2012, 1:03:28 AM7/9/12
to simple-b...@googlegroups.com
Thanks a lot for your answer Seth, now that I know it should work this way, it must be a problem with my jdk or classpath etc.

Feng Zhou

unread,
Jul 9, 2012, 8:16:26 AM7/9/12
to simple-b...@googlegroups.com
It turned out that "Just put the jars into the lib directory" does work, the problem was that I packaged the jar wrongly.

In my import statement, I wrote "import someDomain.SomeClass"; but all class files are in the base directory of the jar---there's no "someDomain" directory in the jar. I put the class files in "someDomain" directory and make a jar from "someDomain" directory, then it works.
Reply all
Reply to author
Forward
0 new messages