【问题标题】:Error: Could not find or load main class in scala错误:无法在 scala 中找到或加载主类
【发布时间】:2017-02-20 21:55:37
【问题描述】:

在为 scala 安装 eclipse scala 插件和 eclipse maven 插件之后。

我是 scala 的新手,所以在测试了一个 scala hello world 项目后,我试图确保环境正常工作。它按预期工作。

但我在尝试执行从公司存储库中签出的项目时遇到了困难。无论我做什么(清理、构建、通过mave 等进行全新安装),我都会收到“错误:无法找到或加载主类com.company.team.spark.sqlutil.testQuery”同时尝试在项目中运行一个小的 hello world 程序。我的直觉是,由于 pomisse,eclipse 无法为项目创建类文件,但即使经过几次尝试,我也无法确定它。请帮我解决这个问题

版本:Eclipse Luna 版本 (4.4.0) 内部版本号:20140612-0600

斯卡拉 - 2.10.6

Scalacode - testQuery.scala

package com.company.team.spark.sqlutil

object testQuery {
  def main(args: Array[String]): Unit = {
   print ("Hello")
  }
}

下面是我使用的POM。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company.team.spark</groupId>
    <artifactId>HomeSpark</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>HomeSpark</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <lib.dir>${project.basedir}\lib\</lib.dir>
    </properties>

    <dependencies>
    <!--    <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-core</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>system</scope>
            <systemPath>${lib.dir}junit-3.8.1.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.10</artifactId>
            <version>2.1.0</version>
            <scope>system</scope>
            <systemPath>${lib.dir}spark-core_2.10-2.1.0.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_2.10</artifactId>
            <version>2.1.0</version>
            <scope>system</scope>
            <systemPath>${lib.dir}spark-sql_2.10-2.1.0.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.databricks</groupId>
            <artifactId>spark-csv_2.10</artifactId>
            <version>1.5.0</version>
            <scope>system</scope>
            <systemPath>${lib.dir}spark-csv_2.10-1.5.0.jar</systemPath>
        </dependency> -->

  <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core_2.10 -->
<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-core_2.10</artifactId>
    <version>2.1.0</version>
</dependency>

   <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql_2.10 --> 
<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-sql_2.10</artifactId>
    <version>2.1.0</version>
</dependency>    

<!-- https://mvnrepository.com/artifact/com.databricks/spark-csv_2.10 -->
<dependency>
    <groupId>com.databricks</groupId>
    <artifactId>spark-csv_2.10</artifactId>
    <version>1.5.0</version>
</dependency>

<dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.9.2</version>
        </dependency>

  </dependencies>



<build>

        <sourceDirectory>${project.basedir}/src/main/scala</sourceDirectory>

        <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>

        <plugins><plugin>

    <groupId>net.alchim31.maven</groupId>
    <artifactId>scala-maven-plugin</artifactId>
    <version>3.1.3</version>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>testCompile</goal>
            </goals>
        </execution>
    </executions>
</plugin></plugins>

</build>    
</project>

Link 转项目结构图

【问题讨论】:

  • 这是从 Eclipse 运行时吗?如果您安装了 maven,请从命令行尝试 mvn clean compile,它将为您下载依赖项并查看是否正常工作。
  • @prayagupd 我厌倦了通过 eclipse [ run as > maven build ] 进行干净编译,它下载了依赖项。但是在尝试通过 /src/test/scala 运行 hello world 时仍然遇到相同的错误。代码就像 object testQuery { def main(args: Array[String]): Unit = { print ("Hello") } } 我很抱歉,忽略了这是一个测试类的部分。但是当我将它移到 src/main/scala 文件夹并单独删除 : Unit = " 部分时,您的建议确实有效。谢谢!
  • @prayagupd 有什么建议可以让他的测试课正常工作吗?

标签: scala maven classnotfoundexception


【解决方案1】:

