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.
code
test.jsh
System.out.println("Kitty on your lap")
/ex
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.
Run
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)
javac
It is wonderful. (vocabulary) My chest, which was already burning after taking a bath, became even hotter.
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".
test2.jsh
try(Stream<String> lines = Files.lines(Paths.get("test.txt"))) {
lines.filter(line -> line.contains("hoge"))
.forEach(System.out::println);
}
/ex
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 ...
http://d.hatena.ne.jp/bitter_fox/20160703/1467577784
java
command.Recommended Posts