A very crude interactive Java shell
Binary: ijava.class (4k)
Source: ijava.java (2.5k)
This REPL evaluates Java expressions interactively, as illustrated:
C:\Users\daniel>ijava
>>> 2 + 3
5
>>> Math.cos(Math.PI)
-1.0
>>> Integer.MIN_VALUE * -1 // underflow
-2147483648
>>> new Integer(42).hashCode()
42
>>> System.getProperty("java.vm.name")
Java HotSpot(TM) Client VM
How does the shell work? It’s a bit of a hack, to say the least! Essentially, it
- reads an expression from standard input
- surrounds the expression with some template code
- writes the code to a temporary file
- runs javac on that file
- dynamically loads the class
- invokes a method in the new class using reflection
- prints the object returned by this method
Posted in Java at April 20th, 2011. No Comments.