• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compiling from command line with a different file name

 
Mandy Singh
Greenhorn
Posts: 23
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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' ??


 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Randall Twede
Ranch Hand
Posts: 4439
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mandy Singh
Greenhorn
Posts: 23
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Everyone !!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic