Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compiler error

 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone pls explain to me why this won't compile

class TapeDeck {
boolean canRecord=false;
void playTape() {
System.out.println("tape playing");
}

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();
}
}
}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This compiles and runs fine for me. What error messages are you getting?
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably put these two classes in a file called TapeDeckTestDrive.java or TapeDeck.java. In both cases, the compiler will complain, because neither TapeDeckTestDrive nor TapeDeck is public.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Smith:
You probably put these two classes in a file called TapeDeckTestDrive.java or TapeDeck.java. In both cases, the compiler will complain, because neither TapeDeckTestDrive nor TapeDeck is public.

No, the compiler would complain if one of these classes were public and the java file did not share the same name (or if there were more than one public class in the same file). But the fact that neither class is public shouldn't matter.

Mine compiled as TapeDeckTestDrive.java.
 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now it compiles, but when i try to run java TapeDeck, I get:

Exception in thread "main" java.lang.NoSuchMethodError: main
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kwame Iwegbue:
now it compiles, but when i try to run java TapeDeck, I get:

Exception in thread "main" java.lang.NoSuchMethodError: main


Yes, because your main() is in TapeDeckTestDrive, not TapeDeck. Run that class instead.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TapeDeck does not have a main method, so it cannot be run from the command line. Try running TapeDeckTestDrive instead. (You should not have to recompile.)
 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your help guys.
it finally compiled after I saved the file in notepad, and added quotes e.g "TapeDeckTestDrive.java"

someone pls explain this to me.
it make no sense, since on a different computer, It compiles without being saved with quotes!!
[ June 22, 2005: Message edited by: kwame Iwegbue ]
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that notepad is fairly dumb, and it will append .txt to anything that you save if it isn't a .txt extension. in order to make sure that your file has the extension you want, you have to use the quotes.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also change the type from "text file" to "all types" or whatever it is, and then it won't append the ".txt" to your filename.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or the best suggestion at all: don't use notepad! There are many, many free and commercial programmer's editors available, all of then infinitely better than notepad.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic