Harpreet Singh
Harpreet Singh

Reputation: 23

Unable to run program

I've been using Eclipse for running Java Programs. Everything was going well earlier but now I'm unable to get the option "1 Java Application" when I click on "Run as" despite having zero errors in my program. Could anyone help me how to deal with it ?

class Base{
    public int baseVar;
    public int var;
    public Base(int v){
     baseVar=v;
     System.out.println("Base class parameterized constructor");
     }
}
class Der extends Base{
    public int derVar;
    public int var;
    public Der(int v){
        super(v);
      derVar=v;
      System.out.println("Derived class parameterized constructor");
    }
    public void display(){
      System.out.println("Base variable value="+baseVar);
      System.out.println("Derived variable value="+derVar);
    }
    public void useOfSuper(){
      var=15;
      var=20;
      System.out.println("Base variable var=" + 
                     super.var);
      System.out.println("Derived variable var="+var);
    }
}
class abc{
    public static void main(String args[]){
      Der Derobj=new Der(10);
      Derobj.display();
      Derobj.useOfSuper();
    }
}

Upvotes: 1

Views: 155

Answers (4)

Jon Skeet
Jon Skeet

Reputation: 1499770

I suspect your program doesn't have an appropriate main method:

public static void main(String[] args)

If it does, try running it from the command line - does that work?

EDIT: As noted in comments, whether or not the presence of a main method affects the context menu appears to depend on the version of Eclipse. In the version I'm using at home (4.2.1) the context menu option doesn't appear unless there's a main method.

Upvotes: 2

Dave Richardson
Dave Richardson

Reputation: 4985

Your main() is in your class abc, move it into your Base and things will work (I am assuming here that all of the code you published has been placed in a single file named Base.java)

Upvotes: 0

user2336315
user2336315

Reputation: 16067

Change :

class abc

with

public class abc

I suspect that the class is private, hence you can't run it. You have to change your java name file to abc.java and make the class abc public.

Upvotes: 1

Harish Kumar
Harish Kumar

Reputation: 528

Please use below. Your code is missing public identifier for class that contains main method.

public class abc {
    public static void main(String args[]) {
        Der Derobj = new Der(10);
        Derobj.display();
        Derobj.useOfSuper();
    }

Upvotes: 0

Related Questions