3

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.

Ivan
  • 63,011
  • 101
  • 250
  • 382
  • See http://stackoverflow.com/questions/809138/creating-a-jar-file-from-a-scala-file – Alexey Romanov Sep 12 '10 at 21:17
  • @alexey-romanov, I've started from there, it didn't help. I know nothing about manifests, I use NetBeans IDE (which itself uses Ant) and rely on it for building. – Ivan Sep 12 '10 at 21:43

2 Answers2

4

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
  • 10,842
  • 3
  • 33
  • 47
  • Says "...Java.lang.ClassNotFoundException: Main...". My main class (object) is Main. – Ivan Sep 12 '10 at 21:29
  • 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 Sep 13 '10 at 08:06
  • 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 Sep 13 '10 at 09:34
  • what is the exact command line you are using, and what exact error message do you get ? – barjak Sep 13 '10 at 11:23
  • @barjak, > java -classpath Foobar.jar:scala-library.jar foobar.Main – Ivan Sep 13 '10 at 11:49
  • 1
    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 Sep 13 '10 at 12:05
  • @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 Sep 13 '10 at 12:10
  • yes, from JRE 6 (I think) you can use an asterisk in the classpath. Example : `java -classpath lib/*` – barjak Sep 13 '10 at 12:12
  • 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 Sep 13 '10 at 12:35
3

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").

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 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 Sep 12 '10 at 21:24
  • 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 Sep 13 '10 at 12:06