简体   繁体   中英

I cant run this program in eclipse but it works on JGRASP

I have the following code that makes an abstract class, concrete class that extends abstract class and a main method. There are no errors as it compiles and run fine in JGRASP. when I run in eclipse, it is just not running, but no errors produced. I named the file Product.java.

Here is the code:

abstract class Product {
    int value;

    public Product(int val) {
        value = val;
    }

    abstract public int multiply(int n);
}

class TimesTwo extends Product {
    public TimesTwo(int val) {
        super(val);
    }

    @Override
    public int multiply(int n) {
        return value * n;
    }

    public static void main(String[] args) {
        TimesTwo two = new TimesTwo(5);
        System.out.println(two.multiply(5));
    }

}

I also try to run on commandline:

javac Product.java

I get Product.class and TimesTwo.class

and when I run Java TimesTwo.class or Java Product.class

I get Exception in thread "main" java.lang.NoClassDefFoundError:

Eclipse运行之前,请确保TimesTwo.java作为单独的Java源文件存在。

You should change TimesTwo to public class and try to run command as below:

java TimesTwo

There is no .class at the ending when running a class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM