blob: 759f864540e61fb68bc6cefc84876ecc0e74ea70 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--
Copyright (c) 2009, 2010, 2011, Oracle and/or its affiliates. All rights reserved.
-->
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="description" content="A short guide to enabling Java Web Start for a simple Java SE application in NetBeans IDE">
<meta name="keywords" content="NetBeans, IDE, integrated development environment, Java Web Start, Java
tutorial, guide, user, documentation, open source, Java">
<link rel="stylesheet" type="text/css" href="../../../netbeans.css">
<title>Enabling Java Web Start in the NetBeans IDE</title>
<style type="text/css">
<!--
.style1 {color: #FF0000}
.style2 {color: #000000}
-->
</style>
</head>
<body>
<h1>Enabling Java Web Start in the NetBeans IDE</h1>
<p>Following the steps in this tutorial, you will learn how to configure your application so that it can be deployed using Java Web Start.
Java Web Start is a technology that is used to run Java applications from a web browser with a single click.
In short, Java Web Start is just another way that you can deploy Java applications. </p>
<p>In this tutorial you will use a simple Java application, a converter, which converts distance measurements between metric and U.S. units.
The code for this sample application will be given to you for download, and you will need to configure project settings in order to launch it using Java Web Start.
You can use any remote web server available for you to upload the application files.</p>
<p>This tutorial takes approximately <span class="style2">20</span> minutes to complete. </p>
<p><b>Contents</b></p>
<img src="../../../images_www/articles/71/netbeans-stamp-71-72-73.png" class="stamp" alt="Content on this page applies to NetBeans IDE 6.9, 7.0, 7.1, 7.2 and 7.3" title="Content on this page applies to the NetBeans IDE 6.9, 7.0, 7.1, and 7.3">
<ul class="toc">
<li><a href="#openproject" title="Project Setup">Opening a Project</a></li>
<li><a href="#configure" title="Creating and Editing Java Source Code">Configuring the Project to Enable Java Web Start</a></li>
<li><a href="#runremote">Running the Application from a Remote Location</a></li>
<li><a href="#summary" title="Testing and Debugging the Application">Summary</a></li>
<li><a href="#seealso" title="Next Steps">See Also</a></li>
</ul>
<p><b>To complete this tutorial, you need the software and resources listed in the following
table.</b></p>
<table>
<tbody>
<tr>
<th width="165" class="tblheader" scope="col">Software or Resource</th>
<th width="142" class="tblheader" scope="col">Version Required</th>
</tr>
<tr>
<td class="tbltd1"><a href="http://download.netbeans.org/">NetBeans IDE</a></td>
<td class="tbltd1">6.9, 7.0, 7.1, 7.2, 7.3 </td>
</tr>
<tr>
<td class="tbltd1"><a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">Java Development Kit (JDK)</a></td>
<td class="tbltd1"> 6 or 7 </td>
</tr>
<tr>
<td class="tbltd1"><a href="https://netbeans.org/projects/samples/downloads/download/Samples%252FJava%252FConverterPrj.zip">Converter zip archive</a></td>
<td class="tbltd1">n/a</td>
</tr>
</tbody>
</table>
<h2>Opening a Project<a name="openproject" id="openproject"></a></h2>
<p>You first need to open the Converter application in the IDE.
The application is already packaged as a NetBeans IDE project so you only need to open the project in the IDE. </p>
<p>The source code for the Converter demo is originally provided in the Java tutorial. Refer to the <a href="http://java.sun.com/docs/books/tutorial/uiswing/examples/components/index.html" target="_blank">Using Swing Components</a> section of the Java tutorial to learn how to write this small application. In this tutorial, you will learn how to configure your project settings so that this Java application can be launched in a web browser. </p>
<ol>
<li>Download the <a href="https://netbeans.org/projects/samples/downloads/download/Samples%252FJava%252FConverterPrj.zip" target="_blank">Converter demo application zip archive</a>.</li>
<li>Extract the archive to a location on your system.</li>
<li>In the IDE, choose File &gt; Open Project from the main menu. <br>
The <tt>ConverterPrj</tt> project opens in the Projects window. You can expand the project's nodes to view the source files. </li>
</ol>
<p class="align-center"><img src="../../../images_www/articles/71/java/javaws/converterprj.png" alt="Image showing the contents of the ConverterPrj." class="margin-around"></p>
<!-- =============================================================================== -->
<h2><a name="configure"></a>Configuring the Project to Enable Java Web Start</h2>
<p>With Java Web Start, the user can launch a Java application by clicking an HTML link to a <tt>JNLP</tt> file for this application inside a web browser. The <tt>JNLP</tt> file, which is a special configuration file, instructs Java Web Start to download, cache, and run the Java application. To run applications with Java Web Start, it is enough to have a compatible version of the Java Runtime Environment (JRE) installed on the client machine. The installation of the Java Development Kit (JDK) is not required. </p>
<p>To enable your Java application to run with Java Web Start, you need to configure the properties of how the IDE should build the project. When Java Web Start is enabled in project properties, the IDE automatically creates a <tt>JNLP</tt> file and an HTML page with the link to the <tt>JNLP</tt> file, together with the JAR file. </p>
<div class="indent">
<h3 class="tutorial">Configuring the Project to Enable Java Web Start </h3>
<p>In this exercise you will configure the project to make it Java Web Start enabled and test its execution locally. </p>
<ol>
<li>Right-click the <tt>ConverterPrj</tt> project node and choose Properties. </li>
<li>Under Categories, choose Web Start and select the Enable Web Start checkbox. </li>
<li>Choose the Local Execution option from the Codebase drop-down list as we will first run the application locally. <br>
The Codebase Preview field shows the path to local application files. </li>
<li>Click Customize to open the Signing dialog box. Select the <tt>self-sign by generated key</tt> option. <br>
The application JAR file will be signed by a certificate that is generated automatically when the project is built. With the self-signed certificate, the application will be able to access the same resources from the computer as a regular application that is running locally. For example, the self-signed certificate allows an application to access local files and the network. </li>
<li>Leave Enable Software Protections selected in the Mixed Code drop-down list and click OK.</li>
<li>(Optional) In the Project Properties dialog box, select the Application panel and change the application title and vendor name. </li>
<li>Click OK to close the Project Properties dialog box. </li>
</ol>
<p class="align-center">
<a href="../../../images_www/articles/71/java/javaws/prjproperties.png" rel="lytebox"
title="Image showing the properties of the ConverterPrj.">
<img src="../../../images_www/articles/71/java/javaws/prjproperties-small.png"
alt="Image showing the properties of the ConverterPrj." border="1"></a></p>
<h3>Compiling and Running the Java Web Start Application from the IDE </h3>
<p><strong>To compile and run the application to test Java Web Start locally:</strong></p>
<ol>
<li>Select the <tt>ConverterPrj</tt> project node in the Projects window and then choose Run &gt; Set Main Project &gt; ConverterPrj in the main menu. </li>
<li>Choose Run&nbsp;&gt; Run&nbsp;Main&nbsp;Project or press F6.
<br>The IDE compiles sources and you should see the splash screen that Java is starting and the warning window asking you whether the signed application can be executed. <p class="align-center">
<a href="../../../images_www/articles/71/java/javaws/warning.png" rel="lytebox"
title="Warning">
<img src="../../../images_www/articles/71/java/javaws/warning-small.png"
alt="Warning" border="1"></a></p> </li>
<li>Select the checkbox to trust the content and click Run in the warning window. <br>
The Converter application starts. <p><img src="../../../images_www/articles/71/java/javaws/converterapp.png" alt="Image showing the Converter application." class="margin-around"></p> </li>
</ol>
<h3 class="tutorial">Exploring Java Web Start Files</h3>
<p>Now let's take a closer look at the Java Web Start files that were created by the IDE during the build process (Run &gt; Build Project). </p>
<p>To view the files, open the Files window in the IDE and expand the <tt>dist</tt> folder.</p>
<p class="align-center"><img src="../../../images_www/articles/71/java/javaws/converterfiles.png" alt="Image showing the files created for the Converter application." class="margin-around"></p>
<p> For Java Web Start, the following two additional files are created: </p>
<ul>
<li><tt>launch.jnlp</tt> - This is an XML file with special elements and attributes that instruct browsers how to run the application. <tt>JNLP</tt> stands for the Java Network Launching Protocol. Attributes of <tt>JNLP</tt> files can include the <tt>JNLP</tt> spec version, application title, vendor name, a link to the application JAR file, etc. </li>
<li><tt>launch.html</tt> - This is an automatically generated HTML page that has a link to the <tt>JNLP</tt> file. Users click this link to start applications via Java Web Start. This generated HTML file also has a commented-out reference to the publicly available Java Deployment Toolkit (<a href="http://java.com/js/deployJava.js" target="_blank">deployJava.js</a>), which provides JavaScript functions in order to avoid browser compatibility issues. You can find more information about the <a href="http://download.oracle.com/javase/6/docs/technotes/guides/jweb/deployment_advice.html#deplToolkit" target="_blank">Java Deployment Toolkit </a>here. <br>
<br>
You can try doing the following: outside of the IDE, navigate to the <tt>launch.html</tt> file on your system, open it in your browser and click the link to launch the Converter demo application. </li>
</ul>
</div>
<h2><a name="runremote" id="runremote"></a>Running the Application from a Remote Location</h2>
<p>After you verified that the application starts successfully with Java Web Start from local sources,
let's upload it to a remote location and launch it from there.</p>
<p class="notes"><b>Note:</b> For deployment of applications with Java Web Start on the web,
the web server you are using should be able to handle <tt>JNLP</tt> files.
The web server must be configured to recognize <tt>JNLP</tt> files as applications, i.e. the MIME type for <tt>JNLP</tt> should be added to the configuration of the web server. Otherwise, files with the <tt>JNLP</tt> extension will be treated as usual text files. For more information about web server configuration, refer to the <a href="http://docs.oracle.com/javase/6/docs/technotes/guides/javaws/developersguide/setup.html" target="_blank">Java Web Start Guide</a>.</p>
<div class="indent">
<h3 class="tutorial">Modifying the JNLP File</h3>
<p>To launch the application from the web, you need to provide a link to the
applications source file on the web in the <tt>JNLP</tt> file. </p>
<ol>
<li>Right-click the <tt>ConverterPrj</tt> project node, choose Properties and select Web Start under Categories. </li>
<li>Choose User Defined as the Codebase.</li>
<li>In the Codebase Preview field, enter the URL where you will be uploading the source files. <br>
For example, <tt>http://mydomain.com/myuser/converter/</tt>.</li>
<li>Click OK in the Project Properties window.</li>
<li>Right-click the <tt>ConverterPrj</tt> node and choose Clean and Build. <br>
This IDE command deletes all previously compiled files and build outputs, recompiles your application,
and builds output files with current settings. </li>
</ol>
<h3 class="tutorial">Uploading the Source Files </h3>
<p>Now we will upload the source files to the server and run the application from there.
Note that all user credentials and project names referred to in this section should be replaced with your personal data. </p>
<ol>
<li>Log into the web server and navigate to the download area for your project. <br>
In our case, the project title is &quot;NetBeans IDE Documentation Area&quot;</li>
<li>Upload the following local files from your project's <tt>dist</tt> folder to the directory of your project:
<tt>ConverterPrj.jar</tt>, <tt>launch.html</tt>, and <tt>launch.jnlp</tt>.
<p>The figure below shows the download area for the NetBeans IDE Documentation Area project. </p>
<p class="align-center">
<a href="../../../images_www/articles/71/java/javaws/download.png" rel="lytebox"
title="Image showing the download area for the NetBeans IDE Documentation Project">
<img src="../../../images_www/articles/71/java/javaws/download-small.png"
alt="Image showing the download area for the NetBeans IDE Documentation Project" border="1"></a></p></li>
<li>Now, run your application. In a browser window, enter the URL to the <tt>launch.html</tt> file
and click the &quot;Launch the application&quot; link.
<p>The Converter application starts with Java Web Start.</p> </li>
</ol>
</div>
<h2><a name="summary"></a>Summary</h2>
<p>In this short tutorial, we showed how you can easily make a Java application deployable over the web using the NetBeans IDE.
This is just one way of how you can deploy Java applications. </p>
<div class="indent"></div>
<!--<br>-->
<div class="feedback-box"><a href="/about/contact_form.html?to=3&amp;subject=Feedback:%20Enabling%20Java%20Web%20Start%20in%20the%20NetBeans%20IDE">Send Feedback on This Tutorial</a></div>
<br style="clear:both;" >
<h2><a name="seealso" id="seealso"></a>See Also </h2>
<p>For more information about using Java Web Start, see the following resources: </p>
<ul>
<li><a href="http://download.oracle.com/javase/6/docs/technotes/guides/javaws/developersguide/contents.html" target="_blank">Java Web Start Guide</a> - a guide to using the Java Web Start technology </li>
<li><a href="http://download.oracle.com/javase/tutorial/deployment/webstart/index.html">Lesson: Java Web Start</a> from the Java tutorial - practical exercises on how to make applications Java Web Start enabled </li>
</ul>
</body>
</html>