• 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

Premain Method in Java

 
Ranch Hand
Posts: 277
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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

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
Oracle Spring Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic