• 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

"couldn't find main class" error comes if the same class is executed and throws exception?

 
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Consider this code:
And the output is:

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at Test.<init>(Test.java:5)
at Test.<clinit>(Test.java:2)
Could not find the main class: Test. Program will exit.


I understand the null pointer exception since I am trying to use an object which is still in its creation.

But why does the error "could not find the main class" comes?
 
Rajdeep Biswas
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes its compiled and executed, and I am getting the desired ExceptionInInitializerError also as you can see in the output. Try it yourself.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my GUESS is that you the Test object isn't fully created by the time the "int b = ..." line runs. I believe he below code indicates that the constructor hasn't completed by the time we get to line 6:

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I quote this from the JLS. Read point number12.4.1, specifically the first point:

A class or interface type T will be initialized immediately before the first occurrence of any one of the following:

T is a class and an instance of T is created.



You don't yet have an instance of Test class that you are trying to assign to the static reference variable test. Hence, it throws a StaticInitializerError. Only after you have an instance can you do that. So, in essence , be careful about the order in which you write a static initializer block or static assignment in your class. The component on the right hand side of the = has to exist at compile time for this to succeed.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajdeep Biswas wrote:
I understand the null pointer exception since I am trying to use an object which is still in its creation.

But why does the error "could not find the main class" comes?


Are you running this in an IDE ?
Running from a command prompt I don't get that last line. I suspect it is just something the IDE puts out when it fails to run the class for any reason.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote:
You don't yet have an instance of Test class that you are trying to assign to the static reference variable test. Hence, it throws a StaticInitializerError.



Wrong.

First, that's not the exception that's thrown. There's no such thing as StaticInitializerError. It's an ExceptionInInitializerError and its root cause is a NullPointerException, caused by accessing test.a in the instance initializer (NOT static initializer) before test has been assigned.

That block runs before the constructor as part of the process of creating a Test instance in the static Test test = new Test() line. But since test isn't assigned until after the Test object is fully created, we get the NPE, which gets wrapped into the ExceptionInInitializerError.


The component on the right hand side of the = has to exist at compile time for this to succeed.



Nonsense.

Compile time has nothing to do with it. None of those objects exist at compile time. It's simply that at runtime a reference has to have a non-null value assigned before we can dereference it.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:
Are you running this in an IDE ?
Running from a command prompt I don't get that last line. I suspect it is just something the IDE puts out when it fails to run the class for any reason.



Yeah, that's probably just something the IDE spits out as a side-effect of the fact that it never entered main().
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Mansukhdeep Thind wrote:
You don't yet have an instance of Test class that you are trying to assign to the static reference variable test. Hence, it throws a StaticInitializerError.



Wrong.

First, that's not the exception that's thrown. There's no such thing as StaticInitializerError. It's an ExceptionInInitializerError

and its root cause is a NullPointerException, caused by accessing test.a in the instance initializer (NOT static initializer) before test has been assigned.

That block runs before the constructor as part of the process of creating a Test instance in the static Test test = new Test() line. But since test isn't assigned until after the Test object is fully created, we get the NPE, which gets wrapped into the ExceptionInInitializerError.



I had rectified it to ExceptionInInitializerError. But when I clicked submit, the site threw that "Aaaagh.. You caught us tinkering...." message. And then I forgot to change it again. Sorry for that.


Jeff Verdegan wrote:

Mansukhdeep Thind wrote:The component on the right hand side of the = has to exist at compile time for this to succeed.



Nonsense.

Compile time has nothing to do with it. None of those objects exist at compile time. It's simply that at runtime a reference has to have a non-null value assigned before we can dereference it.



Yes, I was wrong. Time for me to take it easy.
 
Rajdeep Biswas
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Running it in an IDE or directly at command prompt yields the same last line

Could not find the main class: Test. Program will exit.

 
Rajdeep Biswas
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to know the reason behind this as to why the main class could not be located. IDEs I can understand but not absolutely in direct compilation case. Please enlighten me.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff wrote: ...and its root cause is a NullPointerException, caused by accessing test.a in the instance initializer (NOT static initializer) before test has been assigned.



