aspose file tools*
The moose likes Programming Diversions and the fly likes how to start before the main starts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "how to start before the main starts" Watch "how to start before the main starts" New topic
Author

how to start before the main starts

shantanu kaushik
Greenhorn

Joined: Dec 12, 2011
Posts: 6
how to run a piece of code even before the main method is executed!!!
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.7


luck, db
There are no new questions, but there may be new answers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
How do you think?

Since this is a trick for entertainment purposes only, I shall move this thread to where we discuss such things.
Andrew Vershinin
Greenhorn

Joined: Feb 16, 2012
Posts: 4
Use static initializer.

static {
//your code goes here...
}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Welcome to the Ranch

What happens if you try that in Java7? Particularly if you miss out the main method?
Andrew Vershinin
Greenhorn

Joined: Feb 16, 2012
Posts: 4
Campbell Ritchie wrote:Welcome to the Ranch

Thanks
Campbell Ritchie wrote:What happens if you try that in Java7? Particularly if you miss out the main method?

1. AFAIK, Java 7 supports static initializers, so it will work as usual - after loading of the class.
2. If you miss out the main method - it will cause an error, and code inside initializer will not be executed.
Excuse me for my broken English
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
And what if you try it in Java6?
Andrew Vershinin
Greenhorn

Joined: Feb 16, 2012
Posts: 4
Campbell Ritchie wrote:And what if you try it in Java6?

The same thing.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Campbell, is there a reason why you think it might be different between 5 and 6?
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281

I think static initializer Block are there from JDK 1.0 as its language feature not an API enhancement.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Viktor Kubinec
Ranch Hand

Joined: Jan 28, 2012
Posts: 34
Andrew Vershinin wrote:
Campbell Ritchie wrote:Welcome to the Ranch

2. If you miss out the main method - it will cause an error, and code inside initializer will not be executed.


This is not right. The code inside initializer will be executed and then the error will be thrown (NoSuchMethodError : main)
shantanu kaushik
Greenhorn

Joined: Dec 12, 2011
Posts: 6
Thank you very much for all the details provided
Andrew Vershinin
Greenhorn

Joined: Feb 16, 2012
Posts: 4
Viktor Kubinec wrote:
Andrew Vershinin wrote:
Campbell Ritchie wrote:Welcome to the Ranch

2. If you miss out the main method - it will cause an error, and code inside initializer will not be executed.


This is not right. The code inside initializer will be executed and then the error will be thrown (NoSuchMethodError : main)

I tried:
Test.java


javac Test.java
java -cp . Test

Error: Main method not found in class Test, please define the main method as:
public static void main(String[] args)


Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3682
    
  16
Mike Simmons wrote:Campbell, is there a reason why you think it might be different between 5 and 6?

I think Campbell was suggesting there's a difference between 6 and 7 and if this post is correct (I haven't tried it) then he is right.


Joanne
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Ah, that's probably what he's thinking of. But that post is about what happens if you try to execute a class with no main() method - which has been contrary to the spec all along. It's just that they've recently closed another loophole in the implementation. Whereas this thread here is about executing code before the main() method - which has been allowed, via class initializers, since the dawn of time. (Java time, that is.)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Yes, that is what I was thinking of. I take the point that is not strictly what the question is about, however.
Viktor Kubinec
Ranch Hand

Joined: Jan 28, 2012
Posts: 34
Andrew Vershinin wrote:

This is not right. The code inside initializer will be executed and then the error will be thrown (NoSuchMethodError : main)

I tried:
Test.java


javac Test.java
java -cp . Test

Error: Main method not found in class Test, please define the main method as:
public static void main(String[] args)




Ok, you are right. Sorry. It depends on java version. I'm using java 6, where the static initializer is executed even if the main method is not defined. As it seems in java 7 t is not a you have just shown.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to start before the main starts