我试图在jar
中启动scala应用程序。
以下代码:
object Starter {
def main(args: Array[String]): Unit = {
args.foreach(path => {
val (minX, maxX, minY, maxY) = MainStage.run(path)
println(minX)
println(maxX)
println(minY)
println(maxY)
})
}
}
在同一台计算机上引发错误(尽管它可以在Intellij Idea中运行):
$ java -cp foo-1.0-SNAPSHOT.jar ogr.bar.system.Starter
Exception in thread "main" java.lang.NoClassDefFoundError: scala/Function1
at ogr.bar.system.Starter.main(Starter.scala)
Caused by: java.lang.ClassNotFoundException: scala.Function1
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
然后我尝试制作一个简单的hello world应用程序:
object Starter {
def main(args: Array[String]): Unit = {
println("hello world")
//args.foreach(path => {
// val (minX, maxX, minY, maxY) = MainStage.run(path)
// println(minX)
// println(maxX)
// println(minY)
// println(maxY)
//})
}
}
并引发异常:
$ java -cp foo-1.0-SNAPSHOT.jar ogr.bar.system.Starter
Exception in thread "main" java.lang.NoClassDefFoundError: scala/Predef$
at ogr.bar.system.Starter$.main(Starter.scala:7)
at ogr.bar.system.Starter.main(Starter.scala)
Caused by: java.lang.ClassNotFoundException: scala.Predef$
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
以下是我的pom.xml
<properties>
<javaVersion>1.8</javaVersion>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.8</version>
</dependency>
<dependency>
<groupId>org.scala-lang.modules</groupId>
<artifactId>scala-xml_2.11</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.11</artifactId>
<version>3.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${javaVersion}</source>
<target>${javaVersion}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDir>src/main/scala</sourceDir>
<testSourceDir>src/test/scala</testSourceDir>
</configuration>
</plugin>
</plugins>
</build>
我应该怎么做才能使我的应用正常工作?
答案 0 :(得分:2)
问题是您可能构建了应用程序的精简版。
它不包含所有依赖项,例如scala-library。因此错误
java.lang.NoClassDefFoundError: scala/Predef$