How to address Scala error in eclipse 2019? - scala

I am trying to do a basic scala HelloWorld in Eclipse 2019 and I am getting an error.
The following is my code and the error it is producing. Can someone please help me address this error in eclipse? Thanks
package hello
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
Error:
Exception in thread "main" java.lang.NoClassDefFoundError:
scala/Predef$
at HelloWorld/hello.HelloWorld$.main(HelloWorld.scala:5)
at HelloWorld/hello.HelloWorld.main(HelloWorld.scala)
Caused by: java.lang.ClassNotFoundException: scala.Predef$
at
java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 2 more
Scala library is already in source path

There are two required places to set your class-path:
Build/compilation time class-path: (i) Right-click on your project, (ii) Buildpath > Configure BuildPath, (iii) Add Library (or jar), (iv) Select the Scala Library. This one you already have as supported by your screenshot.
Run-time compilation class-path: This needs to be explicitly set in the Run-time configuration to also include the scala library: (i) Run configurations..., (ii) Classpath, (iii) Add Jar and use the scala-library jar. For this option, I have not tested whether User vs Bootstrap matters. Furthermore, I was unable to use the Add Library here, only Add jar results in a functioning run within Eclipse.
The second option is the likely cause of the error you are getting.

You need to add Scala library to your classpath.
From Eclipse:
Right-click on your project
Configure Buildpath
Add Library
Select the Scala Library

Related

Error occurred during initialization of boot layer java.lang.module.FindException executing Selenium tests using TestNG and Java 12 through Eclipse

ErrorOccuredDuringInitializationofbootlayer I keep getting this error when I run
my test:
Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for
C:\Users\Bonfire.eclipse\org.eclipse.platform_4.12.0_867647348_win32_win32_x86_64\plugins\com.beust.jcommander_1.72.0.jar
Caused by: java.lang.IllegalArgumentException:
com.beust.jcommander.1.72.0: Invalid module name: '1' is not a Java
identifier
How can I fix it?
Add TestNG Library to classpath in buildpath ( not to module path). It will work fine.
To avoid this error, do not add TestNG library in the project or src folder. Try adding it into the package. To do so , follow the below mentioned procedure.
Right click on the package, click Build Path->Configure Build Path, go to Libraries tab, select Classpath and click on Add Library to select TestNG.
This error message...
Error occurred during initialization of boot layer java.lang.module.FindException:
Unable to derive module descriptor for C:\Users\Bonfire.eclipse\org.eclipse.platform_4.12.0_867647348_win32_win32_x86_64\plugins\com.beust.jcommander_1.72.0.jar
Caused by: java.lang.IllegalArgumentException: com.beust.jcommander.1.72.0: Invalid module name: '1' is not a Java identifier
...implies that there was an issue with the java classpath.
As per the discussion Launch with java 11 fails: Error: Could not find or load main class with Caused by: java.lang.ClassNotFoundException: with there are some issues with some particular classpaths for java-11 and the launch fails if:
The classpath contains a folder with spaces
The classpath length is larger than 32767 characters.
Where as java-8 works fine.
Reference
Unable to import org.openqa.selenium.WebDriver using Selenium and Java 11
Outro
Eclipse plugin: TestNG testcase does not run with Jdk11
Need to add 1.8 version library
Remove TestNG from build path library and add again
Recheck - the issue should be resolved.
I got same issue and I solved it. When creating the project, don't create module-info.java. When the project was already created with the module-info.java, deleting this file didn't help me.
I just created new project without module-info.java, and all works good now.
Best option to resolve this:
Find all file and folder starting with . e.g. — .metadata / .setting and delete them.
Add all the JARS.
Add testNG Libraries.
Execute it.
Done.

Running Scala under Eclipse

I'm very new to Scala and I tried to run Scala project under Eclipse. I used sbt to create one, then ran sbteclipse to prepare it for Eclipse and imported it successfully. However when I try to run it I get
Error: Unable to initialize main class Main
Caused by: java.lang.NoClassDefFoundError: scala/Function0
error. Scala, sbt and Java are installed, because when I try to run the same project via console, using sbt, it works. What am I missing?
Thanks for any help!
It looks like the classpath of your Eclipse project is incomplete: it's missing the Scala library. Can you double check in Project Settings that the scala library is present?
If all you want to do is try a simple program, an simpler solution is to create a New Scala Project using the Eclipse wizard.

Why am I getting this NoClassDefFoundError from my code?

