从Eclipse将Scala项目导出为jar [英] Exporting Scala project as jar from Eclipse
问题描述
我有一个Scala项目,我想导出为一个jar。
* 1。一开始我尝试为项目创建一个Java类作为入口点。
public class JMain {
public static void main(String [] args){
System.out.println(Java main call Scala main);
SMain.main(new String [] {}); //SMain.main是实际的* main *
,但是当我导出它作为jar它会给我18个例外等等。我现在知道如何复制然后环境,其中Eclipse管理启动这一点,我确信它依赖于事实,Scala在我的系统已经 - 我需要一个自包含的jar包含所有的东西。 / p>
* 2。我的第二个尝试是尝试 lach 在这里建议如何从Eclipse部署Scala项目?
即:
public class JMain {
public static void main(String [] args){
System.out.println(Java Main);
List< String> argList = new ArrayList< String>();
argList.add(fully.qualified.ClassName); // ???
for(String s:args)argList.add(s);
scala.tools.nsc.MainGenericRunner.main(argList.toArray(new String [0]));
这次它甚至不会从Eclipse运行,虽然它只提供了6个左右的异常着名的 NoClassDefFoundError 。我有一种感觉,我没有得到 fully.qualified.ClassName 。 * 3。如果 main Scala类名为Dis.scala,并且位于包pack中,那么 fully.qualified.ClassName 应该是pack.Dis?
我使用Jre 1.6和Scala 2.9.2
编辑:我已经包括所有外部导入的jar,甚至scala-library.jar - 一切都很好并包装在罐子里
PS我不熟悉Ant或Maven或Sbt。
这里有什么对我有用的:
1.创建scala项目
2.创建Wrapper java项目
3.将scala-library.jar添加到您的java项目构建路径。
所以你只需要第三步,另外,因为其余的看起来类似于我做的。然后你可以高兴地使用:java - jar file.jar
编辑:
如何创建一个包含Scala / Code的JAR文件使用Scala - Eclipse IDE。
- 创建一个新的Scala项目并定义一个以main方法作为入口点的对象。 li>
- 现在创建一个新的Java项目,并将您的Scala项目添加到新的构建路径。 现在在java项目中创建一个Wrapper类,它从scala库中调用入口点类。
- 使用Export-> Java-> Runnable JAR文件,向导现在可以在包装器项目上运行该包装器类来创建eclipse运行配置并测试是否可以调用scala项目。 eclipse运行配置将用作JAR中的入口点。根据您的需要,您可能需要:
将所需的库解压缩到生成的JAR中
或
将生成的JAR包所需的库包装成
最后你得到一个完整的打包JAR,你可以这样使用:
java - jar wrapped.jar
I have a Scala project and I would like to export it as a jar.
*1. At first I tried creating a Java class for the project as an entry point
public class JMain {
public static void main(String[] args) {
System.out.println("Java main calling Scala main");
SMain.main(new String[] {""}); //SMain.main is the actual *main*
and this worked fine and dandy when launched from Eclipse, but when I export it as jar it'll give me 18 exceptions or so. I do now know how to replicate then "environment" in which Eclipse manages to launch this and I'm prety sure it relies on the fact that Scala is on my system already - I need a self contained jar with everything packed in there.
*2. My second try consisted of trying what lach suggested here How to deploy a Scala project from Eclipse? namely:
public class JMain {
public static void main(String[] args) {
System.out.println("Java Main");
List<String> argList = new ArrayList<String>();
argList.add("fully.qualified.ClassName"); //???
for (String s : args) argList.add(s);
scala.tools.nsc.MainGenericRunner.main(argList.toArray(new String[0]));
This time it won't even run from Eclipse, although it gives only 6 or so exceptions starting with the famous NoClassDefFoundError. I have a feeling I'm not getting fully.qualified.ClassName right. *3. If the main Scala class is called "Dis.scala" and is located in package "pack" shouldn't this fully.qualified.ClassName be "pack.Dis"?
I'm using Jre 1.6 and Scala 2.9.2 EDIT: I have included all external imported jars, even scala-library.jar - everything is nice and packed in the jar
P.S. I am not familiar with Ant or Maven or Sbt. I just want my Scala project jared - if possible without getting into hairy things.
Here is what worked for me: 1. Create scala project 2. Create Wrapper java project 3. Add the scala-library.jar to you java project build path.
So you only need the 3rd step in addition since the rest looks similar to what I did. Then you can happily use: java - jar file.jar
EDIT: How to create a JAR File which contains Scala/Code which can be consumed by another Java Project, using Scala - Eclipse IDE.
- Create a new Scala Project and define an object with a main method as entry point.
- Now create a new Java Project and add your Scala Project to the new ones buildpath. Additionally add the scala-library.jar to the Java project.
- Now create a Wrapper class in the java project which calls your entry point class from the scala lib. Run the wrapper class to create a eclipse run configuration and test if you can call the scala project.
- Use the Export->Java->Runnable JAR file, Wizard now on the wrapper project.The eclipse run configuration will be used as entrypoint into the JAR. Depending on your needs you may want to : extract required libraries into generated JAR or Package required libraries into generated JAR
Finally you get a complete packaged JAR which you can use like this:
java - jar wrapped.jar
这篇关于从Eclipse将Scala项目导出为jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!