wood burning stoves*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""couldn Watch ""couldn New topic
Author

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

Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

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

Joined: Mar 26, 2012
Posts: 186

Yes its compiled and executed, and I am getting the desired ExceptionInInitializerError also as you can see in the output. Try it yourself.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

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:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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.


~ Mansukh
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
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.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Mar 26, 2012
Posts: 186

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

Joined: Mar 26, 2012
Posts: 186

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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 26, 2012
Posts: 186

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

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

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41085
    
  43
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.


Ping & DNS - my free Android networking tools app
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: "couldn't find main class" error comes if the same class is executed and throws exception?
 
Similar Threads
Regarding Assertion
Thread question required
//Comment in java
javac problem
class in the default package not found by javac?