title: NoClassDefFoundError: org/apache/spark/sql/SparkSession$ while running spark source code locally
body: <p>I have cloned spark project in my local machine and I build spark project using below command, Build was successful.</p>
  
  <pre><code>mvn -DskipTests clean package
  </code></pre>
  
  <p>I imported spark project as a maven project in IntelliJ idea.
  I set Scala 2.12.10 as global library in my project</p>
  
  <p>But When I try to run any of the sample program in examples module I am getting below error.
  I guess this has something to do with Scala compilation , Please help me understand what is going on here ?</p>
  
  <pre><code>Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/spark/sql/SparkSession$
      at org.apache.spark.examples.GroupByTest$.main(GroupByTest.scala:30)
      at org.apache.spark.examples.GroupByTest.main(GroupByTest.scala)
  Caused by: java.lang.ClassNotFoundException: org.apache.spark.sql.SparkSession$
      at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
      ... 2 more
  </code></pre>
  
  <p>sharing the example spark code i am running </p>
  
  <pre><code>/*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *    http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  // scalastyle:off println
  package org.apache.spark.examples
  
  import java.util.Random
  
  import org.apache.spark.sql.SparkSession
  
  /**
   * Usage: GroupByTest [numMappers] [numKVPairs] [KeySize] [numReducers]
   */
  object GroupByTest {
    def main(args: Array[String]): Unit = {
      val spark = SparkSession
        .builder
        .appName("GroupBy Test")
        .getOrCreate()
  
      val numMappers = if (args.length &gt; 0) args(0).toInt else 2
      val numKVPairs = if (args.length &gt; 1) args(1).toInt else 1000
      val valSize = if (args.length &gt; 2) args(2).toInt else 1000
      val numReducers = if (args.length &gt; 3) args(3).toInt else numMappers
  
      val pairs1 = spark.sparkContext.parallelize(0 until numMappers, numMappers).flatMap { p =&gt;
        val ranGen = new Random
        val arr1 = new Array[(Int, Array[Byte])](numKVPairs)
        for (i &lt;- 0 until numKVPairs) {
          val byteArr = new Array[Byte](valSize)
          ranGen.nextBytes(byteArr)
          arr1(i) = (ranGen.nextInt(Int.MaxValue), byteArr)
        }
        arr1
      }.cache()
      // Enforce that everything has been calculated and in cache
      pairs1.count()
  
      println(pairs1.groupByKey(numReducers).count())
  
      spark.stop()
    }
  }
  // scalastyle:on println
  </code></pre>