-1
  class TapeDeck {

        boolean canRecord = false;

        void playTape() {
                System.out.println("Tape Player");
        }

        void recordTape() {
                System.out.println("Tape Recording");
        }
}

class TapeDeckTestDrive {
        public static void main (String [] args) {

                TapeDeck t = new TapeDeck();

                t.canRecord = true;
                t.playTape();

                if (t.canRecord == true) {
                        t.recordTape();
                }
        }
}

Getting error message, "Exception in thread "main" java.lang.NoSuchMethodError: main"

Chandu
  • 74,913
  • 16
  • 123
  • 127
  • This Community Wiki question lists the possible causes of this common problem: http://stackoverflow.com/questions/5407250/causes-of-java-lang-nosuchmethoderror-main-exception-in-thread-main – Stephen C Jun 28 '11 at 14:41

7 Answers7

3

Run with TapeDeckTestDrive as main class, not TapeDeck - and make your class public

Erik
  • 80,488
  • 12
  • 185
  • 183
2

Make TapeDeckTestDrive a public class like so:

 class TapeDeck {

    boolean canRecord = false;

    void playTape() {
            System.out.println("Tape Player");
    }

    void recordTape() {
            System.out.println("Tape Recording");
    }
}

public class TapeDeckTestDrive {
    public static void main (String [] args) {

            TapeDeck t = new TapeDeck();

            t.canRecord = true;
            t.playTape();

            if (t.canRecord == true) {
                    t.recordTape();
            }
    }
}

Then when you go to run it, call:

java TapeDeckTestDrive
Corey Sunwold
  • 9,892
  • 5
  • 47
  • 55
  • 1
    It is not necessary for a class to be public in order to run from a main(). Most tools though, will presume that the Java source name indicates the main class. If both those classes were 'default' and in TapeDeckTestDrive.java, it would work. – Andrew Thompson Mar 12 '11 at 18:20
  • @Andrew I did not know that. I've just always been told it was "best practice" to keep your driver class public and have never tried otherwise. Is there any instance where keeping it default would be better? – Corey Sunwold Mar 12 '11 at 19:06
1

Maybe because your class is not public.

Patrick
  • 2,587
  • 5
  • 24
  • 45
0

You need to run it as :

java TapeDeckTestDrive

as the class TapeDeckTestDrive has the main method.

Looks like you are running the TapeDeck class which does not have the main method hence the error.

codaddict
  • 410,890
  • 80
  • 476
  • 515
0

TapeDeck.java

public class TapeDeck {

    boolean canRecord = false;

    void playTape() {
            System.out.println("Tape Player");
    }

    void recordTape() {
            System.out.println("Tape Recording");
    }
}

TapeDeckTestDrive.java

public class TapeDeckTestDrive {
    public static void main (String [] args) {

            TapeDeck t = new TapeDeck();

            t.canRecord = true;
            t.playTape();

            if (t.canRecord == true) {
                    t.recordTape();
            }
    }
}

And run using >java TapeDeckTestDrive.java

Alpine
  • 3,730
  • 1
  • 23
  • 18
0

Better make 2 files with your classes and make public the class which has main method. But if you like your above example with 1 files just do:

in command line you can:

javac FileWith2Classes  //compile make you 2 files with your classes
java TapeDeckTestDrive  //and simple run your class where you have main method
lukastymo
  • 23,992
  • 12
  • 50
  • 66
-1

Make your main class to public

public class TapeDeckTestDrive { }

BoomirajP
  • 221
  • 2
  • 7
  • 14
  • No need to method in public, because the both class in same package so default method can access with in the package. Only thing is the class having main method must have the declare public "public class TapeDeckTestDrive { }". – BoomirajP Aug 22 '11 at 04:17
  • and, still, the class having main must **not** be declared public to be executable! At least not with Java 5 or 6; this was true, as I remember, only for the early versions (1.0.1; 1.1) of Java. The JVM has full access to the loaded classes, no matter if it is public or not - actually it also works with a nested **private** (static) class. – user85421 Aug 22 '11 at 11:11
  • i really understood what you are said . But when i declare the class with private and/or static , compiler show the error like Illegal modifier for the class TapeDeckTestDriver; only public, abstract & final are permitted. – BoomirajP Aug 22 '11 at 12:03
  • NESTED classes can be private and static... that's why I wrote "a nested private (static) class" AND again still the class need **not** to be public! – user85421 Aug 22 '11 at 12:05