blob: f39bc57262be1229ce9759b0195a56ab1b6b97a6 [file] [log] [blame]
<!doctype html>
<html class="no-js" lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Packaging and Distributing Java Desktop Applications</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Packaging and Distributing Java Desktop Applications - Apache NetBeans">
<meta name="author" content="Apache NetBeans">
<meta name="description" content="Packaging and Distributing Java Desktop Applications - Apache NetBeans">
<meta name="keywords" content="Apache NetBeans, Tutorials, Packaging and Distributing Java Desktop Applications">
<meta name="generator" content="Apache NetBeans">
<link rel="stylesheet" href="../../../../../../_/css/font-awesome.min.css">
<link rel="alternate" type="application/atom+xml" title="Apache NetBeans Blog" href="https://netbeans.apache.org/blogs/atom" />
<link rel="stylesheet" href="../../../../../../_/css/highlightjs/default.min.css">
<link rel="stylesheet" href="../../../../../../_/css/netbeans.css">
<link rel="apple-touch-icon" sizes="180x180" href="../../../../../../_/images/fav/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="../../../../../../_/images/fav/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="../../../../../../_/images/fav/favicon-16x16.png">
<link rel="manifest" href="../../../../../../_/images/fav/site.webmanifest">
<link rel="mask-icon" href="../../../../../../_/images/fav/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#ffc40d">
<meta name="theme-color" content="#ffffff">
<link href="../../../../../../_/css/font-open-sans.css" rel="stylesheet">
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
</head>
<body>
<div class="title-bar" data-responsive-toggle="responsive-menu" data-hide-for="medium">
<button type="button" data-toggle="responsive-menu"><i style='font-size: 32px; color: #fff; padding: 8px' class='fa fa-bars'></i></button>
<div class="title-bar-title">Apache NetBeans</div>
</div>
<div class="top-bar" id="responsive-menu">
<div class='top-bar-left'>
<a class='title' href="../../../../../../index.html"><img src='../../../../../../_/images/apache-netbeans.svg' style='padding: 8px; height: 48px;'> Apache NetBeans</a>
</div>
<div class="top-bar-right">
<ul class="vertical medium-horizontal menu" data-responsive-menu="drilldown medium-dropdown">
<li> <input id="search-input" type="text" placeholder="Search the docs"> </li>
<li> <a href="../../../../../../front/main/community">Community</a> </li>
<li> <a href="../../../../../../front/main/participate">Participate</a> </li>
<li> <a href="../../../../../../front/main/blogs">Blog</a></li>
<li> <a href="../../../../../../front/main/help">Get Help</a> </li>
<li> <a href="https://plugins.netbeans.apache.org/">Plugins</a> </li>
<li> <a href="../../../../../../front/main/download">Download</a> </li>
</ul>
</div>
</div>
<!-- src/templates/news -->
<section class="hero news alternate">
<div class='grid-container'>
<div class='cell'>
<div class="annotation">Latest release</div>
<h1>Apache NetBeans 27</h1>
<p><a class="button success" href="../../../../../../front/main/download/nb27">Download</a></p>
</div>
</div>
</section>
<div class='grid-container main-content tutorial'>
<article class="doc">
<h1 class="sect0">Packaging and Distributing Java Desktop Applications</h1>
<div class="sectionbody">
<div class="admonitionblock note">
<table>
<tbody><tr>
<td class="icon"><i class="fa icon-note" title="Note"></i></td>
<td class="content">This tutorial needs a review.
You can <a href="https://github.com/apache/netbeans-antora-tutorials/edit/main/modules/ROOT/pages/kb/docs/java/javase-deploy.adoc" title="Edit this tutorial in github">edit it in GitHub </a>
following these <a href="../../../../../../tutorial/main/kb/docs/contributing">contribution guidelines.</a></td>
</tr></tbody>
</table>
</div>
</div>
<div id="toc" class="toc">
<div id="toctitle"></div>
<ul class="sectlevel1">
<li><a href="#_creating_executable_jar_file">Creating Executable JAR File</a>
<ul class="sectlevel2">
<li><a href="#_creating_a_project_with_existing_sources">Creating a Project with Existing Sources</a></li>
<li><a href="#_configuring_the_project">Configuring the Project</a></li>
<li><a href="#_building_the_project_and_creating_the_jar_file">Building the Project and Creating the JAR File</a></li>
</ul>
</li>
<li><a href="#_running_and_distributing_the_jar_file">Running and Distributing the JAR File</a>
<ul class="sectlevel2">
<li><a href="#_running_the_application_inside_of_the_ide">Running the Application Inside of the IDE</a></li>
<li><a href="#_running_the_application_outside_of_the_ide">Running the Application Outside of the IDE</a></li>
<li><a href="#_distributing_the_application_to_other_users">Distributing the Application to Other Users</a></li>
</ul>
</li>
<li><a href="#_starting_your_java_application">Starting Your Java Application</a>
<ul class="sectlevel2">
<li><a href="#_launching_applications_from_the_command_line">Launching Applications From the Command Line</a></li>
<li><a href="#_launching_applications_from_a_script">Launching Applications From a Script</a></li>
</ul>
</li>
<li><a href="#troubleshooting">Troubleshooting Tips</a>
<ul class="sectlevel2">
<li><a href="#_specifying_jar_file_associations">Specifying JAR File Associations</a></li>
<li><a href="#path">Setting the PATH Environment Variable</a></li>
</ul>
</li>
</ul>
</div>
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>One question that a lot of beginning programmers have is: "Now that I&#8217;ve created my application in the IDE, how do I get it to work from the command line outside of the IDE." Similarly, someone might ask, "How do I distribute this application to other users without having to give them the whole IDE as well?"</p>
</div>
<div class="paragraph">
<p>The answers to these questions are relatively simple, but not necessarily obvious. This document addresses those questions by taking you through the basics of using the IDE to prepare your applications for distribution and deployment. In addition, this document provides information that you might need to configure your system (or which you might need to pass on to the users of your application). We will show a few different approaches for deploying an application, so that users can access the application by:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Double-clicking the application&#8217;s Java Archive (JAR) file.</p>
</li>
<li>
<p>Calling the application from the command line.</p>
</li>
<li>
<p>Calling the application from a script file.</p>
</li>
</ul>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
To complete this tutorial, you need the <a href="https://netbeans.org/projects/samples/downloads/download/Samples%252FJava%252FDeploymentTutorial.zip">Deployment Tutorial source files</a>.
</td>
</tr>
</table>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_creating_executable_jar_file"><a class="anchor" href="#_creating_executable_jar_file"></a>Creating Executable JAR File</h2>
<div class="sectionbody">
<div class="paragraph">
<p>This part of the tutorial shows how you can create a distributable application in the IDE and then run that application from outside of the IDE. We will package the application in the form of an executable <a href="http://download.oracle.com/javase/tutorial/deployment/jar/run.html">JAR</a> file.</p>
</div>
<div class="paragraph">
<p>A JAR file is an archive file that can contain multiple files and folders. JAR files are similar to zip files, but JAR files can have additional attributes that are useful for distributing Java applications. These attributes include digitally signing JAR files, additional compression, multiplatform compatibility, etc.</p>
</div>
<div class="paragraph">
<p>In this exercise, you create an IDE project and then place two pre-written Java source files into that project. Then you will compile the classes and build an executable JAR file. Afterwards, you will learn how to run the JAR file from outside of the IDE.</p>
</div>
<div class="paragraph">
<p>The classes used in this tutorial implement features of the <a href="http://www.gnu.org/software/grep/">GNU grep</a> utility, which can be used for searching text or regular expression patterns inside text files. The project contains both command-line and GUI versions of the application, so that you can see different ways of running the application.</p>
</div>
<div class="sect2">
<h3 id="_creating_a_project_with_existing_sources"><a class="anchor" href="#_creating_a_project_with_existing_sources"></a>Creating a Project with Existing Sources</h3>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Download the <a href="https://netbeans.org/projects/samples/downloads/download/Samples%252FJava%252FDeploymentTutorial.zip">DeploymentTutorial.zip</a> file and extract its contents on your system.
This zip archive contains source files for the application plus a few other files that will be used in the tutorial.</p>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>In NetBeans IDE, choose File &gt; New Project.</p>
</li>
<li>
<p>In the Choose Category page, select Java Project With Existing Sources in the Java category and click Next.</p>
</li>
<li>
<p>On the Name and Location page of the wizard, type <code>AnotherGrep</code> as the project name and specify the project&#8217;s location.
Click Next.</p>
</li>
</ol>
</div>
</li>
</ol>
</div>
<div class="paragraph">
<p>The project folder does <em>not</em> have to be in the same location as the source files that you are importing into the project.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>On the Existing Sources page of the wizard, specify the sources that will be in the project.
Click the Add Folder button that is to the right of the Source Package Folders field. Navigate to the <code>DeploymentTutorial</code> folder that you have just unzipped on your system, expand the folder, select the <code>src</code> folder, and click Open. The <code>src</code> folder is added to your Source Package Folders field.</p>
</li>
<li>
<p>Click Finish.</p>
</li>
</ol>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
If, for example, you want to exclude some source files from importing into the project, click Next to open the last Includes &amp; Excludes window. In our case, we want to use all the source files in the <code>src</code> folder, so we click Finish to finish working in the New Project wizard.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>The project opens in the IDE and becomes visible in the Projects window. You can explore the contents of the project by expanding the project&#8217;s Source Packages node, where you should see classes called <code>Grep</code> and <code>xGrep</code> . <code>Grep.java</code> is a console version of the application. <code>xGrep.java</code> is a GUI version of the application and uses methods defined in <code>Grep.java</code> .</p>
</div>
</div>
<div class="sect2">
<h3 id="_configuring_the_project"><a class="anchor" href="#_configuring_the_project"></a>Configuring the Project</h3>
<div class="paragraph">
<p>There are a few configuration steps you need to do, such as:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Choose the Java platform that will be used to compile the sources.</p>
</li>
<li>
<p>Set the project&#8217;s main class. By doing this, you ensure that the JAR file that you create when you build the project is executable.</p>
</li>
</ul>
</div>
<div class="sect3">
<h4 id="_verifying_the_java_platform"><a class="anchor" href="#_verifying_the_java_platform"></a>Verifying the Java Platform</h4>
<div class="paragraph">
<p>Our project needs to be compiled and run on Java 7 or Java 8 platform. Therefore, you need to make sure that Java 7 or Java 8 is respectively used as the platform for this project.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Right-click the project&#8217;s node and choose Properties.</p>
</li>
<li>
<p>On the Libraries tab, ensure that the Java Platform is JDK 1.7 (or JDK 1.8).</p>
</li>
<li>
<p>On the Sources tab, choose JDK 7 (or JDK 8) in the Source/Binary format.</p>
</li>
<li>
<p>Click OK to close the Properties window.</p>
</li>
</ol>
</div>
</div>
<div class="sect3">
<h4 id="_setting_the_main_class"><a class="anchor" href="#_setting_the_main_class"></a>Setting the Main Class</h4>
<div class="paragraph">
<p>In order for a user to easily run your JAR file (by double-clicking the JAR file or by typing <code>java -jar AnotherGrep.jar</code> at the command line), a main class has to be specified inside the JAR&#8217;s <em>manifest</em> file. (The manifest is a standard part of the JAR file that contains information about the JAR file that is useful for the <code>java</code> launcher when you want to run the application.) The main class serves as an entry point from which the <code>java</code> launcher runs your application.</p>
</div>
<div class="paragraph">
<p>When you build a project, the IDE builds the JAR file and includes a manifest. When you set the project&#8217;s main class, you ensure that the main class is be designated in the manifest.</p>
</div>
<div class="paragraph">
<p>To set the project&#8217;s main class:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Right-click the project&#8217;s node and choose Properties.</p>
</li>
<li>
<p>Select the Run category and enter <code>anothergrep.xGrep</code> in the Main Class field.</p>
</li>
<li>
<p>Click OK to close the Project Properties dialog box.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>When you build the project later in this tutorial, the manifest will be generated and include the following entry:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">Main-Class: anothergrep.xGrep</code></pre>
</div>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_building_the_project_and_creating_the_jar_file"><a class="anchor" href="#_building_the_project_and_creating_the_jar_file"></a>Building the Project and Creating the JAR File</h3>
<div class="paragraph">
<p>Now that you have your sources ready and your project configured, it is time to build your project.</p>
</div>
<div class="paragraph">
<p>To build the project:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Choose Run &gt; Build Project (AnotherGrep).
Alternatively, right-click the project&#8217;s node in the Projects window and choose Build.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>When you build your project:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><code>build</code> and <code>dist</code> folders are added to your project folder (hereafter referred to as the <em>PROJECT_HOME</em> folder).</p>
</li>
<li>
<p>All of the sources are compiled into <code>.class</code> files, which are placed into the <code><em>PROJECT_HOME</em>/build</code> folder.</p>
</li>
<li>
<p>A JAR file containing your project is created inside the <code><em>PROJECT_HOME</em>/dist</code> folder.</p>
</li>
<li>
<p>If you have specified any libraries for the project (in addition to the JDK), a <code>lib</code> folder is created in the <code>dist</code> folder. The libraries are copied into <code>dist/lib</code> .</p>
</li>
<li>
<p>The manifest file in the JAR is updated to include entries that designate main class and any libraries that are on the project&#8217;s classpath.</p>
</li>
</ul>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
You can view the contents of the manifest in the IDE&#8217;s Files window. After you have built your project, switch to the Files window and navigate to <code>dist/AnotherGrep.jar</code> . Expand the node for the JAR file, expand the <code>META-INF</code> folder, and double-click <code>MANIFEST.MF</code> to display the manifest in the Source Editor.
</td>
</tr>
</table>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">Main-Class: anothergrep.xGrep</code></pre>
</div>
</div>
<div class="paragraph">
<p>(To find more about manifest files, you can read <a href="http://java.sun.com/docs/books/tutorial/deployment/jar/manifestindex.html">this chapter</a> from the Java Tutorial.)</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_running_and_distributing_the_jar_file"><a class="anchor" href="#_running_and_distributing_the_jar_file"></a>Running and Distributing the JAR File</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_running_the_application_inside_of_the_ide"><a class="anchor" href="#_running_the_application_inside_of_the_ide"></a>Running the Application Inside of the IDE</h3>
<div class="paragraph">
<p>When developing applications in the IDE, typically you will need to test and refine them before distributing. You can easily test an application that you are working on by running the application from the IDE.</p>
</div>
<div class="paragraph">
<p>To run the <code>AnotherGrep</code> project in the IDE, right-click the project&#8217;s node in the Projects window and choose Run.</p>
</div>
<div class="paragraph">
<p>The xGrep window should open. You can click the Browse button to choose a file in which to search for a text pattern. In the Search Pattern field, type text or a regular expression pattern that you would like to match, and click Search. The results of each match will appear in the xGrep window&#8217;s Output area.</p>
</div>
<div class="paragraph">
<p>Information on regular expressions that you can use in this application are available <a href="http://www.gnu.org/software/grep/manual/html_node/Regular-Expressions.html#Regular-Expressions">here</a> and in many other places.</p>
</div>
</div>
<div class="sect2">
<h3 id="_running_the_application_outside_of_the_ide"><a class="anchor" href="#_running_the_application_outside_of_the_ide"></a>Running the Application Outside of the IDE</h3>
<div class="paragraph">
<p>Once you have finished developing the application and before you distribute it, you will probably want to make sure that the application also works outside of the IDE.</p>
</div>
<div class="paragraph">
<p>You can run the application outside of the IDE by following these steps:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>In your system&#8217;s file manager (for example, in the My Computer window on Windows XP systems), navigate to <code><em>PROJECT_HOME</em>/dist</code> and double-click the <code>AnotherGrep.jar</code> file.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>You will know that the application has started successfully when the xGrep window opens.</p>
</div>
<div class="paragraph">
<p>If the xGrep window does not open, your system probably does not have a file association between JAR files and the Java Runtime Environment. See <a href="#troubleshooting">Troubleshooting JAR File Associations</a> below.</p>
</div>
</div>
<div class="sect2">
<h3 id="_distributing_the_application_to_other_users"><a class="anchor" href="#_distributing_the_application_to_other_users"></a>Distributing the Application to Other Users</h3>
<div class="paragraph">
<p>Now that you have verified that the application works outside of the IDE, you are ready to distribute it.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Send the application&#8217;s JAR file to the people who will use the application. The users of your application should be able to run it by double-clicking the JAR file. If this does not work for them, show them the information in the <a href="#troubleshooting">Troubleshooting JAR File Associations</a> section below.</p>
</li>
</ul>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
If your application depends on additional libraries other than those included in JDK, you need to also include them in your distribution (not the case in our example). The relative paths to these libraries are added in the <code>classpath</code> entry of the JAR&#8217;s manifest file when you are developing your application in the IDE. If these additional libraries will not be found at the specified classpath (i.e., relative path) at launch, the application will not start.
Create a zip archive that contains the application JAR file and the library and provide this zip file to users. Instruct the users to unpack the zip file making sure that the JAR file and libraries JAR files are in the same folder. Run the application JAR file.
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_starting_your_java_application"><a class="anchor" href="#_starting_your_java_application"></a>Starting Your Java Application</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The goal of this exercise is to show you some ways that you can start your application from the command line.</p>
</div>
<div class="paragraph">
<p>This exercise shows you how you can start a Java application in the following two ways:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Running the <code>java</code> command from the command line.</p>
</li>
<li>
<p>Using a script to a call a class in the JAR file.</p>
</li>
</ul>
</div>
<div class="sect2">
<h3 id="_launching_applications_from_the_command_line"><a class="anchor" href="#_launching_applications_from_the_command_line"></a>Launching Applications From the Command Line</h3>
<div class="paragraph">
<p>You can launch an application from the command line by using the <code>java</code> command. If you want to run an executable JAR file, use the <code>-jar</code> option of the command.</p>
</div>
<div class="paragraph">
<p>For example, to run the AnotherGrep application, you would take the following steps:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Open a terminal window. On Microsoft Windows systems, you do this by choosing Start &gt; Run, typing <code>cmd</code> in the Open field, and clicking OK.</p>
</li>
<li>
<p>Change directories to the <code><em>PROJECT_HOME</em>/dist</code> folder (using the <code>cd</code> command).</p>
</li>
<li>
<p>Type the following line to run the application&#8217;s main class:</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">java -jar AnotherGrep.jar</code></pre>
</div>
</div>
<div class="paragraph">
<p>If you follow these steps and the application does not run, you probably need to do one of the following things:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Include the full path to the <code>java</code> binary in the third step of the procedure. For example, you would type something like the following, depending on where your JDK or JRE is located:</p>
</li>
</ul>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">C:\Program Files\Java\jdk1.7.0_51\bin\java -jar AnotherGrep.jar</code></pre>
</div>
</div>
<div class="ulist">
<ul>
<li>
<p>Add the Java binaries to your PATH environment variable, so that you never have to specify the path to the <code>java</code> binary from the command line. See <a href="#path">Setting the PATH Environment Variable</a>.</p>
</li>
</ul>
</div>
</div>
<div class="sect2">
<h3 id="_launching_applications_from_a_script"><a class="anchor" href="#_launching_applications_from_a_script"></a>Launching Applications From a Script</h3>
<div class="paragraph">
<p>If the application that you want to distribute is a console application, you might find that it is convenient to start the application from a a script, particularly if the application takes long and complex arguments to run. In this section, you will use a console version of the Grep program, where you need to pass the arguments (search pattern and file list) to the JAR file, which will be invoked in our script. To reduce typing at the command line, you will use a simple script suitable to run the test application.</p>
</div>
<div class="paragraph">
<p>First you need to change the main class in the application to be the console version of the class and rebuild the JAR file:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>In the IDE&#8217;s Projects window, right-click the project&#8217;s node ( <code>AnotherGrep</code> ) and choose Properties.</p>
</li>
<li>
<p>Select the Run node and change the Main Class property to <code>anothergrep.Grep</code> (from <code>anothergrep.xGrep</code> ). Click OK to close the Project Properties window.</p>
</li>
<li>
<p>Right-click the project&#8217;s node again and choose Clean and Build Project.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>After completing these steps, the JAR file is rebuilt, and the <code>Main-Class</code> attribute of the JAR file&#8217;s manifest is changed to point to <code>anothergrep.Grep</code> .</p>
</div>
<div class="sect3">
<h4 id="_bash_scriptfor_unix_and_linux_machines"><a class="anchor" href="#_bash_scriptfor_unix_and_linux_machines"></a><a href="http://www.gnu.org/software/bash/bash.html">BASH</a> script&#8201;&#8212;&#8201;for UNIX and Linux machines</h4>
<div class="paragraph">
<p>Inside the folder on your system where you extracted the contents of the <a href="https://netbeans.org/projects/samples/downloads/download/Samples%252FJava%252FDeploymentTutorial.zip">DeploymentTutorial.zip</a> file, there is a <code>grep.sh</code> bash script. Have a look at it:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">#!/bin/bash
java -jar dist/AnotherGrep.jar $@</code></pre>
</div>
</div>
<div class="paragraph">
<p>The first line states which shell should be used to interpret this. The second one executes your JAR file, created by the IDE inside <code><em>PROJECT_HOME</em>/dist</code> folder. <code>$@</code> just copies all given arguments, enclosing each inside quotes.</p>
</div>
<div class="paragraph">
<p>This script presumes that the Java binaries are part of your PATH environment variable. If the script does not work for you, see <a href="#path">Setting the PATH Environment Variable</a>.</p>
</div>
<div class="paragraph">
<p>More about bash scripting can be found <a href="http://www.gnu.org/software/bash/manual/bashref.html">here</a>.</p>
</div>
</div>
<div class="sect3">
<h4 id="_bat_script_for_windows_machines"><a class="anchor" href="#_bat_script_for_windows_machines"></a>.bat script for Windows machines</h4>
<div class="paragraph">
<p>On Microsoft Windows systems, you can only pass nine arguments at once to a batch file. If there were more than nine arguments, you would need to execute the JAR file multiple times.</p>
</div>
<div class="paragraph">
<p>A script handling this might look like the following:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java"> @echo off
set jarpath="dist/AnotherGrep.jar"
set pattern="%1"
shift
:loop
if "%1" == "" goto :allprocessed
set files=%1 %2 %3 %4 %5 %6 %7 %8 %9
java -jar %jarpath% %pattern% %files%
for %%i in (0 1 2 3 4 5 6 7 8) do shift
goto :loop
:allprocessed</code></pre>
</div>
</div>
<div class="paragraph">
<p>This script is included as <code>grep.bat</code> inside the folder on your system where you extracted the contents of the <a href="https://netbeans.org/projects/samples/downloads/download/Samples%252FJava%252FDeploymentTutorial.zip">DeploymentTutorial.zip</a> file so you can try it out.</p>
</div>
<div class="paragraph">
<p>The nine arguments are represented inside the batch file by <code>%&lt;ARG_NUMBER&gt;</code> , where <code>&lt;ARG_NUMBER&gt;</code> has to be inside <code>&lt;0-9&gt;</code> . <code>%0</code> is reserved for the script name.</p>
</div>
<div class="paragraph">
<p>You can see that only nine arguments are passed to the program at a time (in one loop). The <code>for</code> statement just shifts the arguments by nine, to prepare it for next loop. Once an empty file argument is detected by the <code>if</code> statement (there are no further files to process), the loop is ended.</p>
</div>
<div class="paragraph">
<p>More about batch scripting can be found on <a href="http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx">this page</a>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="troubleshooting"><a class="anchor" href="#troubleshooting"></a>Troubleshooting Tips</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_specifying_jar_file_associations"><a class="anchor" href="#_specifying_jar_file_associations"></a>Specifying JAR File Associations</h3>
<div class="paragraph">
<p>On most systems, you can execute an executable JAR file by simply double-clicking the JAR file. If nothing happens when you double-click the JAR file, it might be because of either of the following two reasons:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The JAR file type is probably not associated with a Java Runtime Environment (JRE) on that system.</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>If the JAR file type is associated with a JRE, the icon that represents that file should include a Java logo.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>The JAR file type is associated with the JRE, but the <code>-jar</code> option is not included in the command that is passed to the JRE when you double-click the icon.</p>
</li>
</ul>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
Sometimes JAR file associations are switched by software that you install, such as software to handle zip files.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>The way how you associate the JAR file type with the <code>java</code> launcher depends on your operating system.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
Make sure that there is a version of the JRE installed on your system. You should use version 1.4.2 or later. You cannot launch a Java application if no Java is installed. (If you have the JDK installed, you also get the JRE. However, if you are distributing the program to a non-programmer, that person does not necessarily have either the JRE or the JDK.)
</td>
</tr>
</table>
</div>
<div class="ulist">
<ul>
<li>
<p>On Windows XP, you can check for installed versions of Java by choosing Start &gt; Control Panel &gt; Add or Remove Software (you should see, for example, Java&#8482; 7 Update 51).</p>
</li>
<li>
<p>On Windows Vista or 7, you can check for installed versions of Java by choosing Start &gt; Control Panel &gt; Programs and Components (you should see, for example, Java&#8482; 7 Update 51).</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>If there is no Java on the system, you can get the JRE one from the <a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">Java SE download site</a>.</p>
</div>
<div class="paragraph">
<p>If you have Java installed on your system, but the file association is not working, continue with the steps for adding the JAR file association on Microsoft Windows:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Choose Start &gt; Control Panel.</p>
</li>
<li>
<p>(Applicable to Windows Vista only.) Click Control Panel Home &gt; Programs.</p>
</li>
<li>
<p>For Windows XP, double-click Folder Options and select the File Types tab.
For Windows Vista or 7, click Default Programs and select Associate a file type or protocol with a program.</p>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>In the Registered File Types list, select JAR File.</p>
</li>
<li>
<p>(On Windows XP, in the Details section of the dialog box), click Change Program.</p>
</li>
<li>
<p>In the Open With dialog box, select Java Platform SE Binary.</p>
</li>
<li>
<p>Click OK to exit the Open With dialog box.</p>
</li>
<li>
<p>Click Close to exit the Folder Options dialog box (on Windows XP) or the Associate a file type or protocol with a specific program dialog box (on Windows 7).</p>
</li>
</ol>
</div>
</li>
</ol>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
If JAR files are associated with the Java Platform SE Binary on your system but double-clicking still does not execute the file JAR file, you might need to specify the <code>-jar</code> option in the file association.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>To specify the <code>-jar</code> option in the file association on Microsoft Windows XP:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Choose Start &gt; Control Panel.</p>
</li>
<li>
<p>For Windows XP, double-click Folder Options and select the File Types tab.</p>
</li>
<li>
<p>In the Registered File Types list, select JAR File.</p>
</li>
<li>
<p>In the Details section of the dialog box, click Advanced.</p>
</li>
<li>
<p>In the Edit File Type dialog box, click Edit.</p>
</li>
<li>
<p>In the Application Used to Perform Action text field, add the following at the end of the path to the JRE:</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java"> -jar "%1" %*</code></pre>
</div>
</div>
<div class="paragraph">
<p>Afterwards, the field should contain text similar to the following:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">"C:\Program Files\Java\jre1.7.0_51\bin\javaw.exe" -jar "%1" %*</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Click OK to exit the Editing Action for Type dialog box.</p>
</li>
<li>
<p>Click OK to exit the Edit File Type dialog box.</p>
</li>
<li>
<p>Click Close to exit the Folder Options dialog box.</p>
</li>
</ol>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
Starting with Windows Vista advanced file associations can be set via RegEdit. See the <a href="http://technet.microsoft.com/en-us/magazine/ee914604.aspx">What Happened to the File Types Dialog?</a> article for details.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>For UNIX and Linux systems, the procedure for changing file associations depends on which desktop environment (such as GNOME or KDE) that you are using. Look in your desktop environment&#8217;s preference settings or consult the documentation for the desktop environment.</p>
</div>
</div>
<div class="sect2">
<h3 id="path"><a class="anchor" href="#path"></a>Setting the PATH Environment Variable</h3>
<div class="paragraph">
<p>If you can not run a Java class or JAR file on your system without pointing to the location of the JDK or JRE on your system, you might need to modify the value of your system&#8217;s <code>PATH</code> variable.</p>
</div>
<div class="paragraph">
<p>If you are running on a Microsoft Windows system, the procedure for setting the PATH variable depends the version of Windows you are using.</p>
</div>
<div class="paragraph">
<p>The following are the steps for setting the <code>PATH</code> variable on a Windows XP system:</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Choose Start &gt; Control Panel and double-click System.</p>
</li>
<li>
<p>In the System Properties dialog box, click the Advanced tab.</p>
</li>
<li>
<p>Click the Environment Variables tab.</p>
</li>
<li>
<p>In the list of user variables, select <code>PATH</code> and click Edit.</p>
</li>
<li>
<p>Add the location of the JRE to the end of the list of paths. The locations in this list are separated by semicolons (;).
For example, if your JRE is located at <code>C:\Program Files\Java\jdk1.7.0_51</code> you would add the following to the end of the PATH variable:</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">C:\Program Files\Java\jdk1.7.0_51\bin</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Click OK to exit the Environment Variables dialog box, and click OK to exit the System Properties dialog box.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>If you are running on a UNIX or Linux system, the instructions for modifying your PATH variable depends on the shell program you are using. Consult the documentation of the shell that you are using for more information.</p>
</div>
</div>
</div>
</div>
<section class='tools'>
<ul class="menu align-center">
<li><a title="Facebook" href="https://www.facebook.com/NetBeans"><i class="fa fa-md fa-facebook"></i></a></li>
<li><a title="Twitter" href="https://twitter.com/netbeans"><i class="fa fa-md fa-twitter"></i></a></li>
<li><a title="Github" href="https://github.com/apache/netbeans"><i class="fa fa-md fa-github"></i></a></li>
<li><a title="YouTube" href="https://www.youtube.com/user/netbeansvideos"><i class="fa fa-md fa-youtube"></i></a></li>
<li><a title="Atom Feed" href="https://netbeans.apache.org/blogs/atom"><i class="fa fa-mf fa-rss"></i></a></li>
<li><a title="Slack" href="https://tinyurl.com/netbeans-slack-signup/"><i class="fa fa-md fa-slack"></i></a></li>
<li><a title="Issues" href="https://github.com/apache/netbeans/issues"><i class="fa fa-mf fa-bug"></i></a></li>
</ul>
<ul class="menu align-center">
<li><a href="https://github.com/apache/netbeans-antora-tutorials/edit/main/modules/ROOT/pages/kb/docs/java/javase-deploy.adoc" title="See this page in github"><i class="fa fa-md fa-edit"></i> See this page in GitHub.</a></li>
</ul>
</section>
</article>
</div>
<div class='grid-container incubator-area' style='margin-top: 64px'>
<div class='grid-x grid-padding-x'>
<div class='large-auto cell text-center'>
<a href="https://www.apache.org/">
<img style="height: 60px" title="Apache Software Foundation" src="../../../../../../_/images/asf_logo_wide.svg" />
</a>
</div>
<div class='large-auto cell text-center'>
<a href="https://www.apache.org/events/current-event.html">
<img style="width:234px; height: 60px;" title="Apache Software Foundation current event" src="https://www.apache.org/events/current-event-234x60.png"/>
</a>
</div>
</div>
</div>
<footer>
<div class="grid-container">
<div class="grid-x grid-padding-x">
<div class="large-auto cell">
<h1><a href="../../../../../../front/main/about">About</a></h1>
<ul>
<li><a href="../../../../../../front/main/community/who">Who's Who</a></li>
<li><a href="https://www.apache.org/foundation/thanks.html">Thanks</a></li>
<li><a href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li>
<li><a href="https://www.apache.org/security/">Security</a></li>
</ul>
</div>
<div class="large-auto cell">
<h1><a href="../../../../../../front/main/community">Community</a></h1>
<ul>
<li><a href="../../../../../../front/main/community/mailing-lists">Mailing lists</a></li>
<li><a href="../../../../../../front/main/community/committer">Becoming a committer</a></li>
<li><a href="../../../../../../front/main/community/events">NetBeans Events</a></li>
<li><a href="https://www.apache.org/events/current-event.html">Apache Events</a></li>
</ul>
</div>
<div class="large-auto cell">
<h1><a href="../../../../../../front/main/participate">Participate</a></h1>
<ul>
<li><a href="../../../../../../front/main/participate/submit-pr">Submitting Pull Requests</a></li>
<li><a href="../../../../../../front/main/participate/report-issue">Reporting Issues</a></li>
<li><a href="../../../../../../front/main/participate/#documentation">Improving the documentation</a></li>
</ul>
</div>
<div class="large-auto cell">
<h1><a href="../../../../../../front/main/help">Get Help</a></h1>
<ul>
<li><a href="../../../../../../front/main/help/#documentation">Documentation</a></li>
<li><a href="../../../../../../wiki/main/wiki">Wiki</a></li>
<li><a href="../../../../../../front/main/help/#support">Community Support</a></li>
<li><a href="../../../../../../front/main/help/commercial-support">Commercial Support</a></li>
</ul>
</div>
<div class="large-auto cell">
<h1><a href="../../../../../../front/main/download">Download</a></h1>
<ul>
<li><a href="../../../../../../front/main/download">Releases</a></li>
<li><a href="https://plugins.netbeans.apache.org/">Plugins</a></li>
<li><a href="../../../../../../front/main/download/#_daily_builds_and_building_from_source">Building from source</a></li>
<li><a href="../../../../../../front/main/download/#_older_releases">Previous releases</a></li>
</ul>
</div>
</div>
</div>
</footer>
<div class='footer-disclaimer'>
<div class="footer-disclaimer-content">
<p>Copyright &copy; 2017-2025 <a href="https://www.apache.org">The Apache Software Foundation</a>.</p>
<p>Licensed under the Apache <a href="https://www.apache.org/licenses/">license</a>, version 2.0</p>
<div style='max-width: 40em; margin: 0 auto'>
<p>Apache, Apache NetBeans, NetBeans, the Apache feather logo and the Apache NetBeans logo are trademarks of <a href="https://www.apache.org">The Apache Software Foundation</a>.</p>
<p>Oracle and Java are registered trademarks of Oracle and/or its affiliates.</p>
<p>The Apache NetBeans website conforms to the <a href="https://privacy.apache.org/policies/privacy-policy-public.html">Apache Software Foundation Privacy Policy</a></p>
</div>
</div>
</div>
<script src="../../../../../../_/js/vendor/lunr.js"></script>
<script src="../../../../../../_/js/search-ui.js" id="search-ui-script" data-site-root-path="../../../../../.." data-snippet-length="100" data-stylesheet="../../../../../../_/css/search.css"></script>
<script async src="../../../../../../search-index.js"></script>
<script src="../../../../../../_/js/vendor/jquery.min.js"></script>
<script src="../../../../../../_/js/vendor/what-input.min.js"></script>
<script src="../../../../../../_/js/vendor/foundation.min.js"></script>
<script src="../../../../../../_/js/vendor/jquery.colorbox-min.js"></script>
<script src="../../../../../../_/js/netbeans.js"></script>
<script>
$(function(){ $(document).foundation(); });
</script>
<script src="../../../../../../_/js/vendor/highlight.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
document.querySelectorAll('pre code').forEach((el) => {
hljs.highlightElement(el);
});
});
</script>
</body>
</html>