Showing posts from 2015

Running Java in a Script

Recently I wanted to invoke a JMX operation in a WSO2 server from a script. I was first thinking of writing a simple Java class with a main method and run it as an executable jar file.

Then I found that we can run a script with the command "jrunscript".

I also found an example of interacting with a JMX MBean from Javascript. However this example didn't work with Java 8.

The reason is that from Java 8, the JavaScript Engine for the JVM is Oracle Nashorn. In Java 7, the JavaScript Engine is based on Mozilla Rhino.

Let's see the difference:

Following script works with Java 7, but not with Java 8

packages = new JavaImporter(java.lang, java.lang.reflect); with (packages) { // create Java String array of 5 elements var a = Array.newInstance(String, 5); // Accessing elements and length access is by usual Java syntax a[0] = "scripting is great!"; print(a.length); print('\n'); print(a[0]); print('\n'); }

Following is the…

Java Mixed-Mode Flame Graphs

Recently, I wrote a blog post on "Java CPU Flame Graphs". That blog post shows how to use Linux perf_events to generate Java Mixed-Mode Flame Graphs.

With Java Profilers, we can get information about Java process only. However with Java Mixed-Mode Flame Graphs, we can see how much CPU time is spent in Java methods, system libraries and the kernel. Mixed-mode means that the Flame Graph shows profile information from both system code paths and Java code paths.

This is sometimes very important to identify performance issues. For more information, see Java in Flames and New JVM Option Enables Generation of Mixed-Mode Flame Graphs

In this blog post, I'm using a sample high cpu consuming Java Program with Java 8 Update 60.

$ mvn -version Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T17:27:37+05:30) Maven home: /usr/local/apache-maven/apache-maven-3.3.3 Java version: 1.8.0_60, vendor: Oracle Corporation Java home: /usr/lib/jvm/jdk1.8.0_60/jre Default l…