Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As others have mentioned elsewhere, you can do this by attaching an agent to the JVM, as that is the only place the JVM will report on what classes are loaded.</p> <p>This class shows how that is done:</p> <pre><code>import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.lang.instrument.Instrumentation; import java.lang.reflect.Method; import java.util.HashSet; import java.util.Set; public class ClassReporter implements Runnable { /** Output file */ File output; /** The JVM instrumentation */ Instrumentation instrument; /** Already reported classes */ Set&lt;Class&lt;?&gt;&gt; known = new HashSet&lt;Class&lt;?&gt;&gt;(); /** Is reporter still running? */ volatile boolean running = true; @Override public void run() { System.out.println("Class Reporter agent running"); while( running ) { if (!report()) return; try { Thread.sleep(5000); } catch (InterruptedException ie) { ie.printStackTrace(); return; } } } /** * Update the report of loaded classes * * @return true if report was written OK */ public synchronized boolean report() { Set&lt;Class&lt;?&gt;&gt; report = new HashSet&lt;Class&lt;?&gt;&gt;(); Class&lt;?&gt;[] classes = instrument.getAllLoadedClasses(); for(Class&lt;?&gt; c:classes) { if (known.add(c)) report.add(c); } if (report.isEmpty()) return true; boolean ret = true; FileWriter fw = null; try { fw = new FileWriter(output, true); for(Class&lt;?&gt; c:classes) { fw.write(c.getName()); fw.write('\n'); for(Method m:c.getDeclaredMethods()) { fw.write(m.toGenericString()); fw.write('\n'); } fw.write('\n'); fw.write('\n'); fw.flush(); } } catch (IOException ioe) { ioe.printStackTrace(); ret = false; } finally { if (fw != null) { try { fw.close(); } catch (IOException ioe) { ioe.printStackTrace(); ret = false; } } } return ret; } /** * Initialize the reporter * * @param agentArgs * the output file name * @param inst * the instrumentation */ public static void premain(String agentArgs, Instrumentation inst) { final ClassReporter cr = new ClassReporter(); cr.instrument = inst; File out = new File(agentArgs); out.delete(); try { out.createNewFile(); } catch (IOException ioe) { System.out.println("Class Reporter could not create file " + out.getAbsolutePath()); return; } cr.output = out; Thread thread = new Thread(cr); thread.setDaemon(true); thread.start(); Thread shutdown = new Thread() { public void run() { System.out.println("Class Reporter writing final report"); cr.running = false; cr.report(); System.out.println("Class Reporter done"); } }; Runtime.getRuntime().addShutdownHook(shutdown); } } </code></pre> <p>To turn the class into an agent, you need to package it specially in a JAR file with an appropriate Manifest. The Manifest is:</p> <pre><code>Premain-Class: ClassReporter </code></pre> <p>You then create the agent jar with this command:</p> <pre><code>jar cvfm cr.jar Manifest.mf ClassReporter*.class </code></pre> <p>and then to actually use it, you run the application specifying the agent on the command-line like this:</p> <pre><code>java -javaagent:cr.jar=cr.txt ActualProgramMainClass </code></pre> <p>The "cr.txt" is the output file generated. You will see output on System.out saying the reporter is running. The output file shows all the information you mention in your question.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload