Why do i get a ClassNotFoundException on running a

2020-05-08 00:38发布

问题:

I'm unable to figure out what is wrong with this program? I'm using an older verison of scala (2.7) because its compatible with certain libraries i'm using

Here is a simple program i'm attempting to run.

The program runs fine using scalac and scala commands.

However on IntelliJ 14+ -> when i create a new project -> select the compiler (scala 2.7) and try to run the above program i get this error below

object SimpleClass {
  def main(args: Array[String]) {
    println("This is a simple Class")
  }
}

Error output.

Why does intelliJ throw the ClassNotFoundException? I've saved the program as SimpleClass.scala

/usr/lib/jvm/java-7-openjdk-i386/bin/java -Didea.launcher.port=7532 -Didea.launcher.bin.path=/home/tejesh/Downloads/idea-IC-141.1532.4/bin -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-7-openjdk-i386/jre/lib/javazic.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/management-agent.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/resources.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/rhino.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/charsets.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/jce.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/jsse.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/rt.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/compilefontconfig.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/ext/java-atk-wrapper.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/ext/icedtea-sound.jar:/usr/local/share/scala-2.7.3/lib/scala-swing.jar:/usr/local/share/scala-2.7.3/lib/scala-library.jar:/home/tejesh/Downloads/idea-IC-141.1532.4/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain SimpleClass
Exception in thread "main" java.lang.ClassNotFoundException: SimpleClass
    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)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:191)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)

I've added the folder containing the SimpleClass as under sources

This is the full program here

回答1:

Recent Scala for IntelliJ IDEA is based on sbt. Apparently Scala 2.7.3 is not supported any longer in this configuration. If you set up a project, IntelliJ will offer you (when you select "Download" for the SDK) no version older than Scala 2.8.0. If you manually select 2.7.3, the following happens when you try to compile:

Error:scalac: Error: org.jetbrains.jps.incremental.scala.remote.ServerException
Error compiling sbt component 'compiler-interface-2.7.3.final-52.0'
    at sbt.compiler.AnalyzingCompiler$$anonfun$compileSources$1$$anonfun$apply$2.apply(AnalyzingCompiler.scala:145)
    at sbt.compiler.AnalyzingCompiler$$anonfun$compileSources$1$$anonfun$apply$2.apply(AnalyzingCompiler.scala:142)
    at sbt.IO$.withTemporaryDirectory(IO.scala:285)
    at sbt.compiler.AnalyzingCompiler$$anonfun$compileSources$1.apply(AnalyzingCompiler.scala:142)
    at 
    ...

The next step is Scala 2.8.2, for which you get:

Error:scalac: Parameter '-nobootcp' is not recognised by Scalac.

It seems the oldest Scala that works with the current IntelliJ is 2.9.0. (You have to use Java 7 and not Java 8 for this!)

The solution would be to downgrade IntelliJ IDEA to an old version that still supports Scala 2.7.3 (perhaps IDEA 11?), or to simply set up the project with Scala 2.9.3, but compile instead manually (or using sbt from the terminal) against 2.7.3.

If you have the source to the library that requires 2.7.3, perhaps the best option is to try to compile the library with contemporary versions of Scala (at least 2.9.0).