从Scala文件创建jar文件 [英] Creating a jar file from a Scala file

查看:122
本文介绍了从Scala文件创建jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Scala的新手,不懂Java。我想用简单的Scala文件创建一个jar文件。所以我有我的HelloWorld.scala,生成一个HelloWorld.jar。

I'm new to Scala and don't know Java. I want to create a jar file out of a simple Scala file. So I have my HelloWorld.scala, generate a HelloWorld.jar.

Manifest.mf:

Manifest.mf:

Main-Class: HelloWorld

在我运行的控制台中:

fsc HelloWorld.scala
jar -cvfm HelloWorld.jar Manifest.mf HelloWorld\$.class HelloWorld.class
java -jar HelloWorld.jar 
  => "Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/jar"

java -cp HelloWorld.jar HelloWorld 
  => 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$100(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:280)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:374)
    at hoppity.main(HelloWorld.scala)


推荐答案

示例目录结构:

X:\scala\bin
X:\scala\build.bat
X:\scala\MANIFEST.MF
X:\scala\src
X:\scala\src\foo
X:\scala\src\foo\HelloWorld.scala

HelloWorld.scala:

HelloWorld.scala:

//file: foo/HelloWorld.scala
package foo {
  object HelloWorld {
    def main(args: Array[String]) {
      println("Hello, world!")
    }
  }
}

MANIFEST.MF:

MANIFEST.MF:

Main-Class: foo.HelloWorld
Class-Path: scala-library.jar

build.bat:

build.bat:

@ECHO OFF

IF EXIST hellow.jar DEL hellow.jar
IF NOT EXIST scala-library.jar COPY %SCALA_HOME%\lib\scala-library.jar .

CALL scalac -sourcepath src -d bin src\foo\HelloWorld.scala

CD bin
jar -cfm ..\hellow.jar ..\MANIFEST.MF *.*
CD ..

java -jar hellow.jar






为了成功使用 -jar 开关,您需要 META-INF /中的两个条目MANIFEST.MF 文件:主类;任何依赖项的相对URL。文档说明:


In order to successfully use the -jar switch, you need two entries in the META-INF/MANIFEST.MF file: the main class; relative URLs to any dependencies. The documentation notes:


-jar

执行封装在
JAR文件中的程序。第一个参数是JAR文件的
名称,而不是
启动类名称。为了使这个
选项起作用,
JAR文件的清单必须包含一行
表格Main-Class:classname。这里,
classname标识具有
的类public static void main(String []
args)方法,该方法用作
应用程序的起点。有关使用Jar
文件和Jar文件清单的
信息,请参阅
Jar工具参考页面和Java Tutorial的Jar
跟踪。

Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name. In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point. See the Jar tool reference page and the Jar trail of the Java Tutorial for information about working with Jar files and Jar-file manifests.

使用此选项时,JAR文件
是所有用户类的来源,
并忽略其他用户类路径设置。

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.




  • java命令行用法

  • manifest spec

    • java command line usage
    • manifest spec
    • (注意:大多数ZIP应用程序都可以检查JAR文件;我可能忽略了批处理脚本中目录名中的处理空间; Scala代码运行器版本2.7.4.final。)

      为了完整性,一个等效的bash脚本:

      For completeness, an equivalent bash script:

      #!/bin/bash
      
      if [ ! $SCALA_HOME ]
      then
          echo ERROR: set a SCALA_HOME environment variable
          exit
      fi
      
      if [ ! -f scala-library.jar ]
      then
          cp $SCALA_HOME/lib/scala-library.jar .
      fi
      
      scalac -sourcepath src -d bin src/foo/HelloWorld.scala
      
      cd bin
      jar -cfm ../hellow.jar ../MANIFEST.MF *
      cd ..
      
      java -jar hellow.jar
      

      这篇关于从Scala文件创建jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