Search code examples
javajprofiler

How do I detect from my application that it was started with JProfiler?


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?


Solution

  • 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;
    }