
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to import external libraries in JShell in Java 9?
JShell is an interactive tool to learn Java language and prototyping Java code. JShell does the work by evaluating the commands that user types into it. This tool works on the principle of REPL (Read-Evaluate-Print-Loop).
By default, JShell automatically imports a few useful java packages when the JShell session is started. We can type the command /imports to get a list of all these 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 javax.mail.internet.InternetAddress
We can also import external libraries in JShell by using the below steps:
If we want to create an InternetAddress object that resides in the javax.mail.internet package, then we need to import that package in JShell.
jshell> import javax.mail.internet.InternetAddress | Error: | package javax.mail.internet does not exist | import javax.mail.internet.InternetAddress; | ^---------------------------------^
In the above, just importing the class doesn't work because the package is unknown to the classpath. We need to add jars or class files to classpath by using the command: "/env –class-path <jars, class files>"
jshell> /env --class-path \Users\user\mail-1.4.7.jar | Setting new options and restoring state. jshell> import javax.mail.internet.InternetAddress
Finally, we can create an InternetAddress object by using below
jshell> InternetAddress from = new InternetAddress("a@a") from ==> a@a
- Related Articles
- How can we import a gson library in JShell in Java 9?\n
- What are the rules for external declarations in JShell in Java 9?
- How to debug JShell in Java 9?
- JShell in Java 9?
- How to get JShell documentation in Java 9?
- How to create JShell instance programmatically in Java 9?
- How to reset the JShell session in Java 9?
- How to implement java.time.LocalDate using JShell in Java 9?
- How to create a thread in JShell in Java 9?
- How to handle an exception in JShell in Java 9?
- How to create scratch variables in JShell in Java 9?
- How to implement a String in JShell in Java 9?
- How to create wrapper objects in JShell in Java 9?
- How to initialize an array in JShell in Java 9?
- How to declare reference types in JShell in Java 9?
