Deploying a Java Project Outside of the IDE


When you have finished a general Java project and are ready to distribute it to others as an application, you can do so fairly easily. The IDE helps you assemble the parts that will go into the final application.

When you build a general Java application where you have specified a main class, the IDE does the following:

  • Creates a JAR file containing the project's compiled classes and other resources and places the JAR file in the project's dist folder.

  • Copies the JAR files that are on the project's classpath and pastes them into the dist/lib folder. Note, however, that any folders on the classpath are not similarly copied.

  • In the Class-Path element of the manifest.mf file that is included in the application's JAR file, lists each of the JAR files that were copied to the dist/lib folder.

The dist folder (including its lib subfolder) thus should contain everything the application needs to run on its own. You can distribute the application by packaging the entire contents of the dist/lib folder into a zip file.

You can instruct users to run the application by doing the following:

1.

If necessary, unzip the application.

2.

At the command line, navigate to the dist folder and type the following:

java -jar JarFileName.jar


The application is run on the user's system, with the class specified in the Main-Class as the application's entry point.

The work the IDE does to make your application easier to distribute is enough for most cases, but there are some cases where you might need to do some further manual configuration, such as when:

  • There are folders on the project's classpath (these folders are not automatically copied to the dist/lib folder like JAR files are).

  • Two or more JAR files on the project's classpath have the same name. In this case, the IDE only copies the first JAR file it finds with that name to the dist/lib folder. You would have to create a place for the other JAR files and move them manually (as well as update the manifest Class-Path element).

  • One of the JAR files included in the dist/lib folder also uses the Class-Path element. If that is the case, you will have to add the items that are listed there to the Class-Path element of your application's main JAR file.

See the subtopics below for further information on things you can do to prepare your applications for distribution.

Writing Your Own Manifest for Your JAR File

When you build a general Java project, a JAR file is created with a simple manifest with entries for Manifest-Version, Ant-Version, and Created-By. If the project has a main class designated, that main class is also designated in the JAR manifest.

If you have other entries that you would like to add to the manifest of a project created from the Java Application template or Java Project with Existing Sources template, you can add them directly in the manifest.mf file that sits next to the build.xml file. Go the Files window and double-click the manifest.mf file's node to edit in the Source Editor.

You can also specify a different manifest file for the project to use. To specify a custom manifest:

  1. Open the Files window by clicking the Files tab (or by pressing Ctrl-2).

  2. Expand the project's nbproject folder and open the project.properties file.

  3. In the manifest.file property, type the manifest's name. If the manifest is not in the same folder as the build.xml file, include the relative path from the build.xml file.

You can write the manifest in the IDE's Source Editor by using the Empty File template. Open the Files window. Then right-click the project's main folder and choose New | Empty File.


For projects created from the Java Library template, a basic manifest is generated, but no editable copy of that manifest appears in the Files window. If you would like to specify a different manifest for a project created from the Java Library template, simply add the manifest.file property to the project's project.properties file and point it to the manifest you have created.

Filtering Contents Packaged into Outputs

If you have any files that appear within your source packages but that you do not want packaged in the project's distributable, you can have those files filtered out of the project's distributable.

By default, .form files and .java files are filtered from the output JAR file of general Java projects. In web projects, .form, .java, and .nbattrs files are filtered out by default.

To change the filter for the JAR file contents of a general Java project:

  1. Right-click the project's node in the Projects window and choose Properties.

  2. Select the Build | Packaging node and modify the regular expression in the Exclude from JAR File field. (The name of this field depends on the type of project. For web projects, the field is called Exclude from WAR File.)

.form files are created for classes you create with the IDE's Form Editor. The IDE uses these files to regenerate the design view of those classes; however, they are not necessary for the packaged application. If you delete a .form file, the corresponding .java file remains, and you can edit the code, but you can no longer use the IDE's Form Editor to change the form.

.nbattrs files are files that NetBeans IDE creates to hold information about given directories. Projects created in NetBeans IDE 4.0 and later are unlikely to have these files, but legacy projects might.




NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 279

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net