• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Public Class Question  RSS feed

 
Jon Engle
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Evening,

My question is in regard to an example given in the Head First for Java textbook. Here are the two example source files:

Source file 1


Source file 2:

Why do I receive the error below when I try to compile (javac EchoTestDrive.java) ? There is a file named EchoTestDrive.java....and it is declared as a public class....what am I missing?

echotestdrive.java:1: class EchoTestDrive is public, should be declared in a file named EchoTestDrive.java
public class EchoTestDrive {
^
1 error

Cheers,

Jon
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you running this on a Linux system? Looks to me like your file's name is "echotestdrive.java," while your class's name is "EchoTestDrive." Case matters when you're running Linux.
 
Jon Engle
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I am running on a Mac...the file names are listed below and I was already able to compile echo.java successfully creating the Echo.class file.

Jon$ ls -lht
total 24
-rw-r--r-- 1 Jon staff 429B Aug 8 20:24 Echo.class
-rwxrwxr-x 1 Jon staff 488B Aug 8 20:23 echotestdrive.java
-rwxr-xr-x 1 Jon staff 104B Aug 8 20:18 echo.java

Cheers,

Jon

UPDATE: Thank you for your advice...everything is working as expected now:

Jon$ mv echotestdrive.java EchoTestDrive.java
Jon$ javac echotestdrive.java
Jon$ ls -lht
-rw-r--r-- 1 Jon staff 614B Aug 8 21:23 EchoTestDrive.class
-rw-r--r-- 1 Jon staff 429B Aug 8 20:24 Echo.class
-rwxrwxr-x 1 Jon staff 488B Aug 8 20:23 EchoTestDrive.java
-rwxr-xr-x 1 Jon staff 104B Aug 8 20:18 echo.java

Jon$ java EchoTestDrive
helloooo...
helloooo...
helloooo...
helloooo...
10
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stevens Miller wrote:Are you running this on a Linux system? Looks to me like your file's name is "echotestdrive.java," while your class's name is "EchoTestDrive." Case matters when you're running Linux.


Jon Engle wrote:No, I am running on a Mac...the file names are listed below and I was already able to compile echo.java successfully creating the Echo.class file.


Maybe it should have read ... "Are you running this on a [Unix] system? .... Case matters when you're running [Unix]".

Linux is designed to be a clone of Unix (or very Unix-like), although technically not a derivative of Unix, meaning case matters in Linux, just like in Unix.

Mac-OS came from NeXt, which is a derivative of the Mach kernel, which is a derivative of BSD Unix, so yes, case matters.

Henry
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Stevens Miller wrote:Are you running this on a Linux system? ...Case matters when you're running Linux.


Maybe it should have read ... "Are you running this on a [Unix] system? .... Case matters when you're running [Unix]".


Heh, good point, Henry. I don't have a Unix system, so I wasn't able to test that. I tried Jon's scenario on my Fedora distro of Linux and confirmed that case mattered, but extending that to all Unix-like systems would have been an extrapolation on my part.

Interesting that his echo.java file produced an Echo.class file. Jon, when you run "javac EchoTestDrive.java," does that compile echo.java into Echo.class for you? Surprising that it would care at the command-line level, but not at the dependency-resolution level.

Anyway, glad you got it working.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Maybe it should have read ... "Are you running this on a [Unix] system? .... Case matters when you're running [Unix]".

Maybe THAT should have read..."Are you running Java? Case matters when you are running Java."

Even my Windows box complains when the class is named "Tester", but the file is named "tester.java":

C:\slop>javac tester.java
tester.java:1: class Tester is public, should be declared in a file named Tester.java
public class Tester {
^
1 error
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Henry Wong wrote:
Maybe it should have read ... "Are you running this on a [Unix] system? .... Case matters when you're running [Unix]".

Maybe THAT should have read..."Are you running Java? Case matters when you are running Java."

Even my Windows box complains when the class is named "Tester", but the file is named "tester.java":

C:\slop>javac tester.java
tester.java:1: class Tester is public, should be declared in a file named Tester.java
public class Tester {
^
1 error


It's actually quite a bit subtler than that. If you create a class (for the first time) called "Test" and store it in "Test.java," both "javac Test.java" and "javac test.java" will work fine. If you create a class (for the first time) called "Test" and store it in "test.java," "javac Test.java" will work fine, but "javac test.java" will give the error message above. If you create a class (for the first time) called "Test," store it as "Test.java" then store it again as "test.java," (saying "Yes" when asked if you want to replace "Test.java") both "javac Test.java" and "javac test.java" will work fine. (Note that, if you actually try this, you have to create entirely new classes, with names like "Test1," "Test2," and so on, or the filesystem's hidden behavior will confuse Heck out of your results).

All Windows file systems that support extended names will preserve the case of the name for you, but Windows itself treats "Test.java" and "test.java" identically. The arcana of NTFS (what I used for my tests) are more complex than a typical user (or Java programmer) could ever want to know, with all kinds of caching, on-disk optimizations of various sorts, and so on. As a result, sometimes case matters, and sometimes it doesn't, and (hang onto your seat) invisible state can decide when it does and when it doesn't. That is, as you can see from the tests above, you can't know from the filename and class name alone whether or not "javac test.java" is going to work.

What you can know is that, if your class names match your filenames and your javac commands refer to files with the same case as well, that will work.

At one time in the distant and misty past, DOS was unambiguous about all this: case didn't matter to DOS. More exactly, DOS's much-loved FAT-16 system originally stored everything purely in upper case (in the venerable old 8.3 format). Unix, of course, was way ahead, with mixed case, no hard limits or formatting impositions on name structure, and a number of other simplifications (some of which allowed for some pretty quirky filenames, including names with spaces in them and, believe it or not, even backspace characters). As the years have gone by and Microsoft realized that Unix simply would not be ignored, their command-level stuff seems to have (perhaps grudgingly) become more Unix-like, with extended naming and NTFS appearing to preserve case (while the system itself continued to ignore it, probably since millions of lines of code already existed that would break if Windows itself suddenly became case-sensitive).

Bottom line, for me, is this: I name my files precisely the same as my classes, and I refer to those files by those precise names. Anything else is asking for grief, imho.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!