从 scala 文件创建可执行 jar 时出现问题

发布于 2024-09-10 21:54:28 字数 2602 浏览 10 评论 0原文

我正在尝试使用 IntelliJ 9.0 将我的项目导出为 jar。我的项目在 Intellij 中编译和运行没有问题,但是当我将其写入 .jar 并打开它时,它会显示错误。

我的主类类似于:

package Main

//Imports

object Main{
  def main(args: Array[String]) {
    println("Main: Hello, world!")
    //do stuff
}

现在,在工件窗口中,我创建了一个包含以下内容的 .jar:

  1. 主类:Main.Main
  2. 类路径:lib/javacsv lib/scala-compiler。 jar lib/scala-library.jar lib/scalatest-1.0-test.jar lib/scalatest-1.0.jar lib/tools.jar lib/jtds-1.2.2.jar lib/flex-messaging-common.jar lib/flex -messaging-core.jar lib/spring.jar lib/mysql-connector-java-5.1.7-bin.jar lib/ojdbc14.jar lib/commons-logging.jar lib/postgresql-8.4-701.jdbc3.jar lib /log4j-1.2.15.jar lib/poi-3.6-20091214.jar lib/poi-ooxml-3.6-20091214.jar lib/dom4j-1.6.1.jar lib/poi-ooxml-schemas-3.6-20091214.jar lib/geronimo-stax-api_1.0_spec-1.0.jar lib/xmlbeans-2.3.0.jar lib/rt.jar lib/ifxjdbc.jar lib/db2jcc4.jar

我已经仔细检查过所有这些类都是在该项目上,并且是该项目上唯一的课程。请注意,它包含 lib/scala-compiler.jar 和 lib/scala-library.jar。

构建项目:java -jar myScalaApp.jar,我得到:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:675)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:316)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:288)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:374)
    at Main.Main.main(Main.scala)
Caused by: java.lang.ClassNotFoundException: scala.ScalaObject
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:316)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:288)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:374)
    ... 13 more

我知道它没有找到 scala 类,但我确保它们在那里。还有什么问题,我该如何解决它?

I'm tryign to export my project as a jar with IntelliJ 9.0. My project compiles and runs with no problem in Intellij, but when I write it to a .jar and open it, it will show an error.

My Main class is something like:

package Main

//Imports

object Main{
  def main(args: Array[String]) {
    println("Main: Hello, world!")
    //do stuff
}

Now, in the artifacts window I created a .jar with the following:

  1. Main Clas: Main.Main
  2. Class Path: lib/javacsv lib/scala-compiler.jar lib/scala-library.jar lib/scalatest-1.0-test.jar lib/scalatest-1.0.jar lib/tools.jar lib/jtds-1.2.2.jar lib/flex-messaging-common.jar lib/flex-messaging-core.jar lib/spring.jar lib/mysql-connector-java-5.1.7-bin.jar lib/ojdbc14.jar lib/commons-logging.jar lib/postgresql-8.4-701.jdbc3.jar lib/log4j-1.2.15.jar lib/poi-3.6-20091214.jar lib/poi-ooxml-3.6-20091214.jar lib/dom4j-1.6.1.jar lib/poi-ooxml-schemas-3.6-20091214.jar lib/geronimo-stax-api_1.0_spec-1.0.jar lib/xmlbeans-2.3.0.jar lib/rt.jar lib/ifxjdbc.jar lib/db2jcc4.jar

I have double checked that all those classes are on the project and are the onnly classes on it. Notice that it inlcudes lib/scala-compiler.jar and lib/scala-library.jar.

Build the project: java -jar myScalaApp.jar and I get:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:675)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:316)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:288)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:374)
    at Main.Main.main(Main.scala)
Caused by: java.lang.ClassNotFoundException: scala.ScalaObject
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:316)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:288)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:374)
    ... 13 more

I understand that it's not finding the scala classes but I made sure they are there. What else can be the problem and how could I fix it?

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

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

发布评论

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

评论(2

作死小能手 2024-09-17 21:54:28

我想说有两件事出了问题:

  • scala-library.jar 不在你的 jar 中;或者
  • 运行时的类路径不包含 scala-library.jar。

我不知道 IntelliJ 如何构建你的 jar:它是否解压所有库 jar 并将它们与你的代码一起放入一个大 jar 中,或者是否将库 jar 按原样添加到你的大 jar 中并操作类路径以获取在罐中罐?

第一点很容易检查:使用 jar -tvf yourJar.jar 列出 jar 的内容,或者使用存档查看器以图形方式查看它。请注意,.jar 基本上是 .zip,因此您可以重命名扩展名,然后使用可以查看 .zip 文件的工具。

只是对第二部分有一个想法:您可以构建一个简单的 Java 主类来打印 System.getProperty("java.class.path") 吗? Java 类应该能够在该 jar 中运行,即使 Scala 类不能运行。

I'd say one of two things has gone wrong:

  • scala-library.jar is not in your jar; or
  • the class path at runtime doesn't include scala-library.jar.

I don't know how IntelliJ builds your jar: Does it unpack all your library jars and mung them together with your code into one big jar, or does it add the library jars as-is to your big jar and manipulate the classpath to get at the jars-within-jar?

The first point is easy enough to check: Either use jar -tvf yourJar.jar to list out the contents of your jar, or use an archive viewer to look into it graphically. Note that a .jar is basically a .zip, so you can rename the extension and then use a tool that can look into .zip files.

Just had an idea about the second part: Can you build a simple Java main class that prints out System.getProperty("java.class.path") ? A Java class should be able to run in that jar even if a Scala class isn't.

甜味超标? 2024-09-17 21:54:28

至少有三种方法可以做到这一点。 (一个适用于 Netbeans,但也应该在 Idea 中工作)

There are at least three ways to do this. (One applies to Netbeans but should work in Idea too)

  • Use jarjar as explained here
  • Use the idea on this page (not so elegant but the easiest) - essentially unpack the Scala directory from scala-library.jar and add this directory to your jar.
  • Use the idea on this page (for NB) - essentially add "scala-library.jar" to your project libraries.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文