{"id":759,"date":"2019-03-25T10:42:48","date_gmt":"2019-03-24T21:42:48","guid":{"rendered":"https:\/\/www.ronella.xyz\/?p=759"},"modified":"2021-09-28T14:00:32","modified_gmt":"2021-09-28T01:00:32","slug":"using-jshell-for-testing-an-api","status":"publish","type":"post","link":"https:\/\/www.ronella.xyz\/?p=759","title":{"rendered":"Using JShell for testing an API"},"content":{"rendered":"<form id=\"aspnetForm\" action=\"\/Workspace\/BusinessFunctions\/ApplicationsDevelopment\/ADWiki\/Pages\/Using%20JShell%20for%20testing%20an%20API.aspx\" method=\"post\" name=\"aspnetForm\">\n<div id=\"s4-workspace\">\n<div id=\"s4-bodyContainer\">\n<div id=\"s4-mainarea\" class=\"s4-pr s4-widecontentarea\">\n<div id=\"MSO_ContentTable\" class=\"s4-ca s4-ca-dlgNoRibbon\">\n<div class=\"s4-ba\">\n<div class=\"ms-bodyareacell\">\n<div id=\"ctl00_MSO_ContentDiv\">\n<div style=\"padding: 11px 0px 0px 0px;\">\n<div class=\"col-fluid-1\">\n<div class=\"col-fluid-2\">\n<div class=\"edit-mode-border\">\n<div id=\"ctl00_PlaceHolderMain_PageContent__ControlWrapper_RichHtmlField\" class=\"ms-rtestate-field\" style=\"display: inline;\" aria-labelledby=\"ctl00_PlaceHolderMain_PageContent_label\">\n<p><strong>Java 9<\/strong> was released and <strong>JShell<\/strong> <span style=\"font-style: italic;\">(i.e. The REPL of Java)<\/span> is now real and we can use this to test an API.<\/p>\n<div>For example if we want to check the reverse function <strong>StringUtils<\/strong> from the <span style=\"font-weight: bold;\">commons-lang3-3.1.jar<\/span>.<\/div>\n<div><\/div>\n<div>1.) Using windows terminal (i.e. cmd command).<\/div>\n<div>2.) From this directory execute to following command jshell command.<\/div>\n<div><\/div>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\">\n<pre>jshell --class-path .\\commons-lang3-3.1.jar<\/pre>\n<\/div>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\">Expect to see something similar to the following:<\/div>\n<div><\/div>\n<div>\n<pre style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\">Welcome to JShell -- Version 9.0.1\r\nFor an introduction type: \/help intro\r\n\r\njshell&gt;<\/pre>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\"><\/div>\n<div>3.) From the jshell prompt import the class <span style=\"font-weight: bold;\">org.apache.commons.lang3.StringUtils<\/span> just like we normally do in java and then press enter.<\/div>\n<div><\/div>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\">e.g.<\/div>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\"><\/div>\n<pre style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\">jshell&gt;<span style=\"font-weight: bold;\"> import org.apache.commons.lang3.StringUtils<\/span><\/pre>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\"><\/div>\n<div>4.) To optionally check all the imported classes on the <span style=\"font-weight: bold;\">current jshell session<\/span> we can use <span style=\"font-weight: bold;\">\/imports command<\/span>.<\/div>\n<div><\/div>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\">e.g.<\/div>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\"><\/div>\n<pre style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\">jshell&gt; <span style=\"font-weight: bold;\">\/imports<\/span><\/pre>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\"><\/div>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\">The sample output would be:<\/div>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\"><\/div>\n<pre style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\">|\u00a0\u00a0\u00a0 import java.io.*\r\n|\u00a0\u00a0\u00a0 import java.math.*\r\n|\u00a0\u00a0\u00a0 import java.net.*\r\n|\u00a0\u00a0\u00a0 import java.nio.file.*\r\n|\u00a0\u00a0\u00a0 import java.util.*\r\n|\u00a0\u00a0\u00a0 import java.util.concurrent.*\r\n|\u00a0\u00a0\u00a0 import java.util.function.*\r\n|\u00a0\u00a0\u00a0 import java.util.prefs.*\r\n|\u00a0\u00a0\u00a0 import java.util.regex.*\r\n|\u00a0\u00a0\u00a0 import java.util.stream.*\r\n|\u00a0\u00a0\u00a0 import org.apache.commons.lang3.StringUtils<\/pre>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\"><\/div>\n<div>5.) Since we know that the StringUtils class was already loaded. We can now execute its reverse function.<\/div>\n<div><\/div>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\">e.g.<\/div>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\"><\/div>\n<pre style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\">jshell&gt; <span style=\"font-weight: bold;\">StringUtils.reverse(\"abcde\")<\/span><\/pre>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\"><\/div>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\">The output must be:<\/div>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\"><\/div>\n<pre style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\">$2 ==&gt; \"edcba\"<\/pre>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\"><\/div>\n<div style=\"font-size: 11pt; font-family: calibri; margin: 0in 0in 0in 0.375in;\"><span style=\"font-style: italic;\">Note: if you want to exit jshell execute the <\/span>\/exit<span style=\"font-style: italic;\"> command.<\/span><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/form>\n","protected":false},"excerpt":{"rendered":"<p>Java 9 was released and JShell (i.e. The REPL of Java) is now real and we can use this to test an API. For example if we want to check the reverse function StringUtils from the commons-lang3-3.1.jar. 1.) Using windows terminal (i.e. cmd command). 2.) From this directory execute to following command jshell command. jshell [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[43,71],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/759"}],"collection":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=759"}],"version-history":[{"count":5,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/759\/revisions"}],"predecessor-version":[{"id":764,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/759\/revisions\/764"}],"wp:attachment":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}