• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comiler goes mad?  RSS feed

 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there people!!

OK, so I've got a question for you (surprise, surprise!).

I've been playing around with my new and shiny coding skills and created a source file called Example.java (how original!). Compiled it, no problem. But when I tried to run this little sucker something weird happened. Message 'Error: Could not find or load main class Example' popped up. Being as clever as I am, I 'lsed' the content of the folder and discovered a very bizarre turn of events. My compiler ate 'x' in 'Example'. Is there any particular reason why it would happen? I mean it's not a biggie but it made me curious.
I did compile the source file few times and every time the compiler creates 'Eample.class' instead of 'Example.class' file..
Got to love mystery!

Thanks!!
K.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<gazes intently at crystal ball/>

Your code in Example.java declares a non-public class Eample.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If Darryl isn't correct (crystal ball gazing can be tricky), you could post your code, and please UseCodeTags (← that's a link).
 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:<gazes intently at crystal ball/>

Your code in Example.java declares a non-public class Eample.


That was awesome

It, indeed was me, not the machine, who was wrong.

Attention to details you say? ;-)
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your Example.have
You made a class as "Eample"
Reson behind creation of Eample. class
-> Compiler will always create a .class(bytecode from your source code) for every class you have in your .java file(Example.java)
At runtime your .class(bytecode)gets executed
So you have to provide correct .class name at runtime
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:-> Compiler will always create a .class(bytecode from your source code) for every class you have in your .java file(Example.java)
At runtime your .class(bytecode)gets executed
So you have to provide correct .class name at runtime

I fully disagree with first sentence and partially with third.

1 .class files will be created if:

a) .java file will contain at least 1 declared class.
b) .java file will contain 0 or 1 declared "public" class.
c) .java file will be named exactly the same as declared "public" class.
d) .java file will contain 0 or more "non-public" classes.

("a", "b" and "d" must be true, "c" - must be true only when "public" class is declared).

2. In order to execute you need to provide correct class name and that class suppose to contain correctly defined method "main", which is usually seen in a form:
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am at fault for giving ambiguous statement.Now I am trying to modify
my words
Compiler will always form a .class for every class in .java file ,if all classes are not public.

In case of any public class you cannot have more than 1 public class in 1.java file,and that .java file must have same name as your .class(public)

You can only execute a class if it has main method in a form which can be identified by jvm
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:Compiler will always form a .class for every class in .java file ,if all classes are not public.

Incorrect. Not necessarily all has to be non-public.

.java file can contain mix of "public" and "non-public" classes. Cannot be more than 1 "public" class, and if you have 1 - the name has to match with filename.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is permissible to have a completely empty .java file, and you will then get exactly 0 .class files!
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Vilda kindly read my post again
I had mentioned Both cases
.java with any public class or without any public class

It is absolutely understood that there can be other classes(non public) with 1 public class.Atleast I knew this
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:Yes Vilda kindly read my post again
I had mentioned Both cases
.java with any public class or without any public class

I misread your post. You were right in your last post - sorry.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!