将 Scala 应用程序导出到可运行的 JAR
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8064699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Export Scala application to runnable JAR
提问by Radu Stoenescu
Can you tell me, if this is possible, how to export a Scala application to a normal runnable JAR that can run directly in the JVM ?
Thanks
您能告诉我,如果可能的话,如何将 Scala 应用程序导出到可以直接在 JVM 中运行的普通可运行 JAR 吗?
谢谢
回答by Tomasz Nurkiewicz
It is perfectly possible, see for instance this: running a maven scala project. Since Scala compiles to Java bytecode, JVM is not even aware of the underlying implementation language.
这是完全有可能的,例如,请参见:running a maven scala project。由于 Scala 编译为 Java 字节码,JVM 甚至不知道底层实现语言。
In essence after compiling Scala sources using scalacyou will get a bunch of .classfiles which you can later package into a JAR. Then you can simply run them using:
本质上,在使用编译 Scala 源代码后,scalac您将获得一堆.class文件,您可以稍后将这些文件打包到 JAR 中。然后您可以简单地使用以下命令运行它们:
$ java -cp "your.jar:scala-library.jar" com.example.Main
Note that you mustinclude scala-library.jaron the CLASSPATH (currently it is almost 9 MiB...) and specify class containing mainmethod.
请注意,您必须scala-library.jar在 CLASSPATH 中包含(目前几乎是 9 MiB...)并指定包含main方法的类。
回答by Fabian
If you use sbt to build you can use one of the one-jar plugins. They will put all dependencys into one big jar file (inclusive all the scala.jar files). This means that you only need one jar file and don't have to manage all the dependencys.
如果您使用 sbt 构建,则可以使用单 jar 插件之一。他们会将所有依赖项放入一个大 jar 文件(包括所有 scala.jar 文件)。这意味着您只需要一个 jar 文件,而不必管理所有依赖项。
As an example with sbt-assembly (mostly copied from https://github.com/sbt/sbt-assembly):
以 sbt-assembly 为例(主要从https://github.com/sbt/sbt-assembly复制):
project/plugins.sbt:
项目/plugins.sbt:
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "X.X.X")
build.sbt:
构建.sbt:
import AssemblyKeys._ // put this at the top of the file
seq(assemblySettings: _*)
then you can generate the jar with:
然后您可以使用以下命令生成 jar:
sbt assembly
回答by Viktor Hedefalk
As an alternative to Fabian's answer, if you're using Maven, you can use the assembly-plugin. Something like:
作为 Fabian 答案的替代方案,如果您使用的是 Maven,则可以使用 assembly-plugin。就像是:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>package-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifestEntries>
<SplashScreen-Image>splash.png</SplashScreen-Image>
</manifestEntries>
<manifest>
<mainClass>se.aptly.epm.main.PrognosisApp</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
That will package all your deps up, including scala-library.jar (if it's in your deps), but will do so flattened with all classes unpacked. This is because runnable jar's cannot out of the box use code in jars in the jar.
这将打包您所有的 deps,包括 scala-library.jar(如果它在您的 deps 中),但会在解压所有类的情况下进行扁平化处理。这是因为可运行的 jar 不能开箱即用地使用 jar 中的 jar 中的代码。
To make that work (which is nicer), use http://code.google.com/p/onejar-maven-plugin/, I think it's a Maven mojo wrapper to one-jar: http://one-jar.sourceforge.net/
为了使这项工作(更好),请使用http://code.google.com/p/onejar-maven-plugin/,我认为它是一个 Maven mojo 包装器到 one-jar:http://one-jar。 sourceforge.net/
There is also an sbt-plugin for one-jar: https://github.com/sbt/sbt-onejar
还有一个用于 one-jar 的 sbt-plugin:https: //github.com/sbt/sbt-onejar
回答by Antoine
In order to package a swing application in a runnable jar, the solution that worked for me was to export my project as a normal jarfile (non executable) and update the jar's manifestto:
为了将 Swing 应用程序打包到可运行的 jar 中,对我有用的解决方案是将我的项目导出为普通 jar文件(不可执行)并将 jar 的清单更新为:
- add scala-library.jar scala-swing.jar packages to the path
- indicate the main class
- 将 scala-library.jar scala-swing.jar 包添加到路径
- 表示主类
You can find the Manifest file inside the jar (that you can open with 7z for example) at the following path:
您可以在以下路径中找到 jar 中的 Manifest 文件(例如,您可以使用 7z 打开):
META-INF/MANIFEST.MF
Add the following lines at the end of the manifest:
在清单的末尾添加以下几行:
Main-Class: myPackage.myMainClass
Class-Path: scala-library.jar scala-swing.jar
Now your jar should execute properly when clicking on it.
现在你的 jar 应该在点击它时正确执行。
NOTE: You can find more information about manifest customizing here: http://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html.
注意:您可以在此处找到有关清单自定义的更多信息:http: //docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html。
回答by HHH
Hers is my solution, maven -->create scalarunnable jar.
Hers 是我的解决方案,maven -->create scalarunnable jar。
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<execution>
<id>scala-compile-first</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<includes>
<include>**/*.scala</include>
</includes>
</configuration>
</execution>
<execution>
<id>scala-test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>xxx.xxx.xxx.Main</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>reference.conf</resource>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>

