eclipse 我无法运行 Java 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5291494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 15:58:10  来源:igfitidea点击:

I can't run java application

javaeclipse

提问by Mike

I'm not sure if I need to paste my code here, but when I go to run my code I right click on the class object and it usually says run as java application, but now all it says is run configurations.

我不确定是否需要在这里粘贴我的代码,但是当我去运行我的代码时,我右键单击类对象,它通常说作为 Java 应用程序运行,但现在它说的是运行配置。

I'm using Eclise.

我正在使用 Eclise。

Here is al of my code. I know it's a bracket placement issue

这是我的全部代码。我知道这是一个支架放置问题

import java.util.Calendar;

public class Date {

    private int month;
    private int day;
    private int year;

    public static void main(String[] args) {
    } 

    public Date(int theMonth, int theDay, int theYear) {
        month = checkMonth( theMonth );
        year = checkYear( theYear );
        day = checkDay( theDay );
        System.out.printf("Date object constructor for date %s\n", toString() );
    }

    private int checkYear(int testYear) {
        if ( testYear > 0 )
            return testYear;
        else {
            System.out.printf("Invalid year (%d) set to 1.\n", testYear );
            return 1;
        }
    }

    private int checkMonth( int testMonth ) {
        if ( testMonth > 0 && testMonth <= 12 )
            return testMonth;
        else {
            System.out.printf("Invalid month (%d) set to 1.\n", testMonth );
            return 1;
        }
    }


    private int checkDay( int testDay ) {
        int daysPerMonth[] = 
            { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

        if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
            return testDay;

        if ( month == 2 && testDay == 29 && ( year % 400 == 0 ||
            ( year % 4 == 0 && year % 100 != 0 ) ) )
            return testDay;

        System.out.printf( "Invalid day (%d) set to 1.\n", testDay );

        return 1;
    }

    public void nextDay() {
        int testDay = day + 1;
        if ( checkDay( testDay ) == testDay )
            day = testDay;
        else {
            day = 1;
            nextMonth();
        }
    }

    public void nextMonth() {
        if ( 12 == month )
            year++;
        month = month % 12 + 1;
    }

    public String toString() {
        return String.format( "%d/%d/%d", month, day, year );
    }
}

class DateTest {
    public static void main( String args[] ) {
        System.out.println( "Checking increment" );
        Date testDate = new Date( 03, 13, 2011 );

        for ( int counter = 0; counter < 3; counter++ ) {
            testDate.nextDay();
            System.out.printf( "Incremented Date: %s\n", testDate.toString() );
        }
    }
}

回答by Buhake Sindi

Couple of things:

几件事:

  1. Please remove the following lines of code (inside your Dateclass).

    public static void main(String[] args) {

    }

  2. Make sure that class Dateis found inside Date.java and DateTestis found inside DateTest.java (as you can see that each class name starts with a Capital Letter and the name of the class is exactlythe same as the file name with a .java extension). In DateTest, you'll have to import your Dateclass accordingly.

  1. 请删除以下代码行(在您的Date班级中)。

    公共静态无效主(字符串 [] args){

    }

  2. 确保Date在 Date.java 中找到该类并在 DateTest.javaDateTest中找到该类(如您所见,每个类名都以大写字母开头,并且该类的名称与带有 .java 扩展名的文件名完全相同)。在 中,您必须相应地导入您的类。DateTestDate

Hope this helps.

希望这可以帮助。

回答by donnyton

Remember, in order to run a Java application, it must have a mainmethod. In particular, it must be declared as:

请记住,为了运行 Java 应用程序,它必须有一个main方法。特别是,它必须声明为:

public static void main(String[] args)
{
    //do stuff here
}

When you runyour program in Eclipse, what it is actually doing is executing all the code in main(). It will NOT execute any other methods/code unless you call it from main (or, of course, it is called indirectly through main). Therefore if you want ANYTHING to happen, you'll need to put what you want to actually do in main. For example:

当您在 Eclipse 中运行您的程序时,它实际上是在执行 main() 中的所有代码。它不会执行任何其他方法/代码,除非您从 main 调用它(或者,当然,它是通过 main 间接调用的)。因此,如果您希望任何事情发生,您都需要将您真正想做的事情放在 main 中。例如:

public static void main(String[] args)
{
    System.out.println("Hello World!");
}

when run via Eclipse, will print "Hello World!" and finish. Whatever stuff you want to do with the Date class should then go in that method too.

通过 Eclipse 运行时,将打印“Hello World!” 并完成。无论您想用 Date 类做什么,也应该在该方法中使用。

回答by k-deux

you changed your code - so I edit my answer:

您更改了代码 - 所以我编辑了我的答案:

get rid of the main method in the Date class and make your Date Test class public.

摆脱 Date 类中的 main 方法,并使您的 Date Test 类公开。

public class { ... }

is the DateTest class stored in a File called DateClass.java?

DateTest 类是否存储在名为DateClass.java 的文件中?