sbt包在sbt 0.11.2中创建一个独立的jar

时间:2011-12-12 05:55:59

标签: scala sbt

如何在sbt 0.11.2生成一个独立的jar?

现在我正在尝试sbt package,当我尝试在生成的jar文件上运行java -jar app.jar时,它会出现以下异常:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/App
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at Test.main(Test.scala)
Caused by: java.lang.ClassNotFoundException: scala.App
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

3 个答案:

答案 0 :(得分:5)

检查sbt-onejar - 一个用于构建单个可执行JAR的插件,其中包含所有代码和依赖项作为嵌套JAR。

答案 1 :(得分:2)

我能够使用https://github.com/sbt/sbt-assembly

实现这一目标

只需按照https://github.com/sbt/sbt-assembly/blob/master/README.md中的说明操作,然后运行sbt assembly

答案 2 :(得分:0)

E.g。使用https://github.com/ymasory/sbt-prototype制作可执行jar的Proguard

~/src$ git clone https://github.com/ymasory/sbt-prototype.git test
Cloning into 'test'...
[...]
~/src$ cd test/
~/src/test$ sbt sbt-version
[info] Loading project definition from /home/user/src/test/project
[info] Set current project to myproj (in build file:/home/user/src/test/)
[info] 0.11.2
~/src/test$ sbt compile proguard
[info] Loading project definition from /home/user/src/test/project
[info] Updating {file:/home/user/src/test/project/}default-d8cff0...
[info] Resolving com.github.siasia#xsbt-proguard-plugin_2.9.1;0.11.2-0.1.1 ...
[info] Resolving net.sf.proguard#proguard-base;4.6 ...
[...]
[info] Compiling 1 Scala source to /home/user/src/test/target/scala-2.9.2/classes...
[success] Total time: 7 s, completed May 11, 2012 8:08:15 PM
[info] Packaging /home/user/src/test/target/scala-2.9.2/myproj_2.9.2-1.0.0-SNAPSHOT.jar ...
[info] Done packaging.
ProGuard, version 4.6
[...]
[success] Total time: 44 s, completed May 11, 2012 8:09:00 PM
~/src/test$ java -version
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.1) (6b24-1.11.1-4ubuntu2)
OpenJDK Client VM (build 20.0-b12, mixed mode, sharing)
~/src/test$ java -jar target/scala-2.9.2/myproj_2.9.2-1.0.0-SNAPSHOT.min.jar 
hello myproj
~/src/test$