Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running scala apps with java -jar

Tags:

java

jar

scala

I got some problems with the java. Check it out.

sebastian@sebastian-desktop:~/scaaaaaaaaala$ java -cp /home/sebastian/.m2/repository/org/scala-lang/scala-library/2.8.0.RC3/scala-library-2.8.0.RC3.jar:target/scaaaaaaaaala-1.0.jar scaaalaaa.App

Hello World!

That's cool, right, but how bout this:

sebastian@sebastian-desktop:~/scaaaaaaaaala$ java -cp /home/sebastian/.m2/repository/org/scala-lang/scala-library/2.8.0.RC3/scala-library-2.8.0.RC3.jar -jar target/scaaaaaaaaala-1.0.jar

Exception in thread "main" java.lang.NoClassDefFoundError: scala/Application
 at java.lang.ClassLoader.defineClass1(Native Method)
 at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
 at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
 at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
 at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
 at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
 at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
 at scaaalaaa.App.main(App.scala)
Caused by: java.lang.ClassNotFoundException: scala.Application
 at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
 ... 13 more

Wat the heck? Any idea why the first works and not the 2nd? How do I -jar my scala??

My thanks in advance, brother.

like image 532
paintcan Avatar asked May 28 '10 15:05

paintcan


People also ask

Can I run Scala with Java?

To run scala programs with the java command, you need to include the scala library as a runtime dependency of your application. I'm including the same Compile. jar from the java example as a compile time dependency, to show how everything behaves. The scala runtime is just a jar file (scala-library.

Can Java use Scala libraries?

Scala is designed to work smoothly with Java and Java libraries like JMSL. This is clearly evident in how Scala picks up the same CLASSPATH environment variable Java uses, so if the Java library you wish to use is already there in the CLASSPATH, then you're good to go.

Can we convert Scala code to Java?

Decompile Scala code to Java In the Project tool window, right-click a Scala library class that you want to decompile. From the context menu, select Decompile Scala to Java. IntelliJ IDEA converts code to Java and opens the converted file in the editor.


2 Answers

If you define -jar the -classpath is ignored:

Java manual:

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

You can define the classpath dependencies in the Manifest metadata.

The easiest way to start your app is using the scala scripts:

scala -classpath target/scaaaaaaaaala-1.0.jar scaaalaaa.App Hello World!
like image 104
Thomas Jung Avatar answered Oct 18 '22 22:10

Thomas Jung


Simply running

scala scaaaaaaaaala-1.0.jar 

works for me with 2.11.6

like image 6
hgrey Avatar answered Oct 18 '22 23:10

hgrey