Home  >  Article  >  Java  >  How to import external library in JShell in Java 9?

How to import external library in JShell in Java 9?

WBOY
WBOYforward
2023-09-05 20:29:02604browse

如何在Java 9的JShell中导入外部库?

JShell is an interactive tool for learning the Java language and prototyping Java code. JShell does its work by evaluating commands entered by the user. The working principle of this tool is REPL(Read-Evaluate-Print-Loop).

By default, JShell will automatically import some useful java packages when JShell is running. The session begins. We can enter the command /imports to get a list of all these imports.

<strong>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</strong>

We can also import external libraries by using JShellThe steps are as follows:

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.

<strong>jshell> import javax.mail.internet.InternetAddress
| Error:
| package javax.mail.internet does not exist
| import javax.mail.internet.InternetAddress;
| ^---------------------------------^</strong>

In the above, just importing the class won't work because the package is unknown 强> to the classpath. We need to add jars or classfiles to classpath using the following command: "/env –class-path

<strong>jshell> /env --class-path \Users\user\mail-1.4.7.jar
| Setting new options and restoring state.

jshell> import javax.mail.internet.InternetAddress</strong>

Finally, we can create an InternetAddress object## using the following method #

<strong>jshell> InternetAddress from = new InternetAddress("a@a")
from ==> a@a</strong>

The above is the detailed content of How to import external library in JShell in Java 9?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete