Hackernoon logoThe one and only Java tool shipped without a jar by@yolpogists

The one and only Java tool shipped without a jar


nudge4j ships without a jar

Copy/Paste this bit of code and we will add some awesome functionality to your Java program.

Have you ever come across web sites making a similar claim ?

No. I don’t think so.

We, Java developers, don’t copy/paste bits of code.

Except for nudge4j, of course. But that’s the odd case.

Instead, web developers are quite used to this.

Web developers know that they can integrate functionality from third-parties by copying/pasting a tiny script into their HTML pages.

Some examples are:

It’s a great engineering work: by adding a few lines you end up with pages with conversations (disqus), with web analytics (google analytics) and with social buttons (addthis).

There is more to it: disabling/enabling the added functionality then becomes as easy as commenting/uncommenting their snippets of code.

In Java, we just don’t do it like that.

And, to be honest, there are clearly some good reasons.

  • Java applications don’t run in a sandboxed browser
  • Java is a strongly typed language and API customization is easier when the jar is known at compilation time
  • With so many established Java build tools (Ant, Maven, Graddle) third-party library creators are too busy integrating with them, to have time to even consider a different approach
  • Java is verbose. And it gets even worse if you cannot make any assumption on where your snippet is going to be. For example you ought to fully qualify every class you intend to use or wrap code inside anonymous classes/functions to avoid variable conflicts.
Copy/Paste this bit of code and we will add some awesome functionality to your Java program.

Can we really try that ?

And how can we create the smallest snippet of Java code which adds whatever functionality to the JVM ?

Below is my attempt to write the tiniest, minimalest, claustrophobically spaceless snippet ever.

Copy/Paste this tiny bit of code (less-than-200-characters) and talk to your JVM from the browser on http://localhost:5050/
try{new javax.script.ScriptEngineManager().getEngineByName("js").eval("load('http://nudge4j.appspot.com/n/n4j.js')");}catch(Exception e){throw new RuntimeException(e);}

The less-than-200-characters-of-code is

  • lazily wrapping everything in a try-catch-exception
  • creating a JavaScript engine
  • using the load function to run an external JavaScript with the required functionality

Frankly, I don’t think you can get any shorter than that.

But I’d love to be proved wrong. Let me know if you can think of a shorter way.


Join Hacker Noon

Create your free account to unlock your custom reading experience.