{"id":2005,"date":"2018-08-17T13:30:40","date_gmt":"2018-08-17T13:30:40","guid":{"rendered":"http:\/\/capture.ccio.us\/?p=2005"},"modified":"2018-08-17T13:30:40","modified_gmt":"2018-08-17T13:30:40","slug":"list-loaded-classes-jvm-nashorn-javascript","status":"publish","type":"post","link":"https:\/\/capture.club\/portal\/2018\/08\/17\/list-loaded-classes-jvm-nashorn-javascript\/","title":{"rendered":"List All Loaded Classes in the Java JVM Using Nashorn JavaScript"},"content":{"rendered":"<body><p><\/p><img decoding=\"async\" class=\"aligncenter wp-image-2014 size-medium\" src=\"http:\/\/capture.ccio.us\/wp-content\/uploads\/2018\/08\/Screenshot-2018-08-17-07.16.23-300x169.png\" alt=\"Java Virtual Machine\" width=\"300\" height=\"169\" loading=\"lazy\">\n<p>\u00a0<\/p>\n<p>As I was working through an issue the other day, at one point I wanted to see what was loaded in the Java Virtual Machine (JVM).\u00a0 To make matters a bit more tricky, I had to accomplish this using Nashorn JavaScript.\u00a0 So after a bit of poking around, I found this in Java:<\/p>\n<blockquote>\n<pre><code>\nimport java.lang.reflect.Field;\nimport java.util.Iterator;\nimport java.util.Vector; \n\npublic class ListLoadedClasses {\n   private static Field ClassLoader_classes_field = null;\n    private static Iterator list(ClassLoader CL)\n            throws NoSuchFieldException, SecurityException,\n            IllegalArgumentException, IllegalAccessException {\n        Class CL_class = CL.getClass();\n        while (CL_class != java.lang.ClassLoader.class) {\n            CL_class = CL_class.getSuperclass();\n        }\n       ClassLoader_classes_field = CL_class\n                .getDeclaredField(\"classes\");\n        ClassLoader_classes_field.setAccessible(true);\n        Vector classes = (Vector) ClassLoader_classes_field.get(CL);\n        return classes.iterator();\n    }\n\n    public static void main(String args[]) throws Exception {\n        ClassLoader myCL = Thread.currentThread().getContextClassLoader();\n        while (myCL != null) {\n            System.out.println(\"ClassLoader: \" + myCL);\n            for (Iterator iter = list(myCL); iter.hasNext();) {\n                System.out.println(\"\\t\" + iter.next());\n            }\n            myCL = myCL.getParent();\n        }\n    }\n}\n<\/code><\/pre>\n<\/blockquote>\n<p>So in the above snippet, you can see that we\u2019re getting the ClassLoader objects from the head, and iterating over their respective loaded classes. All-in-all, pretty straight forward.<\/p>\n<h2>JavaScript, For the Win, again, again<\/h2>\n<p>But what if we didn\u2019t have access to a Java environment? What if we were working in, say, a Nashorn JavaScript environment and wanted to do this same thing. We can accomplish that by porting the above code over to Nashorn, which looks like this:<\/p>\n<blockquote>\n<pre><code>\nvar listLoadedClasses = function(doc){\n    var e = java.lang.Exception;\n    var Class = java.lang.Class;\n    var Thread = java.lang.Thread;\n    var ClassLoader = java.lang.ClassLoader;\n    var System = java.lang.System;\n    var Iterator = java.util.Iterator;\n    \n    try{\n         var myCL = Thread.currentThread().getContextClassLoader();\n         var str = \"\";\n         while (myCL !== null) {\n            str += \" ClassLoader: \" + myCL +\" \\n\";\n            for (var iter = list(myCL); iter.hasNext();) {\n                str += \" \\t\" + iter.next() + \" \\n\";\n            }\n            myCL = myCL.getParent();\n        }\n        logger.info(str);\n    }catch(e){\n        \n    }\n    \n    function list(CL){\n        var ex = java.lang.Exception;\n        var Vector = java.util.Vector;\n        var ClassLoader_classes_field = java.lang.reflect.Field;\n        try{\n            var CL_class = CL.getClass();\n        while (CL_class != java.lang.ClassLoader.class) {\n            CL_class = CL_class.getSuperclass();\n        }\n        ClassLoader_classes_field = CL_class.getDeclaredField(\"classes\");\n        ClassLoader_classes_field.setAccessible(true);\n        var classes =  ClassLoader_classes_field.get(CL);\n        return classes.iterator();\n        }catch(ex){\n            \n        }\n    }\n    \n    return doc;\n}\n\n<\/code><\/pre>\n<\/blockquote>\n<p>So it looks a bit different, but the end result is exactly the same.<\/p>\n<p>\u00a0<\/p>\n<p>Ultimately, there is really nothing you can\u2019t do with Nashorn, and that is what makes it so cool.<\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>\u00a0 As I was working through an issue the other day, at one point I wanted to see what was loaded in the Java Virtual Machine (JVM).\u00a0 To make matters a bit more tricky, I had to accomplish this using Nashorn JavaScript.\u00a0 So after a bit of poking around, I found this in Java: import [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[],"tags":[],"class_list":["post-2005","post","type-post","status-publish","format-standard","hentry"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/posts\/2005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/comments?post=2005"}],"version-history":[{"count":0,"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/posts\/2005\/revisions"}],"wp:attachment":[{"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/media?parent=2005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/categories?post=2005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/capture.club\/portal\/wp-json\/wp\/v2\/tags?post=2005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}