gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:40:14 24 4
gpt4 key购买 nike

我试图制作一个简单的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

最佳答案

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

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

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

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

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

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

代码与build.sbt一起使用

关于scala - 如何修复Intellij的Scala应用程序上的“错误:找不到或加载主类”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58807246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com