File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes compiler error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compiler error" Watch "compiler error" New topic
Author

compiler error

kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
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();
}
}
}


Who dares, wins! (SAS motto)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

This compiles and runs fine for me. What error messages are you getting?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Sep 02, 2000
Posts: 197
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Sep 02, 2000
Posts: 197
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

Joined: Aug 20, 2001
Posts: 1817

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.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.
 
jQuery in Action, 2nd edition
 
subject: compiler error