My Program - HelloWorld.scala
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
jar cvfe HelloWorld.jar HelloWorld HelloWorld*.class
After jar file is created i tried to get the output from jar file
java -jar HelloWorld.jar
but i got the error
Exception in thread "main" java.lang.NoClassDefFoundError: scala/Predef$
at HelloWorld$.main(HelloWorld.scala:12)
at HelloWorld.main(HelloWorld.scala)
Caused by: java.lang.ClassNotFoundException: scala.Predef$
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
You have to include the scala runtime and libraries into the java classpath, you can certainly do that from commandline, but I suggest you to use more comfortable tools, see for example the tutorial at www.scala-lang.org
Add line
Class-Path: /home/dmitin/.m2/repository/org/scala-lang/scala-library/2.12.3/scala-library-2.12.3.jar
(i.e. your location of scala-library) to HelloWorld.jar/META-INF/MANIFEST.MF.
Then
java -jar HelloWorld.jar Dmytro
produces
Hello, Dmytro!
Related
I'm trying to compile a project with Scala and Java sources from the command line, using the following two commands:
scalac -d . *.scala *.java
javac -d . -cp /usr/local/Cellar/scala/2.11.6/libexec/lib/scala-library.jar:. *.java
The compile goes fine, however when I try to run the code I get the following exception.
Exception in thread "main" java.lang.NoClassDefFoundError: scala/collection/Seq
at Agent.main(Agent.java:62)
Caused by: java.lang.ClassNotFoundException: scala.collection.Seq
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
For what it's worth, compiling and running through IntelliJ works fine. Any ideas on what's happening?
I am trying to create an executable Jar from my Scala project. Therefore, I followed a tutorial. It suggested creating a Java main class within eclipse, which calls the Scala entry point. This works fine when executing within eclipse. After adding this class I was able to export an executable jar. However, it wont work with
java -jar myjar.jar
I made sure to activate "Package required library into jar" when exporting. My Java main class looks like this (where Driver is also located in the package core)
package core;
public class Main {
public static void main(String[] args) {
Driver.main(args);
}
}
And when executing the exported char the follwing error is thrown, which I can debug:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.NoClassDefFoundError: scala/collection/Seq
at core.Driver$.main(Driver.scala:14)
at core.Driver.main(Driver.scala)
at core.Main.main(Main.java:5)
... 5 more
Caused by: java.lang.ClassNotFoundException: scala.collection.Seq
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
You could use the One-jar application to guarantee all the necessary jars are in the jarfile, I suspect the scala-lang jars are not in it or in the classpath of the command line.
Maven: https://code.google.com/p/onejar-maven-plugin/
SBT: https://github.com/sbt/sbt-onejar
If you aren't using either maven or SBT I would suggest switching to them as well as they are the main supported build systems in Scala
I am trying to package a simple executable Jar written in Scala, through Eclipse's Export function.
When attempting to execute the Jar from cmd java -jar test2.jar
I get the following error. Any ideas? Thank you.
Caused by: java.lang.NoClassDefFoundError: scala/Predef$
at Parser.Test(Parser.scala:5)
at Main.main(Main.java:12)
... 5 more
Caused by: java.lang.ClassNotFoundException: scala.Predef$
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 7 more
You just need to add the scala library jar to the classpath.
You can do it like this (assuming that the "scala-library.jar" jar is in the current directory) :
java -cp scala-library.jar -jar test2.jar
The documentation for tha java comamnd can be found here: http://docs.oracle.com/javase/6/docs/technotes/tools/windows/java.html
for me i was using jdk 11 and scala library container 2.12.3 and when i changed to jdk 8 it works fine
it is because of jdk11 scala version support
JDK Compatibility
right click on the project , Build path ,configure Build path , edit your jdk to less than 11
You can also package scala library together with your project. See this post
I did removed "Scala Library container" from project.
Next - add needed scala's library (scala-library.jar) to Java BuildPath (in Properties for ) as "Add External JARs..." in my library (not as reference to scala lib). And innclude them all to "Order and Export"
Finally - do Export for project with option "Package required libraries into generate JAR"
and this works fine :)
When I package my scala code into a jar using Eclipse, I am unable to run said jar file. I've got:
object Hello extends App{
println("Hello World!")
}
and:
public class Runner {
public static void main(String[] args) {
Hello.main(args);
}
}
I use eclipse to package this into a runnable jar called Hello.jar. java -jar Hello.jar then gives:
Exception in thread "main" java.lang.NoClassDefFoundError: scala/App
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at Hello.main(Hello.scala)
at Runner.main(Runner.java:4)
Caused by: java.lang.ClassNotFoundException: scala.App
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
... 13 more
Your application has a dependency on the Scala runtime libraries. These need to be available when the app is run, and currently they are not.
There are two ways to fix this:
Specify the classpath when you launch the JAR - convenient as a one-off, but clunky. Run java -jar /path/to/yourapp.jar from the command line, and add a -cp parameter which includes the path to the Scala library JAR.
Package up the Scala library JAR inside your own JAR, and modify the Manifest to reference this JAR (by adding the Class-Path attribute - see here). This requires more work with your build process, but then means that the app can be launched by simply double-clicking on the resulting JAR.
That's because eclipse don't ship scala-library with your code. Even more, you should make sure that all dependencies are in manifest file for executable jar. Here's a similar question from stackoverflow:
running scala apps with java -jar
I'm getting a new error:
Starting up
Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at org.lwjgl.Sys$1.run(Sys.java:75)
at java.security.AccessController.doPrivileged(Native Method)
at org.lwjgl.Sys.doLoadLibrary(Sys.java:68)
at org.lwjgl.Sys.loadLibrary(Sys.java:84)
at org.lwjgl.Sys.<clinit>(Sys.java:101)
at org.lwjgl.opengl.Display.<clinit>(Display.java:128)
at org.newdawn.slick.AppGameContainer$1.run(AppGameContainer.java:39)
at java.security.AccessController.doPrivileged(Native Method)
at org.newdawn.slick.AppGameContainer.<clinit>(AppGameContainer.java:36)
at GameStarter$.main(SnakeGame.scala:43)
at GameStarter.main(SnakeGame.scala)
I have imported the lwjgl.jar libary.
This is not a problem with Scala. lwjgl consists of a part written in Java and another part that is native. The native part should be found in form of a dll (Windows) or so (*nix) file. You should put this path in the java.library.path property.
See the faq: http://lwjgl.org/faq.php#faq1