IntelliJ throwing error "could not find or load main class" while running scala jar file in the IDE

Hi ,

I created a simple hello world scala project in IntelliJ. I used the ‘Build Artefact’ option to generate the jar file. when I run the jar file from the IDE I get “could not find or load main class” . Please suggest how to make it work in IntelliJ IDE.

Thanks in advance

Hi Sasanka,

It will be hard for me to explain without looking at your code. You can get valuable insights from this answer.

To help users writing Scala and Spark code on their local machines, we’re building an end to end guide to show how to build Scala and Spark projects in local machine and then deploy it to any production server including CloudxLab.

It will take us few days to make it live.

Hi Sasanka,

There could the following reasons for the error:

  1. The main method is not visible. Please make sure that the declaration of method looks like this:
    public static void main(String[] args){

    }
  2. The class in which the main method is defined is not visible. It should be a public class.
  3. Also, please define a package for your class. It is very simple. create a folder pkg in src folder and move your code into that folder. Then at the top of your classes, mention “package pkg”
  4. While creating jar set the entry point to the class which has the “main” method.
  5. Also, in certain cases, if you set the compiler to be a higher version than the jvm, you may get this error. The theory behind this is that the Java code compiled with the higher version of Java compiler would not run on the lower version of java for example code compiled with Java 8 will not run on Java virtual machine version 7.

I’m a beginner with Scala and Spark using IntelliJ IDE and I’m getting the same error. Please suggest exactly what I’m doing wrong here.

Do I need to add any dependencies?
I’m using default configurations and they are not showing any error. Still is there room for improvement?

Please help. Appreciate.

Did you install the scala plugin as outlined here:
https://www.jetbrains.com/help/idea/discover-intellij-idea-for-scala.html

Hi Abhinav,
Would you please provide me a link if you have done it.

Thanks,
Shambhu Nath