根据 scala-maven-plugin 的要求使用编译安装。您可能正在使用全新安装,它正在从 /bin 中删除生成的 .class 文件,eclipse 无法找到或加载主类。

【讨论】:

    【解决方案2】:

    在选择 scala IDE 而不是与 scala IDE 插件集成的 eclipse 后,我能够解决这些问题。

    还将 pom.xml 更改为以下内容:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.company.fuison</groupId>
      <artifactId>SomeCloud</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>${project.artifactId}</name>
      <description>My wonderfull scala app</description>
      <inceptionYear>2015</inceptionYear>
      <licenses>
        <license>
          <name>My License</name>
          <url>http://....</url>
          <distribution>repo</distribution>
        </license>
      </licenses>
    
      <properties>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
        <encoding>UTF-8</encoding>
        <scala.version>2.11.5</scala.version>
        <scala.compat.version>2.11</scala.compat.version>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>org.scala-lang</groupId>
          <artifactId>scala-library</artifactId>
          <version>${scala.version}</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core_2.11 -->
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.11</artifactId>
        <version>2.0.0</version>
    </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core_2.11 -->
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-sql_2.11</artifactId>
        <version>2.0.0</version>
    </dependency>
    
    
        <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>1.9.2</version>
        </dependency>
    
    <!-- https://mvnrepository.com/artifact/com.databricks/spark-csv_2.11 -->
    <dependency>
        <groupId>com.databricks</groupId>
        <artifactId>spark-csv_2.11</artifactId>
        <version>1.5.0</version>
    </dependency>
        <!-- Test -->
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.specs2</groupId>
          <artifactId>specs2-core_${scala.compat.version}</artifactId>
          <version>2.4.16</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.scalatest</groupId>
          <artifactId>scalatest_${scala.compat.version}</artifactId>
          <version>2.2.4</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.specs2</groupId>
          <artifactId>specs2-junit_${scala.compat.version}</artifactId>
          <version>2.4.16</version>
          <scope>test</scope>
        </dependency>
    
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api-scala_2.11</artifactId>
            <version>2.8.1</version>
        </dependency>
    
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.1</version>
        </dependency>
    
    
    
        <!-- https://mvnrepository.com/artifact/org.scala-lang/scala-library 
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.12.1</version>
        </dependency>
        -->
        <!-- https://mvnrepository.com/artifact/com.typesafe.scala-logging/scala-logging_2.11 -->
        <dependency>
            <groupId>com.typesafe.scala-logging</groupId>
            <artifactId>scala-logging_2.11</artifactId>
            <version>3.5.0</version>
        </dependency>
    
    
      </dependencies>
    
      <build>
        <resources>
                <resource>
                    <directory>${project.basedir}/config/log4j</directory>
                </resource>
            </resources>
    
        <sourceDirectory>src/main/scala</sourceDirectory>
        <testSourceDirectory>src/test/scala</testSourceDirectory>
        <plugins>
          <plugin>
            <!-- see http://davidb.github.com/scala-maven-plugin -->
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
              <execution>
                <goals>
                  <goal>compile</goal>
                  <goal>testCompile</goal>
                </goals>
                <configuration>
                  <args>
    
                    <arg>-dependencyfile</arg>
                    <arg>${project.build.directory}/.scala_dependencies</arg>
                  </args>
                </configuration>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>
              <useFile>false</useFile>
              <disableXmlReport>true</disableXmlReport>
              <!-- If you have classpath issue like NoDefClassError,... -->
              <!-- useManifestOnlyJar>false</useManifestOnlyJar -->
              <includes>
                <include>**/*Test.*</include>
                <include>**/*Suite.*</include>
              </includes>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
    

    【讨论】:

      猜你喜欢
      • 2018-03-30
      • 1970-01-01
      • 2018-07-21
      • 2018-10-19
      • 2019-07-16
      • 1970-01-01
      • 2016-06-12
      • 2016-03-16
      • 2016-01-03
      相关资源
      最近更新 更多