Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

วิธีเปลี่ยน Editor เริ่มต้นของ JShell ใน Java 9

JShell implement REPL (Read-Evaluate-Print Loop) ที่อ่านโค้ดจาก command-line ประเมิน snippet ที่ให้มา และพิมพ์ผลลัพธ์กลับมา

ใน JShell สามารถแก้ไขโค้ดจาก default JShell editor ได้โดยใช้ JShell Editor Pad เราสามารถใช้คำสั่ง "/set" เพื่อเปลี่ยน default editor เพื่อกำหนด editor อื่นได้ เมื่อใช้คำสั่ง "/edit" editor นั้นจะถูกใช้งาน ในการทำเช่นนี้ ให้พิมพ์คำสั่ง "/set editor [editor]"

สมมติว่าต้องการตั้งค่าแอปพลิเคชัน Notepad เป็น default editor ให้พิมพ์คำสั่ง: "/set editor notepad.exe"

jshell> /set editor | /set editor -default
jshell> int i = 20
i ==> 20
jshell> double j = 30.0
j ==> 30.0
jshell> /set editor notepad.exe | Editor set to: notepad.exe
jshell> /edit

ตอนนี้ หากต้องการแก้ไขโค้ดที่พิมพ์ใน command prompt โดยใช้คำสั่ง "/edit" จะเปิดแอปพลิเคชัน notepad ขึ้นมา

วิธีเปลี่ยน Editor เริ่มต้นของ JShell ใน Java 9

หากเราตั้งค่า JShell editor เป็นโปรแกรมที่ ไม่มีอยู่จริง หรือ path ผิด JShell จะไม่แสดง error message เมื่อเรารันคำสั่ง "/edit" เราจะได้ error message เฉพาะตอนพยายามเปิด editor เท่านั้น

jshell> /set editor emacs | Editor set to: emacs
jshell> /set editor | /set editor emacs
jshell> /edit | Edit Error: process IO failure: Cannot run program "emacs": CreateProcess error=2, The system cannot find the file specified