tags:

views:

92

answers:

2

A program of mine (written in Scala 2.8) works fine when launched by means of NetBeans IDE. But when I try to run it from outside, with "java- jar", it says "Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject...". Putting all the libraries, incl. Scala runtime inside the same dir as the jar to be run doesn't help. If i try to run the jar with scala itself, it complains that it can't decode it as utf-8 (it expects a scala source rather than a jar, I suppose). So how do I run a Scala application at all?

UPDATE: For those who come here later having the same question I'd recommend to read comments under barjak's answer (including those latest ones hidden), the answer is there. VonC also gives some interesting links on the subject.

+2  A: 

Are you using scala-library.jar as described in Adventures with Scala blog post?

java -classpath scala-library.jar:. YourClass

or:

java -classpath scala-library.jar:yourApp.jar YourClass

Where YourClass is was your scalac compiled Scala code.

You will find the same scala-library.jar used in the SO question "Creating a jar file from a Scala file" (or in the blog post "the not so elegant way of creating an executable jar from scala code").

VonC
The main class (object) of the app is "Main" with "main" function. The project is "Foobar". The lines you've given works with neither "Main", nor "main", nor "Foobar" nor "foobar" for "YourClass".
Ivan
I've managed. The solution was to list all the used libs in classpath. But can't I just point to a dirctory instead (containing more jars than needed, incl. source and doc jars)?
Ivan
+3  A: 

The -jar and -classpath of the java command are mutually exclusive : you can't do java -jar YourScalaProg.jar -classpath scala-library.jar

If you want to run your application with java -jar, then the full classpath must be specified in the Class-Path section of the jar's manifest.

You can run your application using only -classpath, like that : java -classpath YourScalaProg.jar:scala-library.jar your.package.MainClass.

barjak
Says "...Java.lang.ClassNotFoundException: Main...". My main class (object) is Main.
Ivan
Did you use the canonical name of the class ? The canonical name is the name of the class with the package name prepended : `org.blah.Main`. Also, depending on your OS, you must use either `:` or `;` as the classpath separator.
barjak
Seems that my i686 Arch Linux uses ':' (as using ';' leads to java help to be printed). using the canonical main class name didn't help.
Ivan
what is the exact command line you are using, and what exact error message do you get ?
barjak
@barjak, > java -classpath Foobar.jar:scala-library.jar foobar.Main
Ivan
I've managed. Thank you for pointing my attention on the fact the error message has changed. The solution was to list all the used libs in classpath. But can't I just point to a dirctory instead (containing more jars than needed, incl. source and doc jars)?
Ivan
@barjak, Be so kind to edit your answer to reflect my solution and your advice to use full class name, and I will explicitly accept your answer, so the question will be marked as solved and you'll gain points.
Ivan
yes, from JRE 6 (I think) you can use an asterisk in the classpath. Example : `java -classpath lib/*`
barjak
to be more precise, if I write -classparh *, or ./*, or copy my main jar to lib ans use -classparh lib/* - I get no good. The only correct syntax seems to be -classparh MyApp.jar:lib/*
Ivan