简体   繁体   English

使用Java主类的Eclipse可执行Scala Jar

[英]Executable Scala Jar from Eclipse using Java main class

I am trying to create an executable Jar from my Scala project. 我正在尝试从我的Scala项目中创建一个可执行的Jar。 Therefore, I followed a tutorial. 因此,我遵循了一个教程。 It suggested creating a Java main class within eclipse, which calls the Scala entry point. 它建议在eclipse中创建一个Java主类,该类调用Scala入口点。 This works fine when executing within eclipse. 在eclipse中执行时,这工作正常。 After adding this class I was able to export an executable jar. 添加此类后,我能够导出可执行jar。 However, it wont work with 但是,它不能与

java -jar myjar.jar

I made sure to activate "Package required library into jar" when exporting. 导出时,请确保激活“将所需的库打包到jar中”。 My Java main class looks like this (where Driver is also located in the package core) 我的Java主类如下所示(驱动程序也位于包核心中)

package core;

public class Main {
     public static void main(String[] args) {
        Driver.main(args);
    }
}

And when executing the exported char the follwing error is thrown, which I can debug: 当执行导出的字符时,会引发以下错误,我可以调试该错误:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.NoClassDefFoundError: scala/collection/Seq
    at core.Driver$.main(Driver.scala:14)
    at core.Driver.main(Driver.scala)
    at core.Main.main(Main.java:5)
    ... 5 more
Caused by: java.lang.ClassNotFoundException: scala.collection.Seq
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)

You could use the One-jar application to guarantee all the necessary jars are in the jarfile, I suspect the scala-lang jars are not in it or in the classpath of the command line. 您可以使用One-jar应用程序来确保所有必需的jar都在jarfile中,我怀疑scala-lang jar不在其中或在命令行的classpath中。

Maven: https://code.google.com/p/onejar-maven-plugin/ SBT: https://github.com/sbt/sbt-onejar Maven: https//code.google.com/p/onejar-maven-plugin/ SBT: https//github.com/sbt/sbt-onejar

If you aren't using either maven or SBT I would suggest switching to them as well as they are the main supported build systems in Scala 如果您既不使用maven也不使用SBT,我建议您切换到它们,因为它们是Scala中受支持的主要构建系统

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

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