Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Cách lấy phiên bản Java, OS, vendor trong JShell Java 9

Java 9 giới thiệu JShell – công cụ REPL cho phép đánh giá snippet code (khai báo, câu lệnh, biểu thức). Ta có thể lấy thông tin Java Version, Vendor, OS Version, OS Name bằng phương thức static System.getProperty().

Ví dụ trong JShell

C:\Users\User> jshell
| Welcome to JShell -- Version 9.0.4
jshell> System.out.println(System.getProperty("java.version"));
9.0.4
jshell> System.out.println(System.getProperty("java.vendor"));
Oracle Corporation
jshell> System.out.println(System.getProperty("os.version"));
6.3
jshell> System.out.println(System.getProperty("os.name"));
Windows 8.1
jshell>

Các property thường dùng

Property KeyMô tảVí dụ giá trị
java.versionPhiên bản Java runtime9.0.4, 11.0.2, 17.0.1
java.vendorNhà cung cấp JDKOracle Corporation, Eclipse Adoptium
java.homeThư mục cài đặt JREC:\Program Files\Java\jdk-9.0.4
os.nameTên hệ điều hànhWindows 10, Linux, Mac OS X
os.versionPhiên bản OS10.0, 5.15.0-91-generic
os.archKiến trúc CPUamd64, aarch64
user.nameTên user đăng nhậpadmin, nguyenvana
user.dirThư mục làm việc hiện tạiC:\Projects\MyApp

Trong JShell, chỉ cần gõ System.getProperty("key") để tra cứu nhanh mà không cần viết chương trình hoàn chỉnh.