Jul 05, 2013 When I moved from Eclipse to NetBeans on my main project, I realized there are some small features which are not in NetBeans. One of them is the possibility to generate the serial version UID. Standard NetBeans option is to show, for all classes which implements Serializable and do not have a serialVersionUID field, an “Add default.
![]()
In the past two articles you have seen how to customize your Javaapplication so that it looks and feels more like a native Macintoshapplication when running on Mac OS X without changing the end user experienceon other platforms. A combination of runtime properties and coding changesthat targeted Mac OS X specific APIs made a big difference to thataudience.Recall that Mac OS X is a melding of two worlds. Hard coreUNIX programmers can pop open a Terminal window and write their Java codeusing vi and compile and run it from the command line.
There is, however, themore traditional Mac audience that interacts with their computer through afriendly UI that follows Apple Human Interface guidelines.In thisarticle, we look at deploying your Java application. The technical geekaudience might be happy with running a class with a main method from the command line but the wideraudience expects a double-clickable icon that looks and acts like every othernative application.
In this article, we travel from one end of the spectrum tothe other to broaden your potential user base.Although you should'test everywhere', your build machine may not be a Mac. Fortunately, as youwill see, a double-clickable Macintosh application is just a directory withsome special contents and a name that ends with.app. Even on a Windows machine you should be able tomodify your build script to package up a Mac-specific version of yourapplication.
Primitive DistributionsBecause Mac OS X shipswith J2SE 1.4.1 and J2SE 1.3.1, you can distribute your application as classfiles or jar files and - in theory - your customer could run your applicationfrom the Terminal application. We start with these models and quickly move todouble-clickable jar files and shell scripts.For this article, use theas the running example.
Download and unzip the zip file. Inside theJavaSoundDemo directory you will findthe source files inside of the srcsubdirectory, a jar file, audio files, and html files that we will notuse.Raw Class FilesAs a developer, you don't think twice aboutcompiling the source files and running the application using the command line.Compiling the eight files in the srcdirectory generates fifty class files. You can then run the sample applicationfrom the command line like this.java JavaSoundThe Java Sound Demo starts up. We haven't customized the application in anyway so the menu appears at the top of the JFrame and not where Macintosh usersexpect. The application looks like this out of the box.You have done this compile and run step so many times that you hardly thinktwice about it. Think of the least technical person you know and ask whetherthey would be likely to follow these steps to run your application if acompeting application were easier to install and run.This exampledemonstrates two separate areas of usability. Once we got the application upand running it looked good and ran fine.
You would not, however, want todistribute an application to an end user this way. You would have to somehowbundle up the fifty class files for easy download and installation. You wouldthen have to provide instructions for running the application using, in thecase of Mac OS X, the Terminal application. Jar FilesIf youare going to have to package up the class files for distribution anyways, youmay as well produce a jar file. And, if you are going to produce a jar file,it ought to be executable. In the case of the Java Sound Demo, the fileJavaSoundDemo.jar is executable. Because MacOS X ships with the Jar Launcher application, the end user needs only doubleclick on the jar file and the application will launch.To make the jarfile executable, the manifest must include the name of the Main class file.Unjar JavaSoundDemo.jar with thecommand jar xvj JavaSoundDemo.jar.Here's the file META-INF/MANIFEST.MF.
Manifest-Version: 1.0 Main-Class: JavaSound Created-By: 1.3.0 (SunMicrosystems Inc.) Shell Scripts and Helper ApplicationsFor larger or more complicated applications you arelikely to have more than one jar file along with resource files. A commonstrategy for targeting multiple platforms is to include a batch file and ashell script. Choose the non-platform specific download from thehomepage.Inside of the bin subdirectory you will find applications for running NetBeanson a variety of platforms.The shell script runide.sh can berun from the command line like this.sh runide.sh -jdkhome /Library/Java/HomeThe NetBeans IDE starts up with this decidedly non-Mac OS X look andfeel.You could, ofcourse, modify the shell script to modify this look and feel, but the NetBeansdevelopers decided on a different approach.
Even though the typical NetBeansaudience member is technically competent, there should be a friendlier way tostart the IDE. They have created a native Mac OS X application calledNetBeansLauncher.The version ofNetBeansLauncher that is included in thegeneric NetBeans download is a good next step. You will see how the team tookit farther in the next section. On a Mac OS X computer you can double click onthe macosxlauncher.dmg file insideof the bin directory. This is a diskimage. Drag the NetBeansLauncher fromthe expanded disk image back into the bin directory. Now double click on theNetBeansLauncher.
The ReadMe file that was also inthe disk image provides the following information about usage. When launched for the first time, NetBeansLauncher needs to findNetBeans root directory. First it looks into NetBeansLauncher.app itself.
Ifit does not find NetBeans root directory there user must specify NetBeans rootdirectory manually.For this download, the first time the userstarts up the NetBeansLauncher, theyneed to navigate to the netbeansdirectory. After that, double clicking on the NetBeansLauncher starts up the NetBeans IDE as if it wereany other native Mac OS X application. First Class Mac OS X ApplicationsIf you download the Mac OS X disk image from the NetBeansdistribution and mount it you may be surprised at the simplicity of what youfind. Unlike the complex structure visible in the other distributions, youwill see five files with documentation and a single application. To install,you can move this NetBeansLauncherapplication anywhere on your hard drive. Double click on it and the NetBeansIDE starts right up.This is the experience that is expected on Mac OSX.
The package structure and complexity is hidden from the user and they can'taccidentally move a file that renders the IDE unusable. In this section we'lllook more closely at the package structure and how to create a Mac OS Xapplication whether or not our build machine is a Mac. Packages in MacOS XConsider again the sentence from the NetBeansLauncher instructions that says in order tolocate the NetBeans root directory, 'First it looks into NetBeansLauncher.appitself.' This implies that NetBeansLauncher is a directory with the name'NetBeansLauncher.app'.
![]()
In the mounted disk image, either right click orCtrl-click on the NetBeansLauncher icon and choose to 'Show PackageContents'.The structureis the same for all Mac OS X applications. There is a Contents directory with an XML file namedinfo.plist, a text file named PkgInfo, a MacOSdirectory, and a Resources directory. If you don't have a creator coderegistered with Apple the PkgInfo text file should contain only thefollowing.
AAPL????If you have a creator code, use it inplace of the question marks. Ordinarily the MacOS folder contains a small stubfile that launches the Java VM. In this case the NetBeans team has writtentheir own application. We will look more closely at a property list file inthe next section. For now, take a look at the contents of theResources directory.You can see the contents of the same netbeans package inside of the Resources subdirectory. This is your key to deploying onMac OS X.
Add in the necessary pieces and then just bundle up your ordinarydistribution in the appropriate location. If you have a more flexible buildprocess you should also strip out those pieces that aren't needed for the MacOS X application such as the Windows executables.If you are interestedin digging deeper into the structure of a Mac OS X application, you will findmore information in the Apple publication.
Creating 'Native' Java Applications on Mac OS XIf you develop on Mac OS X you can use the Jar Bundler application to turn jar files into Mac OS Xapplications. Jar Bundler isdistributed with the other developer tools and is located inDeveloper/Applications/. Start it, select the'Classpath and Files' tab and add the file JavaSoundDemo.jar.Select the 'Build Information'tab. For 'Main Class', navigate to the JavaSoundDemo.jar file again and selectJavaSound from the drop down list.
This list ispopulated by any classes in the jar file containing a main method. Accept all of the default settings for theoptions. You can use the default Java application icon or you can create yourown. The icon you see below started as a screen shot of the running Java SoundDemo and was transformed into an icon using the IconComposer application that is also distributed as partof the developer tools.Press 'CreateApplication' and enter the name 'JavaSoundDemo'.
A Mac OS X application iscreated for you. You can show the package contents of the generatedapplication as before. You can view the property list with Apple's PropertyList Editor or with any text editor. It is just an XML file with propertiesstored as name - value pairs.The Javaproperties indicate the location of the jar file, the name of the Main class,and the version of the JRE to be used.
![]()
The other properties include a pointerto the icon file and to the Java application stub file that is the nativeexecutable. Creating Mac OS X Java Applications on OtherPlatformsTake a look at the contents of the package that wasgenerated by Jar Bundler on Mac OSX.On another platform you need to duplicate this structure. To create anapplication named 'JavaSoundDemo' on, say, a Windows machine, start bycreating a directory and naming it JavaSoundDemo.app. Next, create a subdirectory namedContents.
Inside of Contents you will need a MacOS directory with the JavaAPplicationStub. You can create thePkgInfo text file and your Info.plist can also be generated by hand and shouldcontain the following XML.You will need a Resourcesdirectory with a Java subdirectoryinto which you put the JavaSoundDemo.jar file. In other words, with theexception of the JavaApplicationStuband the music.icns file, everythingelse can be created on another platform.If you use Ant you can easilyadd a target that takes your jar files, images, and other resources andbundles it up as a Mac OS X application bundle that includes the plist fileand Java application stub in the appropriate locations.
Every time you createa new build you will automatically have your Mac OS X version. You can alsofind a growing number of Ant tasks that automate some of the steps outlined inthis article. SummaryWhen it comes time to deploy your Javaapplication, consider the ease of use of your target audience. Even developersappreciate the double clickable version of the NetBeans IDE. Creating a Javaapplication that looks and feels like a native application does not require alot of extra work and can easily be integrated into your build process even ifyour build machine runs a different operating system. For More Information.
We found out that your browser is a little bit old!
![]() Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
March 2023
Categories |