So essentially, what you are saying is that the Test instance has not been assigned to static variable test and is being accessed inside the instance initializer block. Hence, the NPE. Correct? And since the assignment is itself a part of the static initialization, it wraps the NPE inside a ExceptionInInitializerError. Am I correct?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajdeep Biswas wrote:I want to know the reason behind this as to why the main class could not be located. IDEs I can understand but not absolutely in direct compilation case. Please enlighten me.



That error is a bit misleading.

What's happening--the real error--is as I described previously. You're trying to access test.a before test has been set. You can see that from the ExceptionInInitializerError with the cause of NPE. The reason this results in the "can't find main class" error in some cases is, I assume, because the logic of some JVMs says, "If we die before executing main(), assume it's because we can't find the class we were asked to run." The reason it only happens in some cases and not all is because some JVMs are apparently smarter than that, and realize that we can die before hitting main() for other reasons, such as what happens here.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote:

Jeff wrote: ...and its root cause is a NullPointerException, caused by accessing test.a in the instance initializer (NOT static initializer) before test has been assigned.



So essentially, what you are saying is that the Test instance has not been assigned to static variable test and is being accessed inside the instance initializer block. Hence, the NPE. Correct? And since the assignment is itself a part of the static initialization, it wraps the NPE inside a ExceptionInInitializerError. Am I correct?



Yes, except that it's that a reference to the Test instance has not been assigned. Objects are not values in Java, and no object is ever assigned to a variable.

And it's not the assignment to test being part of the static initialization that matters. It's the creation of a test instance. That happens in a static initialization, but it relies on the results of that initialization having already been set.
 
Rajdeep Biswas
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. Thanks to Jeff, Mansukh and others for the information. Now I am assuming that the reaction might vary for different JVM implementations.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajdeep Biswas wrote:Now I am assuming that the reaction might vary for different JVM implementations.



The "ExceptionInInitializer ... cause by NullPointerException" part will not vary (except maybe for differences in the wording of the exception messages from one version to the next).

The "couldn't find main class" part has already been demonstrated to vary, right in this thread.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally, I never look at anything past the first error. I figure that whatever cause the first error is going to make everything else so unstable that any additional thing reported is dubious, at best. Fix the first problem reported, and re-run your tests.

Trying to figure out why the second (and third, and fourth, and fifth...) errors show up is a loosing game.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Mansukhdeep Thind wrote:

Jeff wrote: ...and its root cause is a NullPointerException, caused by accessing test.a in the instance initializer (NOT static initializer) before test has been assigned.



So essentially, what you are saying is that the Test instance has not been assigned to static variable test and is being accessed inside the instance initializer block. Hence, the NPE. Correct? And since the assignment is itself a part of the static initialization, it wraps the NPE inside a ExceptionInInitializerError. Am I correct?



Yes, except that it's that a reference to the Test instance has not been assigned. Objects are not values in Java, and no object is ever assigned to a variable.



Apologies to the OP for digressing a bit from the main issue. So Jeff, the correct jargon to use here would be that references point to objects that are created on the heap as a part of class instantiation and instance variables are assigned respective values. Is that correct?


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote:the correct jargon to use here would be that references point to objects that are created on the heap as a part of class instantiation



Yeah, I guess that's about right.

and instance variables are assigned respective values. Is that correct?



Not just instance variables. Any variable can be assigned a value. Not sure what you're getting at there.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote:Apologies to the OP for digressing a bit from the main issue.


We would much rather that you didn't apologize for digressing, but that you didn't digress in the first place. This has become a bit of an irritating habit of yours, and it needs to stop. If you have questions yourself, start a new topic instead of hijacking someone else's.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:

Mansukhdeep Thind wrote:Apologies to the OP for digressing a bit from the main issue.


We would much rather that you didn't apologize for digressing, but that you didn't digress in the first place. This has become a bit of an irritating habit of yours, and it needs to stop. If you have questions yourself, start a new topic instead of hijacking someone else's.



OK
 
Don't play dumb with me! But you can try this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic