1

I have the following hello.scala file:

object hello {
    def main(args: Array[String]): Unit = {
        println("Hello World!")
    }
}

After using scalac to compile it, I get back hello.class and hello$.class.

How to execute it using java command? What should I put where ??? are?

$ java ??? hello
0

4 Answers 4

4

You are looking for

java -cp /wherever/my/scala/lives/lib/scala-library.jar hello

or you can just

scala hello
Sign up to request clarification or add additional context in comments.

3 Comments

You'll also need to add the current directory to the classpath, so that should be java -cp .:/wherever/scala-library.jar hello - the current dir is only used by default if classpath is not specified.
Where can I get the scala-library.jar??
@user3416134 - It's in the lib directory of any Scala distribution from scala-lang.org and ought to be in the same directory in every Linux Scala package.
3

Use the scala command to run it. i.e. scala hello

Comments

2

I think it might be the time to introduce the user yet another tool that would ease developing Scala applications - compiling the sources, building a single-jar artifact with all the dependencies and such. I don't think the single object hello application is the last word on what the OP develop in Scala and ultimately the tool's required.

With that said, I'd strongly suggest a project build tool and sbt might be a good fit.

Download the sbt launcher from the sbt project's web site and run sbt in the directory where the hello object sits in.

To run the hello object, execute sbt and let it execute other commands - of which run is the command to run the hello object application.

jacek:~/sandbox/so/hello-object
$ sbt
[info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
[info] Updating {file:/Users/jacek/.sbt/0.13/plugins/}global-plugins...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Set current project to hello-object (in build file:/Users/jacek/sandbox/so/hello-object/)
[hello-object]> run
[info] Updating {file:/Users/jacek/sandbox/so/hello-object/}hello-object...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/jacek/sandbox/so/hello-object/target/scala-2.10/classes...
[info] Running hello
Hello World!
[success] Total time: 12 s, completed Mar 16, 2014 2:32:39 PM

If you however need to know what exactly the command line was to run your hello object, execute last or last run to be explicit and have the parts been printed out for you.

[hello-object]> last
[debug]
[debug] Initial source changes:
[debug]     removed:Set()
[debug]     added: Set()
[debug]     modified: Set()
[debug] Removed products: Set()
[debug] External API changes: API Changes: Set()
[debug] Modified binary dependencies: Set()
[debug] Initial directly invalidated sources: Set()
[debug]
[debug] Sources indirectly invalidated by:
[debug]     product: Set()
[debug]     binary dep: Set()
[debug]     external source: Set()
[debug] All initially invalidated sources: Set()
[debug] Copy resource mappings:
[debug]
[info] Running hello
[debug] Waiting for threads to exit or System.exit to be called.
[debug]   Classpath:
[debug]     /Users/jacek/sandbox/so/hello-object/target/scala-2.10/classes
[debug]     /Users/jacek/.sbt/boot/scala-2.10.3/lib/scala-library.jar
[debug] Waiting for thread run-main-3 to terminate.
[debug]     Thread run-main-3 exited.
[debug] Interrupting remaining threads (should be all daemons).
[debug] Sandboxed run complete..
[debug] Exited with code 0
[success] Total time: 0 s, completed Mar 16, 2014 2:41:57 PM
[debug] > shell

The Classpath part after Running hello is what you need - the classpath to run your hello object.

jacek:~/sandbox/so/hello-object
$ java -cp /Users/jacek/sandbox/so/hello-object/target/scala-2.10/classes:/Users/jacek/.sbt/boot/scala-2.10.3/lib/scala-library.jar hello
Hello World!

Once you're satisfied with the result of your Scala development and the application appears ready for production deployment, execute package to create a jar file for other projects to use it (even Java ones).

[hello-object]> package
[info] Packaging /Users/jacek/sandbox/so/hello-object/target/scala-2.10/hello-object_2.10-0.1-SNAPSHOT.jar ...
[info] Done packaging.
[success] Total time: 1 s, completed Mar 16, 2014 2:34:34 PM

You may want to publish it to Ivy2 (default) or Maven repositories with publishLocal so the other Scala/Java/JRuby/Groovy projects could use it.

[hello-object]> publishLocal
[info] Packaging /Users/jacek/sandbox/so/hello-object/target/scala-2.10/hello-object_2.10-0.1-SNAPSHOT-sources.jar ...
[info] Done packaging.
[info] Wrote /Users/jacek/sandbox/so/hello-object/target/scala-2.10/hello-object_2.10-0.1-SNAPSHOT.pom
[info] :: delivering :: default#hello-object_2.10;0.1-SNAPSHOT :: 0.1-SNAPSHOT :: integration :: Sun Mar 16 14:35:57 CET 2014
[info]  delivering ivy file to /Users/jacek/sandbox/so/hello-object/target/scala-2.10/ivy-0.1-SNAPSHOT.xml
[info] Main Scala API documentation to /Users/jacek/sandbox/so/hello-object/target/scala-2.10/api...
model contains 2 documentable templates
[info] Main Scala API documentation successful.
[info] Packaging /Users/jacek/sandbox/so/hello-object/target/scala-2.10/hello-object_2.10-0.1-SNAPSHOT-javadoc.jar ...
[info] Done packaging.
[info]  published hello-object_2.10 to /Users/jacek/.ivy2/local/default/hello-object_2.10/0.1-SNAPSHOT/poms/hello-object_2.10.pom
[info]  published hello-object_2.10 to /Users/jacek/.ivy2/local/default/hello-object_2.10/0.1-SNAPSHOT/jars/hello-object_2.10.jar
[info]  published hello-object_2.10 to /Users/jacek/.ivy2/local/default/hello-object_2.10/0.1-SNAPSHOT/srcs/hello-object_2.10-sources.jar
[info]  published hello-object_2.10 to /Users/jacek/.ivy2/local/default/hello-object_2.10/0.1-SNAPSHOT/docs/hello-object_2.10-javadoc.jar
[info]  published ivy to /Users/jacek/.ivy2/local/default/hello-object_2.10/0.1-SNAPSHOT/ivys/ivy.xml
[success] Total time: 4 s, completed Mar 16, 2014 2:36:01 PM

And the last but not least - to have a single jar with all the dependencies bundled use the sbt-assembly plugin.

Comments

1

Yet another approach, call the file for instancehello.sh, set it to executable,

chmod +x hello.sh

Note the header of the file,

#!/bin/sh
exec scala -savecompiled "$0" "$@"
!#

object hello {
    def main(args: Array[String]): Unit = {
        println("Hello World!")
    }
}

Run it simply with

./hello.sh

The switch -savecompiled creates a JAR file on the first execution, following executions startup will be faster since no (re)compilation of the scripted code is required.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.