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

How to import external library in JShell in Java 9?

WBOY
Release: 2023-09-05 20:29:02
forward
671 people have browsed it

如何在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>
Copy after login

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>
Copy after login

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>
Copy after login

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

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template