2

I know that I "shouldn't do this", but I fiddle with the ClassLoaders a bit and JProfiler doesn't like that and it hasn't to do anything with what I want to profile, so just stick to the question, please :).

How do I detect from my application that it was started with JProfiler? Or maybe just that JProfiler is currently connected?

Daniel
  • 27,718
  • 20
  • 89
  • 133
  • 1
    You can inspect command line arguments (Not those you get in `main` method) and search for "agent". – talex Sep 27 '16 at 13:58
  • Oookay. How do I get those? Do I need native Code die that? – Daniel Sep 27 '16 at 14:04
  • 1
    See http://stackoverflow.com/questions/1490869/how-to-get-vm-arguments-from-inside-of-java-application for how to get the VM parameters. JProfiler should not interfere with your classloaders, what problem do you experience? – Ingo Kegel Sep 27 '16 at 14:26
  • I don't know. I asked this question here http://stackoverflow.com/questions/39727323/get-all-command-line-option – talex Sep 27 '16 at 14:27
  • @IngoKegel: Thanks for that, that worked out! – Daniel Sep 27 '16 at 14:34

1 Answers1

2

The answer is simple if you know how:

public static boolean isStartedWithJProfiler() {
    RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
    List<String> arguments = runtimeMxBean.getInputArguments();
    for( String argument : arguments ) {
        if( argument.contains("profilerti") ) {
            return true;
        }
    }
    return false;
}
Daniel
  • 27,718
  • 20
  • 89
  • 133