wood burning stoves*
The moose likes Beginning Java and the fly likes Compiling from command line with a different file name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compiling from command line with a different file name" Watch "Compiling from command line with a different file name" New topic
Author

Compiling from command line with a different file name

Mandy Singh
Greenhorn

Joined: May 30, 2011
Posts: 23

Hello Everyone,

I have a source file called 'dog.java' which has a public class called 'Dog'.

I know that the file name and the public class name must be the same but was just experimenting to learn something new.

when i execute the command: javac dog.java ; i get an error as expected.

but on executing: javac Dog.java ; the file gets successfully compiled ( Even though 'Dog.java' did not exist ).

This maybe due to the fact that in order to get the file handle, 'javac' uses file system interfaces which don't differentiate between upper and lower case characters ( i have checked this on windows ). Hence, it gets hold of the file ( dog.java ) and then 'javac' compares the input to it i.e. 'Dog.java' and the public class 'Dog'.

But if that is true, why does this compile successfully...

File name: 'Dog.java' public class name: 'Dog' command: 'javac dog.java'

Here, the input to 'javac' was 'dog.java' which doesn't match the class name 'Dog'.

I mean what is going on here ? Who's the villain - 'javac' or 'the file system' or 'myself' ??



*** If opportunity doesn't knock, build a door !! ***
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

Are you using Windows? File names on Windows are case-insenstive; as far as Windows is concerned, "dog.java" is the same as "Dog.java". However, the Java compiler is case-sensitive with regard to filenames and class names.

It's recommended to always make the filename exactly the same as the class name, also on systems like Windows that have case-insensitive filenames.

The villain is the Windows file system.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

its true. i noticed it a while back. i was using the command prompt window to cd(change directory). i accidentally typed lab05 instead of Lab05 and it worked anyway. kind of suprised me.


SCJP
Visit my download page
Mandy Singh
Greenhorn

Joined: May 30, 2011
Posts: 23

Guys,

its true that the file system on windows is case-insensitive, but what role does 'javac' play here with respect to the file names i.e. considering the two cases mentioned in my question ?

Shouldn't 'javac' report an error in the case where...

File name: 'Dog.java' public class name: 'Dog' command: 'javac dog.java'

..as the input to 'javac' does not match the class name: 'Dog' ?

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Actually the compiler is not specified to require that the file name match the public class name, although implementations may enforce that. All the spec cares about is that a top-level public class is in a .class file of the same name.
Mandy Singh
Greenhorn

Joined: May 30, 2011
Posts: 23

Thanks Everyone !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compiling from command line with a different file name