Scala:创建依赖于外部 Scala 库的小型可执行 Jar

发布于 2024-11-01 22:56:53 字数 858 浏览 1 评论 0原文

我正在尝试以“干净的方式”打包一个小型应用程序(仍在学习 Scala!)。目标是拥有一个可执行的 JAR 文件。我已经完成了以下操作:

  • 使用 sbt 打包 JAR ->将与
scala -cp myjarfile.jar MyClass

或者

java -classpath path\to\scala-library.jar;myjarfile.jar MyClass

但不能与

java -jar myjarfile.jar

因为这样就找不到 scala/ScalaObject 了。在最后一个上添加类路径是没有用的,因为 -jar 选项将忽略 -classpath 选项。请注意,我已在系统 CLASSPATH 变量中添加了 scala 库,但在使用 -jar 时它似乎也被忽略了。

  • 将 scala 库内容(首先解压缩)添加到 sbt 创建的 jar 中。然后就可以了(可以双击并启动 jar),但是操作有点冗长,并且生成的文件有几兆字节大。不理想。

经过几个小时的谷歌搜索后,我发现没有办法创建一个双击时启动的小 jar 文件,并且不涉及痛苦的操作。我缺少什么?我猜想应该有某种方法来定义 scala 库在系统级别的位置。您如何处理想要编译并准备运行以提高效率的小型应用程序?

请注意,当我使用 sbt 时,我手头没有 jar 工具(我依赖于 JRE,而不是 JDK)。

谢谢! 皮尔里克.

I'm trying to package a small application (still learning Scala!) in a "clean way". The goal is to have an executable JAR file. I've done the following:

  • packaged a JAR using sbt -> will work with
scala -cp myjarfile.jar MyClass

or

java -classpath path\to\scala-library.jar;myjarfile.jar MyClass

but won't work with

java -jar myjarfile.jar

because then scala/ScalaObject cannot be found. And no use adding a classpath on this last one, since the -jar option will ignore the -classpath option. Note that I have added the scala libs in my system CLASSPATH variable, but it seems to be ignored too when -jar is used.

  • added the scala library contents (by unzipping them first) to the jar created by sbt. This then works (the jar can be double-clicked and launched), but the manipulation is somewhat lengthy, and the resulting file is several megabytes big. Not ideal.

After hours of googling, I can see no way to create a small jar file that will launch when double-clicked and that doesn't involve a painful manipulation. What am I missing? I'm guessing there should be some way to define where the scala libraries are at system level. How do you deal with small applications that you want to be compiled and ready-to-run for efficiency?

Note that while I'm using sbt, I don't have a jar tool at hand (I'm relying on a JRE, not a JDK).

Thanks!
Pierric.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那些过往 2024-11-08 22:56:53

以下设置对我有用:

  • 将 scala-library.jar 放在与可执行 jar 相同的文件夹中(并从那里调用 java)
  • 将其放入清单中:
    Class-Path: scala-library.jar

另一种选择是将 scala-library.jar 的内容合并到您的应用程序 jar 中。缺点是这会增加其尺寸。但是您可以使用 Proguard 从最终的 jar 中删除未使用的类。我认为有一种简单的方法可以使用 sbt 使用 proguard 打包可执行 jar。

关于使用Proguard进行收缩,你可以看一下这个页面< /a>.这是关于Android开发的;只需忽略这部分并查看收缩结果表即可。一些示例应用程序缩小到不到 100kB。

编辑

也许您需要稍微完善一下您的问题。你想达到什么目的?您只想在您的系统上安装该程序还是想分发它?

如果您想要快速启动 Java 应用程序而不会对 JVM 启动时间产生太大影响,您可以查看 钉枪

The following setup works for me:

  • have scala-library.jar in the same folder as the executable jar (and call java from there)
  • put this into your manifest:
    Class-Path: scala-library.jar

Another option is to merge the contents of scala-library.jar into your application jar. The drawback is that this will increase its size. But you can use Proguard to strip unused classes from your final jar. I think there is an easy way of using sbt to package an executable jar using proguard.

Regarding the shrinking using Proguard, you can have a look at this page. It's about Android development; just ignore this part and have a look at the tables of the shrinking results. Some example applications shrink to less than 100kB.

Edit

Maybe you need to refine your question a bit. What are you trying to achieve? Do you want to install the program only on your system or do you want to distribute it?

If all you want is quickly launching a Java application without much impact of the JVM start-up time you can have a look at nailgun.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文