如何修复Intellij的Scala应用程序上的“错误:找不到或加载主类”?

我试图制作一个简单的Scala应用程序,Page Rank,但是我无法运行它。

我从中获得代码的来源:https://github.com/abbas-taher/pagerank-example-spark2.0-deep-dive

我在Intellij中创建了一个新的scala项目(包com.scalaproj),并将代码从源代码添加到scala对象SparkPageRank中。正如教程中所述,我还添加了spark-2.1.0-bin-hadoop2.7的外部jar。

/*
 * 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 com.scalaproj

import org.apache.spark.sql.SparkSession

/**
 * Computes the PageRank of URLs from an input file. Input file should
 * be in format of:
 * URL         neighbor URL
 * URL         neighbor URL
 * URL         neighbor URL
 * ...
 * where URL and their neighbors are separated by space(s).
 *
 * This is an example implementation for learning how to use Spark. For more conventional use,* please refer to org.apache.spark.graphx.lib.PageRank
 *
 * Example Usage:
 * {{{
 * bin/run-example SparkPageRank data/mllib/pagerank_data.txt 10
 * }}}
 */
object SparkPageRank {

  def showWarning() {
    System.err.println(
      """WARN: This is a naive implementation of PageRank and is given as an example!
        |Please use the PageRank implementation found in org.apache.spark.graphx.lib.PageRank
        |for more conventional use.
      """.stripMargin)
  }

  def main(args: Array[String]) {
    if (args.length < 1) {
      System.err.println("Usage: SparkPageRank <file> <iter>")
      system.exit(1)
    }

    showWarning()

    val spark = SparkSession
      .builder
      .appName("SparkPageRank")
      .getOrCreate()

    val iters = if (args.length > 1) args(1).toInt else 10
    val lines = spark.read.textFile(args(0)).rdd
    val links = lines.map{ s =>
      val parts = s.split("\\s+")
      (parts(0),parts(1))
    }.distinct().groupByKey().cache()
    var ranks = links.mapValues(v => 1.0)

    for (i <- 1 to iters) {
      val contribs = links.join(ranks).values.flatMap{ case (urls,rank) =>
        val size = urls.size
        urls.map(url => (url,rank / size))
      }
      ranks = contribs.reduceByKey(_ + _).mapValues(0.15 + 0.85 * _)
    }

    val output = ranks.collect()
    output.foreach(tup => println(tup._1 + " has rank: " + tup._2 + "."))

    spark.stop()
  }
}
// scalastyle:on println

我没有错误地构建了项目,但是当我尝试运行它时,它说:

Error: Could not find or load main class com.scalaproj.SparkPageRank

我也在eclipse中尝试了此操作,同样的错误。有人可以告诉我该怎么做吗?

Here you can see the folder tree of my project

wangqian9889 回答:如何修复Intellij的Scala应用程序上的“错误:找不到或加载主类”?

我能够重现该错误。您只需在Intellij中将包含SparkPageRank对象的目录标记为Sources根目录。 (目录必须为蓝色)

右键单击目录->将目录标记为->源根目录

希望这项工作对您有帮助!

========讨论后更新======

由于此解决方案无法正常工作,因此您必须创建一个maven项目,并添加spark和scala依赖项。在github链接中,我不确定没有pom.xml或build.sbt的代码是否可以工作。

========更新11/13 =======

代码与build.sbt一起使用

本文链接:https://www.f2er.com/3121900.html

大家都在问