I am beginner on java and i am having trouble while compiling below given code on CMD.

This code is taken from a book.

E:\sher\SkyDrive\Exercise Files\L04>javac Student.java

E:\sher\SkyDrive\Exercise Files\L04>java Student
Exception in thread "main" java.lang.NoSuchMethodError: main

// Student.java
/* Demostrates the most basic features of a class. A student is defined by their
name and rollNo. There are standard get/set accessors for name and rollNo. */

// public Static Void main (String args[]){}

public class Student{
    private String name;
    private int rollNo;

// Standard Setters

public void setName (String name){
    this.name=name;
    }

    // Note the masking of class level variable rollNo.

    public void setRollNo(int rollNo){
        if(rollNo>0){
            this.rollNo = rollNo;
            }   else    {
                this.rollNo=100;
                }
        }


// Standard Getters

public String getName(){
    return name;
}

public int getRollNo(){
    return rollNo;
}

// Constructor that uses a default value instead of taking an argument.

public Student() {
    name = "not set";
    rollNo = 100;
}

//parameterized Constructor for a new student

public Student(String name, int rollNo){
    setName(name);      //call to setter of name
    setRollNo(rollNo);      //call to setter of rollNo
}

//copy Constructor for a new student

public Student(Student s){
    name = s.name;
    rollNo = s.rollNo;
}

//method used to display method on console

public void print(){
System.out.println("Student name:" + name + ",roll no:" + rollNo);
}
}// end of class

Hi,
You cannot call a java class if it doesn't have a main method. In the code you posted there is this line:
// public Static Void main (String args[]){} (Static needs to change to static)
When you run a java class: E:\sher\SkyDrive\Exercise Files\L04>java Student
Its main method is called.
Simply uncomment the method main, or create a new class with a main method inside. In the body of the main you will put the code that you want to execute.

Thanks a lot for your reply and solution.

When I did the same thing like removeing the comment from line (//public static void main (String args[]){}), and placing this line after line # 7 before (private String name;).

My code complied successfuly, but when i ran the program, it shows no output on console. :(

If i shift all the code after the class Student into the main function, i got 27 errors on compiling the code. :(

Here is what i did:

public class Student{

public static void main (String args[]){

    private String name;
    private int rollNo;

// Standard Setters

public void setName (String name)
    {
    this.name=name;
    }

    ...........

    //method used to display method on console

public void print(){
                    System.out.println("Student name:" + name + ",roll no:" + rollNo);
                    }
    } //End of main


}// End of class

But if i do the below method, code compiled and ran without errors but show no output.

public class Student{

public static void main (String args[]){

    private String name;
    private int rollNo;

You can not define a method inside of another method. Make sure there is an ending '}' for a method's definition BEFORE starting to define a new method.
Line 3 starts a method definition. Line 10 starts a new method definition. There needs to be an ending '}' for the method started on line 3 before the new definition on line 10.

Yes I am new on forums and want to learn JAVA. I posted my problem on the forum which you indicated about.

But still found no working solution. :'(

My program is not compiling correctly, it gives erros.

The first mistu have

did is, inserting another method within main method.you can't do that. you can add multiple methods within a class not within a method.
So first correct it.

I found there is problem in print method also.

I am giving general solution here.i think this might be useful to you.just go through it.

public class Student
{

public static void main (String args[])
{
String name="";
int rollNo=100;
    Student s1 = new Student();
   name=s1.setName("God");
   s1.print(name,rollNo);
// Standard Seters
}
public String setName (String name1)
    {
    return name1;
    }
   // ...........
    //method used to display method on console
public void print(String name,int rollNo){
                    System.out.println("Student name:" + name + ",roll no:" + rollNo);
                    }
     //End of main
}// End of class

hm ....
thank you ...!
I've solved this error because of your correction ...!
I appriciated your effort regarding java...!
If I need further helpline what should be done by me...!
Can I email you?
if yes then please share your email address with me...!

Aries: this entire forum is about helping people in Java.
ver few appreciate getting pm's and/or personal mails. the point is to ask questions in a (new) thread on this forum, so that everyone can benefit from the replies and sollutions that are provided by the members (and so more than just one person can help out). it's a bit the same concept as 'open source'.

This article has been dead for over six months. Start a new discussion instead.