• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Head First error

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get the following error when trying to compile an example from the book.

MyEx.java:5: class ExTestDrive is coderanch, should be declared in a file named ExTestDrive.java
public class ExTestDrive {
^
1 error


Here is the example.



class MyEx extends Exception { }

public class ExTestDrive {

public static void main(String[] args) {

String test = args[0];

try {

System.out.print("t");
doRisky(test);
System.out.print("o");

} catch (MyEx e) {

System.out.print("a");

} finally {

System.out.print("w");

}

System.out.print("s");

}

static void doRisky(String t) throws MyEx {

System.out.print("h");

if ("yes".equals(t)) {

throw new MyEx();

}

System.out.print("r");

}

}
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There can only be one public top-level class in a source file, and the name of that source file must match exactly with the name of the public class. Java is case-sensitive.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adam, welcome to JavaRanch!

Compiler errors are usually pretty good at telling you what's wrong. In this case...

MyEx.java:5: class ExTestDrive is coderanch, should be declared in a file named ExTestDrive.java

...tells you that there's a problem on line 5 of your file MyEx.java. Specifically, you've declared a public class called "ExTestDrive" that needs to be declared in a file called "ExTestDrive.java."

The reason behind this error is that the Java compiler allows no more than one public class definition per source file -- and the file must share the name of that public class. So in other words, if you have a public class called ExTestDrive, then it needs to be in a file called ExTestDrive.java.
 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"Cuius rei demonstrationem mirabilem sane detexi hanc marginis exiguitas non caperet." ~Pierre de Fermat






 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vishwas Hegde:
...


Wikipedia: Fermat's Last Theorem.
 
Adam Prebola
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow! That was fast. I guess I should've said, I understand the error and why, but this is a book for beginners. However, it doesn't state to make a seperate file. So I would assume that I should put all the code in one file. Does this seem odd?
 
Vishwas Hegde
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks marc...

In my humble opinion... It would have been easier to all, if you had written it in English....

Anyways.. thanks

Regards
Vishwas Hegde
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vishwas Hegde:
... It would have been easier to all, if you had written it in English...


I think the original Latin is more likely to prompt people to Google and discover the background that makes this quote so intriguing (and even humorous in this context). An English translation would likely be taken at face value.
[ February 21, 2006: Message edited by: marc weber ]
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah. Fermat was great.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Lynn:
Yeah. Fermat was great.


And Sir Andrew Wiles!

One of my favorite books is Fermat's Enigma by Simon Singh. (A NOVA episode called "The Proof" was based on this book. Here's a transcript.)
reply
    Bookmark Topic Watch Topic
  • New Topic