nudge4j: GET INSIDE YOUR JVM
nudge4j is a tiny piece of java code to make your java application accessible to the browser. It's meant for use during development to provide an environment for experimenting with code against a running application.

Requirements

Integration

The way it works

Examples

The following examples are ready to be sent (copy/paste) into your N4J console.
  1. System.out
  2. JVM uptime
  3. java 8 new Date/Time API
  4. Thread Dump
  5. Memory
  6. Binary Numbers
  7. slf4j
  1. // System.out/System.err.println
    java.lang.System.out.println("a message in System.out");
    java.lang.System.err.println("a message in System.err");
    'check your JVM log'
    
  2. // JVM Uptime
    var String =            java.lang.String; 
    var Integer =           java.lang.Integer;
    var ManagementFactory = java.lang.management.ManagementFactory; 
    
    var rt = ManagementFactory.getRuntimeMXBean();
    var seconds = Math.floor(rt.getUptime() / 1000);
    var s = seconds % 60;
    var m = Math.floor(seconds / 60) % 60;
    var h = Math.floor(seconds / (60 * 60)) % 24;
    var hhmmss = String.format("%02d:%02d:%02d", new Integer(h), new Integer(m), new Integer(s));
    
    'JVM uptime ['+hhmmss+']';
    
  3. // An example of java 8 new Date/Time API.
    var LocalDate =  Java.type('java.time.LocalDate');
    var Month =      Java.type('java.time.Month'); 
    var ChronoUnit = Java.type('java.time.temporal.ChronoUnit'); 
    
    var today = LocalDate.now();
    var firstDayThisYear = LocalDate.of(today.getYear(), Month.JANUARY, 1);
    
    var days = ChronoUnit.DAYS.between(firstDayThisYear, today);
    days + " days have passed since the beginning of the year."
    
  4. // Thread Dump
    var threads = java.lang.Thread.getAllStackTraces().keySet().toArray();
    var str ='';
    for (var i=0;i<threads.length;i++) {
      str+=threads[i]+' - '+threads[i].getState()+'\n';
    }
    str;
    
  5. // Memory
    var mem = java.lang.management.ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
    var usd = Math.round( mem.getUsed() / 1048576 )
    var cmt = Math.round( mem.getCommitted()  / 1048576 )
    var max = Math.round( mem.getMax() / 1048576 )
    
    'Used:'+usd+'MB, Committed:'+cmt+'MB, Max:'+max+'MB';
    
    
  6. // Binary Numbers
    var str ='';
    for (var i=0;i<1000;i++) {
      str+=i+'='+java.lang.Integer.toBinaryString(i)+'\n'; 
    }
    str;
    
  7. // SLF4J - requires http://www.slf4j.org/
    var LoggerFactory = Packages.org.slf4j.LoggerFactory;
    var logger = LoggerFactory.getLogger('n4j');
    logger.info("Hello n4j (info)");
    logger.warn("Hello n4j (warn)");
    logger.error("Hello n4j (error)");
    
    
    

Notes

[1] internet access:
Some resources are accessed on demand. In particular, you need to be able to access:
[2] copy/paste this code anywhere:
Strictly speaking, you need to paste the snippet some place where a thread will be executing it.
[3] ... and connect:
if you have already embedded the code in your java application, you can try it now.
[4] N4J.start(int port,Object args...)
[5] one html page
The html page (console) is really a place holder for an external javascript which generates the full page. This allows to keep the jar size to a minimum
Nudge4J
Copyright © 2016 - Lorenzo Puccetti
Fork me on GitHub