Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Premain Method in Java

 
Ashwin Sridhar
Ranch Hand
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15367
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ashwin Sridhar
Ranch Hand
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic