title: Read data from a website using Nashorn
body: <p>I want to retrieve data from a website using Nashorn script engine</p>
  
  <p>I have the java code where I can retrieve data from a sample website template.</p>
  
  <p>Now I want to call that java file from java script file.</p>
  
  <p>following is the code:</p>
  
  <p><strong>JAVA CODE(Nsample.java):</strong></p>
  
  <pre><code>package sample;
  import java.net.*;
  import java.io.*;
  
  
  public class Nsample
  {
  public static void main(String[] args)
  {
  String output  = getUrlContents("https://freewebsitetemplates.com/");
  System.out.println(output);
  }
  
  
  
  public static String getUrlContents(String theUrl)
  {
  StringBuilder content = new StringBuilder();
  
  
  try
  {
  
    URL url = new URL(theUrl);
  
  
    URLConnection urlConnection = url.openConnection();
  
  
    BufferedReader bufferedReader = new BufferedReader(new 
    InputStreamReader(urlConnection.getInputStream()));
  
    String line;
  
  
    while ((line = bufferedReader.readLine()) != null)
    {
      content.append(line + "\n");
    }
     bufferedReader.close();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    return content.toString();
    }
   }
  </code></pre>
  
  <p><strong>JAVASCRIPT code:(sample.js)</strong></p>
  
  <pre><code>var n = Java.type('C.JavaFolder.sample.Nsample');
  
  var result = n.getUrlContents("https://freewebsitetemplates.com/");
  print(result);
  </code></pre>
  
  <p>I'm trying to compile javascript code using command prompt but it is showing CLASSNOTFOUNDEXCEPTION.</p>
  
  <p>The command was <strong>jjs sample.js</strong>.Im assuming I did some mistake in Java.type() function.</p>
  
  <p>Can anyone solve this?</p>