java.py
is a python script allowing you to execute java code quickly without opening an IDE
or typing all the boilerplate code:
$ java.py 'Stream.of(1, 2, 3, 4).map(Integer::toBinaryString)'
>> (java.util.stream.ReferencePipeline$3) ["1", "10", "11", "100"]
The result of the last instruction is displayed:
$ java.py 'Scanner s = new Scanner(System.in); s.nextLine()' <<< 'hello world!'
>> (String) hello world!
The collection/array formatting allows you to quickly identify contained types:
$ java.py "new Object[]{1, \"a\", null, 'a'}"
>> (Object[]) [1, "a", null, 'a']
$ java.py 'Set<String> s = new HashSet<>(); s.add("hello"); s.add("world!"); s'
>> (java.util.HashSet) ["hello", "world!"]
Missing an import? You can add content at the start of the generated file with -i
:
$ java.py -i java.time.Instant 'Instant.now()'
>> (java.time.Instant) 2016-02-29T12:44:54.870Z
Want a pretty output? Use the -p
flag:
$ java.py -p 'Random r = new Random(); Stream.generate(r::nextInt).limit(5)'
>> (java.util.stream.SliceOps$1) [1716066362,
>> 403370638,
>> 653951165,
>> -2063181131,
>> -862370509]
Curious about the generated bytecode for some code? Use the -b flag:
$ java.py -b 'String a = "a", b = a + "b"'
>> Compiled from "Paul.java"
>> public class Paul {
>> public static void main(java.lang.String[]) throws java.lang.Exception;
>> Code:
>> 0: ldc #2 // String a
>> 2: astore_1
>> 3: new #3 // class java/lang/StringBuilder
>> 6: dup
>> 7: invokespecial #4 // Method java/lang/StringBuilder."<init>":()V
>> 10: aload_1
>> 11: invokevirtual #5 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
>> 14: ldc #6 // String b
>> 16: invokevirtual #5 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
>> 19: invokevirtual #7 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
>> 22: astore_2
>> 23: return
>> }
Requires java 8 and python 3 to work.