简体   繁体   English

使用 java -jar 运行 Scala 应用程序

[英]running scala apps with java -jar

I got some problems with the java.我在 java 上遇到了一些问题。 Check it out.一探究竟。

sebastian@sebastian-desktop:~/scaaaaaaaaala$ java -cp /home/sebastian/.m2/repository/org/scala-lang/scala-library/2.8.0.RC3/scala-library-2.8.0.RC3.jar:target/scaaaaaaaaala-1.0.jar scaaalaaa.App

Hello World!

That's cool, right, but how bout this:这很酷,对,但是如何:

sebastian@sebastian-desktop:~/scaaaaaaaaala$ java -cp /home/sebastian/.m2/repository/org/scala-lang/scala-library/2.8.0.RC3/scala-library-2.8.0.RC3.jar -jar target/scaaaaaaaaala-1.0.jar

Exception in thread "main" java.lang.NoClassDefFoundError: scala/Application
 at java.lang.ClassLoader.defineClass1(Native Method)
 at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
 at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
 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:307)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
 at scaaalaaa.App.main(App.scala)
Caused by: java.lang.ClassNotFoundException: scala.Application
 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:307)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
 ... 13 more

Wat the heck?见鬼了? Any idea why the first works and not the 2nd?知道为什么第一个有效而不是第二个吗? How do I -jar my scala??我如何 -jar 我的 Scala?

My thanks in advance, brother.我先谢谢你,兄弟。

If you define -jar the -classpath is ignored:如果您定义 -jar,则 -classpath 将被忽略:

Java manual: Java手册:

-jar When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored. -jar 使用此选项时,JAR 文件是所有用户类的来源,其他用户类路径设置将被忽略。

You can define the classpath dependencies in the Manifest metadata .您可以在Manifest 元数据中定义类路径依赖项。

The easiest way to start your app is using the scala scripts:启动应用程序的最简单方法是使用 scala 脚本:

scala -classpath target/scaaaaaaaaala-1.0.jar scaaalaaa.App Hello World!

Simply running简单地运行

scala scaaaaaaaaala-1.0.jar 

works for me with 2.11.6 2.11.6 对我有用

For an executable jar, the classpath should be in the jar's manifest.对于可执行 jar,类路径应该在 jar 的清单中。 For help on doing that, look through the answers to Stackoverflow: How to create an executable jar with dependancy jars .有关这样做的帮助,请查看Stackoverflow: How to create an executable jar with dependency jars的答案。

use sbt-assembly sbt plugin to produce a jar containing all dependencies使用sbt-assembly sbt 插件生成一个包含所有依赖项的 jar

sbt-assembly github sbt-assembly github

eg add a line in project/plugins.sbt :例如在project/plugins.sbt添加一行:

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.10")

adjust your build.sbt eg with调整你的build.sbt例如

mainClass in (assembly) := Some("mypackage.mysubpackage.MyMainClass")
assemblyJarName in assembly := "my_assembly.jar"

then build your assembled jar with然后用

sbt assembly

and launch it with并启动它

java -jar ./target/scala-2.12/my_assembly.jar

Et voilà, no class-path this & thats needed any more.等等,不再需要这样和那样的类路径。 only 1 jar.只有 1 罐。

Below is the way to execute the Scala Executable Jar下面是执行 Scala Executable Jar 的方法

  • We need scala installed in your system.我们需要在您的系统中安装 scala。

  • Here first will give the jar path and jar name这里首先会给出jar路径和jar名称

  • If your code needs any dependency jar then keep all jar in one directory and give a path of this in command like below after the ":".如果您的代码需要任何依赖 jar,那么将所有 jar 保存在一个目录中,并在命令中的“:”之后给出一个路径,如下所示。

  • At last give your class/object name.最后给出你的类/对象名称。

scala -cp "/your/jar/path/jar_name.jar:/your/dependency/jar/path/*" SampleCode scala -cp "/your/jar/path/jar_name.jar:/your/dependency/jar/path/*" SampleCode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM