【问题标题】:Eclipse error when trying to run Java application - "Selection does not contain a main type" - But it does?尝试运行 Java 应用程序时出现 Eclipse 错误 - “选择不包含主要类型” - 但确实如此?
【发布时间】:2013-06-27 11:41:39
【问题描述】:

我创建了一个 Java 包 Matrix。当我尝试运行它时,我收到错误“选择不包含主要类型”。但是,正如我从 DriverMatrix 类中复制和粘贴的代码所示,有一个 main 方法声明。我试图重新启动 Eclipse,但仍然出现错误。我创建了名为 Matrix 的包,然后为每个类导入了 .java 文件。有人知道这里发生了什么吗?这是主要声明以及一些代码:

package Matrix;

import java.io.*;
import java.util.Scanner;

public class DriverMatrix 
{

static private IntegerArithmetics integerArithmetics = new IntegerArithmetics();
static private DoubleArithmetics doubleArithmetics = new DoubleArithmetics();

public static void main(String[] args) throws FileNotFoundException
{
    Scanner inFile = new Scanner (new FileReader("in.txt"));
    PrintWriter outFile = new PrintWriter("out.txt");

    Matrix<Integer,IntegerArithmetics> matrix1 = new Matrix<Integer,IntegerArithmetics>(integerArithmetics,2,3);
    Matrix<Integer,IntegerArithmetics> matrix2 = new Matrix<Integer,IntegerArithmetics>(integerArithmetics,2,3);
    Matrix<Integer,IntegerArithmetics> matrix3 = new Matrix<Integer,IntegerArithmetics>(integerArithmetics,2,3);
    Matrix<Integer,IntegerArithmetics> matrix4 = new Matrix<Integer,IntegerArithmetics>(integerArithmetics,2,3);
    Matrix<Integer,IntegerArithmetics> matrix5 = new Matrix<Integer,IntegerArithmetics>(integerArithmetics,3,2);
    Matrix<Integer,IntegerArithmetics> matrix6 = new Matrix<Integer,IntegerArithmetics>(integerArithmetics,2,2);

【问题讨论】:

  • 什么是完整的错误堆栈?它在您的代码中指的是哪一行?
  • 在 Eclipse 中,您可以尝试右键单击您的班级和 Run as -&gt; Java Application,但我自己没有 Eclipse 来尝试。
  • 它是否在源文件夹中的 Java 项目中?

标签: java eclipse import package main


【解决方案1】:

您的项目中似乎存在Matrix 类和包名冲突,这可能会导致eclipse 错误。尝试重命名其中一个,然后运行您的应用程序。

【讨论】:

    【解决方案2】:

    根据你的代码

    Matrix<Integer,IntegerArithmetics> matrix1 = new Matrix<Integer,IntegerArithmetics>(integerArithmetics,2,3);
    

    似乎你有一个包和类都被命名为矩阵。首先尝试更改此设置,因为命名冲突显然会导致错误。

    来源:Naming a package

    【讨论】:

    • 谢谢!我刚开始使用 Eclipse,一般来说对 Java 还是很陌生。我不知道包的命名约定。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    相关资源
    最近更新 更多