This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Compiling from command line with a different file name  RSS feed

 
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' ??


 
Java Cowboy
Sheriff
Posts: 16083
88
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.
 
Ranch Hand
Posts: 4702
9
Java Scala
  • 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' ?

 
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 !!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!