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

查看:17
本文介绍了从 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:scalain
X:scalauild.bat
X:scalaMANIFEST.MF
X:scalasrc
X:scalasrcfoo
X:scalasrcfooHelloWorld.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:

@ECHO OFF

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

CALL scalac -sourcepath src -d bin srcfooHelloWorld.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 文件必须包含一行表单主类:类名.这里,classname 标识具有的类public static void main(String[]args) 方法作为您的应用程序的起点.见Jar 工具参考页面和 JarJava 教程的踪迹关于使用 Jar 的信息文件和 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 命令行使用
  • 清单规范
  • (注意:大多数 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天全站免登陆