Execute Java code stored on the clipboard.

Introduction

Have you ever wanted to try a little Java program? image.png

From Java9, JShell has been added as a REPL tool. With Java11, you can now execute Java files without the previous javac. Also, the online execution environment such as paiza.IO is convenient.

It's definitely easy to do. However, I wonder if I can try the program more easily ... Saya: bangbang: I wish I could execute the Java code stuck on the clipboard ...: point_up:

So I came up with the title executing Java code stored on the clipboard </ b> </ font>.

I searched the internet lightly, but as far as I searched, it didn't come out, so I will try to make it. (I don't need it in the first place, so it doesn't come out ...: poop: It smells something anymore ...: poop: is aside.)

I made it

As a mechanism, it is a simple mechanism to get a character string from the clipboard and pass the character string to jshell. By the way, the following packages are imported to JShell by default.

jshell> /import
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.stream.*

First, create a part to get the character string from the clipboard. Anything is fine, but I wrote it in Java.

PrintClipboardString.java


import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
/**
 *Outputs character string data from the clipboard.
 */
public class PrintClipboardString {
    /**
     *Main
     */
    public static void main(String[] args) {
        System.out.println(getClipboardString());
    }
    /**
     *Returns string data from the clipboard.
     * 
     * @return Returns the string data of the clipboard. Returns null if it is not string data.
     */
    public static String getClipboardString() {
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();

        try {
            return (String) clip.getData(DataFlavor.stringFlavor);
        } catch (UnsupportedFlavorException e) {
            return null;
        } catch (IOException e) {
            return null;
        }
    }
}

Next, create a batch file (DOS) that uses this PrintClipboardString.java. It's the role of getting the string data from the clipboard and passing it to JShell.

clipbord_jshell.bat


@echo off
setlocal enabledelayedexpansion

set CLIP_BOARD=
for /f "usebackq tokens=*" %%i in (`java PrintClipboardString`) do (
    set CLIP_BOARD=!CLIP_BOARD!^
%%i
)
echo =============
echo source
echo =============
echo !CLIP_BOARD!
echo.

echo =============
echo execution result
echo =============
echo !CLIP_BOARD! | jshell -
echo.

pause

Since this is DOS, the for statement is used in consideration of the part that handles the command execution result, but I think that it is easy because backticks can be used normally with ʻUnix shell`.

I tried using it

1. I tried to output a character string.

System.out.println("hello"); ↑ Copy this text (store it in the clipboard) and execute the batch file.

$clipbord_jshell.bat
=============
Source
=============
System.out.println("hello");

=============
Execution result
=============
hello

Press any key to continue. . .

Good feeling: sunny:

2. I tried using the Math class.

System.out.println(Math.random()); System.out.println(Math.random()); System.out.println(Math.random());

↑ Copy these 3 lines of text (store them in the clipboard) and execute the batch file.

$clipbord_jshell.bat
=============
Source
=============
System.out.println(Math.random());System.out.println(Math.random());System.out.println(Math.random());

=============
Execution result
=============
0.13619188697681628
0.6036857063092008
0.7567411771330568

Press any key to continue. . .

The source is displayed on one line, but the execution result is good even with multiple lines. Since java.math. * Has already been imported in JShell, it could be executed without importing separately.

3. I counted a few more days until Christmas.

I think that there are many cases where you want to try the date operation system relatively.

import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; LocalDateTime d = LocalDateTime.now(); LocalDateTime c = LocalDateTime.of(2018, 12, 25 , 23, 59); System.out.println ("until Christmas ..." + ChronoUnit.DAYS.between (d, c) + "day"); ↑ Copy 5 lines of this text (store it in the clipboard) and execute the batch file.

$clipbord_jshell.bat
=============
Source
=============
import java.time.LocalDateTime;import java.time.temporal.ChronoUnit;LocalDateTime d = LocalDateTime.now();LocalDateTime c = LocalDateTime.of(2018, 12,
 25 , 23, 59);System.out.println("Until Christmas ..." + ChronoUnit.DAYS.between(d, c) + "Day");

=============
Execution result
=============
20 days until Christmas

Press any key to continue. . .

Deketa: relaxed:

in conclusion

-It seems that it can be used to execute a program (completed program) copied from the Internet. ・ It is troublesome to have to output standard output (System.out ~) one by one. -It is easy to mistakenly copy and execute something other than Java code. Get angry: no_good_tone3: -If a line comment is inserted in the middle, the following is regarded as a comment and the logic is not evaluated at all. (Is there room for improvement here?) ・ I think it's a garbage tool, but my child is cute: poop: ・ Is security okay? by infrastructure engineer ·I was not reflecting.

That's Merry Christmas: christums_tree:

Recommended Posts