简体   繁体   中英

Running Scala Maven Project

I am a scala beginner, and was running a starter project on Maven and using IntelliJ as IDE.

This is the link to project on github which I am using Github project and I compiled the project against OpenJDK8.

The HelloJava class runs successfully, however, when I try running the HelloScala class I come across the following error:

java -cp scala-maven-example-1.0.0-SNAPSHOT.jar com.jesperdj.example.HelloScala

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: scala/Function0
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)

Caused by: java.lang.ClassNotFoundException: scala.Function0
   at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
   at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
   at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more

Scala has its own runtime libraries above JVM. A compiled Scala .class has import ed some classes from Scala runtime libraries. When you try to run a Scala .class file, you need to append the Scala runtime to the classpath .

If you are running inside IntelliJ IDEA, the Scala Plugin will automatically do this, but when you run java from command line, you should do this yourself.

If you are using Maven, then you can add a <plugin> . From Scala Docs -> Scala with Maven -> Creating a Jar :

 <build> ... <plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.your-package.MainClass</mainClass> </manifest> </archive> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> 

After adding this, mvn package will also create [artifactId]-[version]-jar-with-dependencies.jar under target. Note: this will also copy the Scala library into your Jar . This is normal. Be careful that your dependencies use the same version of Scala, or you will quickly end up with a massive Jar.

To run your programs on Intellij you need to install scala sdk along with JDK.

Once you have scala SDK in your Intellij classpath you are good to go with scala coding.

Tick the checkbox 'include dependencies with "Provided" scope' under: 'Run\\Debug Configurations'. (Helps if yours is a maven project and you added the 'scala-library' dependency scope as 'Provided') 'Run\\Debug Configurations'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM