wood burning stoves 2.0*
The moose likes Java in General and the fly likes Premain Method in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Premain Method in Java" Watch "Premain Method in Java" New topic
Author

Premain Method in Java

Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Hi All,

Have anyone used Premain method in java?

I am using -javaagent on my server and I was suprised to note that main method is not the entry to the class but it is Premain method.

What does actually Premain Method do ?

This what java doc says , java-doc

Regards,
Ashwin


Ashwin Sridhar
SCJP | SCWCD | OCA
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

This has to do with a feature of Java called "agents". Have a look at the API documentation of the package java.lang.instrument for more information about this. I've never used that myself before, but it has to do with debugging Java programs. An agent is a special module that you can load when the JVM starts, that can do things to help with debugging or monitoring the application that's going to run in the JVM.

Normal programs don't have a premain method.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

I used this feature of java. I used it to set variables on my server at the start up and use them to create resources at server level.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ashwin Sridhar wrote:I used this feature of java. I used it to set variables on my server at the start up and use them to create resources at server level.


That's not what it's for. There's a better way to accomplish that.
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

That's not what it's for. There's a better way to accomplish that


While implementing I thought it may not be for this purpose. Since everything worked out, I never bothered too much. What could be the downside of this approach ?

What are other better ways to accomplish this ?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ashwin Sridhar wrote:
That's not what it's for. There's a better way to accomplish that


While implementing I thought it may not be for this purpose. Since everything worked out, I never bothered too much. What could be the downside of this approach ?


It's non-standard and may confuse whoever looks at the code in the future (you or other developers). It may have undesirable side effects. It may require some additional context or configuration that you can't assume will always be there.

Even if it always functions perfectly, the first reason is enough not to use it given that there are more conventional alternatives.

What are other better ways to accomplish this ?


Hard to say without more context as to how you're executing and what kind of setup you're doing.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Premain Method in Java