• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comiler goes mad?

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<gazes intently at crystal ball/>

Your code in Example.java declares a non-public class Eample.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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? ;-)
 
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic