Script Java code


Do you like the bath, everyone? I like it. It's a good time to soak in a warm bath and relax. It's strange, and when you're doing something else apart from programming, you have something to do with programming. This was also the case. One day, when I was dumbfounded in the bath as usual, it suddenly flashed. "Is it possible for JShell to read a file in which Java code is written and execute its contents?" "If it can be done, can it execute Java code like a script?" hand.

It seems that there are people in the world who program while taking a bath, but unfortunately I haven't reached that level, so I can't try it right away. I couldn't help it, so I left the bathroom early, being conscious of not forgetting the flash.


Put on your loungewear, open your MacBook, and google with keywords like that. As expected, JShell seems to be able to read a file with Java code and execute its contents. Try immediately. Since JShell is used, the following samples can only be executed with Java 9 or higher. I don't know.



System.out.println("Kitty on your lap")

Since it is a JShell, this is all you need to do with the code. No need for a semicolon. / ex is the end command of JShell, and without it, it will be in the state of interactive execution of JShell.


jshell test.jsh

All you have to do is pass the filename to the jshell command. It took a while for it to run after I pressed Enter </ kbd>, but it just displayed "Kitty on your lap". It is wonderful. The extension of the file is .jsh following the reference link below, but it seems that other extensions will also work. You can use .java if you like. (It's confusing, but ...)


By the way, since this execution method uses JShell, the merit of JShell itself should be the merit of this execution method as it is. In other words, it has the following merits. (Excluding the benefits of running directly on JShell, such as tab completion)

It is wonderful. (vocabulary) My chest, which was already burning after taking a bath, became even hotter.

Try a little more

The above 4th and 5th merits cannot be confirmed in the above sample, so I will write another example. Code that reads a text file and displays the lines that contain "hoge".


try(Stream<String> lines = Files.lines(Paths.get("test.txt"))) {
    lines.filter(line -> line.contains("hoge"))

You can use Stream, Files, and Paths without explicitly importing them. Also, Files.lines () can throw an IOException, but it works without writing any code to handle the IOException. It's pretty nice to write short for Java. However, in this example, it seemed that the semicolon could not be omitted. (An error, probably an internal compile-time error)


As my body got colder, so did my head.

Although it is short for Java and can be executed without explicit compilation, it is not comparable to scripting languages such as Python and Ruby in terms of code shortness and ease of execution. Also, it seems better to read the reference link below for details, but it is disadvantageous in terms of performance compared to executing it with the normal java command.

When that happens, I can't think of many use cases. In most cases, it's better to use a scripting language, and if you want to run a snippet for a quick Java operation check, it's often sufficient to type directly in a JShell. Can it be used by people who can only write Java or who are particular about Java and want to write scripts? The big disadvantage is that it can only cover such niche demand ...


Reference link


  • If you have Java 11 or above, you can directly execute normal Java source with the java command.
  • It is dangerous to sleep while soaking in the bath. If you are tired, be careful
  • It is dangerous to program while soaking in the bath. If you do it at your own risk

Recommended Posts

Script Java code
Java code TIPS
Java sample code 02
Java sample code 03
Java sample code 04
Java sample code 01
Java character code
Digital signature sample code (JAVA)
Java parallelization code sample collection
Java test code method collection
[Windows] Java code is garbled
Java in Visual Studio Code
Write Java8-like code in Java8
Guess the character code in Java
Code Java from Emacs with Eclim
Java Spring environment in vs Code
Java 15 implementation and VS Code preferences
Java 9 new features and sample code
[Java] Boilerplate code elimination using Lombok
Java build with mac vs code
Arbitrary string creation code by Java
Execute packaged Java code with commands
Java source code reading java.lang.Math class
[Java] Boilerplate code elimination using Lombok 2
Run tomcat shell script on java8
Java development environment (Mac, VS Code)
[Android] Convert Android Java code to Kotlin
Sample code using Minio from Java
Basic structure of Java source code
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
Prepare Java development environment with VS Code
Java array
Studying Java ―― 9
Java scratch scratch
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
Link Java and C ++ code with SWIG
java reflexes