I have been trying to use a Jar file as a library in my code, and it compiles fine. However, at runtime, I keep getting the NoClassDefFoundError message. Why is this happening? I have included the Jar file in the compile path and the runtime path too.
Here is the error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.pdfbox.cos.COSDocument.(COSDocument.java:51)
at org.apache.pdfbox.pdmodel.PDDocument.(PDDocument.java:136)
at processing.PDFToJPG.main(PDFToJPG.java:58)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
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)
... 3 more
Here is my code:
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
public static void main(String[] args) {
try {
PDDocument doc = new PDDocument();
} catch (Exception e) {
e.printStackTrace();
}
}
I am using NetBeans IDE as well as Windows 10
This is my setting for compile classpath:
[
This is my setting for runtime classpath:
[
EDIT: Thank you for your help, it really worked. All i needed to do was to download the dependencies Jar file, not editing the classpath like what i have been trying to do
I think you need another jars besides the one you have already included. Try to add common-logging 1.4. Apparently, there is a dependency between pdfbox1.8.jar and this jar as stated on their site.
EDIT: There are more dependencies fontbox and jempbox to take in account as well.
EDIT2: I made a zip with all dependencies needed you can download it here.
I agree with Aurelien's post: it looks like you are missing Apache Commons Logging - and other runtime dependencies.
You might want to consider creating your project as a 'Maven' Project (And Netbeans supports Maven pretty well): and then adding 'pdfbox' as a 'dependency'; this should make life a lot easier for you - since Maven will fetch any other required dependencies.
You can get the 'Maven Coordinates' for the various PDFBox versions from here:
http://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox
If you want to build your final project into a single JAR containing all the deps; or to create a separate 'lib' directory of them: you will have to make some minor changes to the Maven project file ('pom.xml') to do this.
This Stackoverflow Post has an example of doing that.

Running tests in IntelliJ ClassNotFoundException

I tried many different run configs, but whatever I do I get this exception when running specs2 tests in IntelliJ for scala.
It always fails to find a class that ends with a $ sign. I checked - and there really is no such class file. There's AppControllerIT.class and lots of classes like AppControllerIT$innerFunctionOrclass.clas, but not AppControllerIT$.class
Any ideas?
Thanks!
com.haha.market.api.e2e.controllers.AppControllerIT$
java.lang.ClassNotFoundException: com.haha.market.api.e2e.controllers.AppControllerIT$
STACKTRACE
java.net.URLClassLoader.findClass(URLClassLoader.java:381)
java.lang.ClassLoader.loadClass(ClassLoader.java:424)
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
java.lang.ClassLoader.loadClass(ClassLoader.java:357)
org.specs2.reflect.Classes$$anonfun$loadClassEither$1.apply(Classes.scala:140)
org.specs2.reflect.Classes$$anonfun$loadClassEither$1.apply(Classes.scala:140)
org.specs2.control.ActionT$$anonfun$safe$1.apply(ActionT.scala:89)
org.specs2.control.ActionT$$anonfun$reader$1$$anonfun$apply$6.apply(ActionT.scala:80)
org.specs2.control.Status$.safe(Status.scala:100)
Classes with $ signs at the end are generate from compiled Scala objects. This means you may have an object defined similar to this:
package com.haha.market.api.e2e.controllers
object AppControllerIT {
}
From your error, it seems that an older compiled artifact or a library (?) is polluting your classpath. First, try cleaning up the project (mvn clean or sbt clean). Next, try to clean any libraries you have in your project inside IntelliJ. IntelliJ sometimes caches multiple versions of the same libraries which may cause confusion during runtime. To clean those up go to "File -> Project Structure" in IntelliJ and manually delete any duplicated libraries you may have.

Problem creating an executable jar from scala file

I'm tryign to export my project as a jar with IntelliJ 9.0. My project compiles and runs with no problem in Intellij, but when I write it to a .jar and open it, it will show an error.
My Main class is something like:
package Main
//Imports
object Main{
def main(args: Array[String]) {
println("Main: Hello, world!")
//do stuff
}
Now, in the artifacts window I created a .jar with the following:
Main Clas: Main.Main
Class Path: lib/javacsv lib/scala-compiler.jar lib/scala-library.jar lib/scalatest-1.0-test.jar lib/scalatest-1.0.jar lib/tools.jar lib/jtds-1.2.2.jar lib/flex-messaging-common.jar lib/flex-messaging-core.jar lib/spring.jar lib/mysql-connector-java-5.1.7-bin.jar lib/ojdbc14.jar lib/commons-logging.jar lib/postgresql-8.4-701.jdbc3.jar lib/log4j-1.2.15.jar lib/poi-3.6-20091214.jar lib/poi-ooxml-3.6-20091214.jar lib/dom4j-1.6.1.jar lib/poi-ooxml-schemas-3.6-20091214.jar lib/geronimo-stax-api_1.0_spec-1.0.jar lib/xmlbeans-2.3.0.jar lib/rt.jar lib/ifxjdbc.jar lib/db2jcc4.jar
I have double checked that all those classes are on the project and are the onnly classes on it. Notice that it inlcudes lib/scala-compiler.jar and lib/scala-library.jar.
Build the project: java -jar myScalaApp.jar and I get:
Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:675)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:316)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:288)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:374)
at Main.Main.main(Main.scala)
Caused by: java.lang.ClassNotFoundException: scala.ScalaObject
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:316)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:288)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:374)
... 13 more
I understand that it's not finding the scala classes but I made sure they are there. What else can be the problem and how could I fix it?
I'd say one of two things has gone wrong:
scala-library.jar is not in your jar; or
the class path at runtime doesn't include scala-library.jar.
I don't know how IntelliJ builds your jar: Does it unpack all your library jars and mung them together with your code into one big jar, or does it add the library jars as-is to your big jar and manipulate the classpath to get at the jars-within-jar?
The first point is easy enough to check: Either use jar -tvf yourJar.jar to list out the contents of your jar, or use an archive viewer to look into it graphically. Note that a .jar is basically a .zip, so you can rename the extension and then use a tool that can look into .zip files.
Just had an idea about the second part: Can you build a simple Java main class that prints out System.getProperty("java.class.path") ? A Java class should be able to run in that jar even if a Scala class isn't.
There are at least three ways to do this. (One applies to Netbeans but should work in Idea too)
Use jarjar as explained here
Use the idea on this page (not so elegant but the easiest) - essentially unpack the Scala directory from scala-library.jar and add this directory to your jar.
Use the idea on this page (for NB) - essentially add "scala-library.jar" to your project libraries.