🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this tutorial, we will learn how to work with import statements using JShell. We will also see how to import third-party library imports using JShell.
The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code. JShell is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results.
Complete the Java JShell tutorial at https://www.javaguides.net/2022/06/java-shell-jshell-tutorial.html
Using Java Default Imports
By default, jshell imports some common packages on startup. You can type /imports command to see all the available imports:jshell> /imports
| 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.*
For example, we can use java.lang.String class methods using JShell:
jshell> "java" + "guides"
$13 ==> "javaguides"jshell> "java".equals("java")
$14 ==> truejshell> "java guides".substring(0, 4)
$15 ==> "java"Java Import Different Package
In addition to the Java API packages, you can import other packages to use their types in JShell.jshell> import java.time.*
jshell> System.out.println(LocalDate.now())
2022-06-08
Use again /imports commands to see all the imports:
jshell> /imports
| 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.*
| import java.time.*
Import Third-Party Packages
In order to import the third-party packages, you need first to use JShell’s /env-class-path command to add the packages to JShell’s CLASSPATH, which specifies where the additional packages are located. You can then use import declarations to experiment with the package contents in JShell.Let’s add the Person class to JShell’s CLASSPATH. Also, I have downloaded a Gson library JAR file to add to CLASSPATH too so you can use the Gson class to generate a JSON-formatted text from the person class instance.
public class Person {
private String name;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
}Use the following command to add packages and libraries to the current JShell session’s CLASSPATH:jshell> /env -class-path ./lib/gson-2.8.5.ja
| Setting new options and restoring state.
Conclusion
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment