Developing a Simple Bean

Home  »  Java  »  Advance Java  »  Java Beans  »  Developing a Simple Bean



This section presents an example that shows how to develop a simple Bean and connect it to other components via BDK.

Following steps to be adopted to create a new Bean:

  •   Create the Java source file.

  •   Compile the source file.

  •   Create a manifest file.

  •   Generate a JAR file.

  •   Start the BDK.

  •   Test.


This example creates a simple bean button


Diagram 25.9 Type run in the command prompt



Working of the Code:

To create a bean, type the above code and save it as SimpleBean.java. Comiple the above code using the javac.exe compiler as under:

javac SimpleBean.java

Create a manifest file with a .mf extension as under. The Manifest file is a text file which contains the name of the class file.

SimpleBean.mf
Name: SimpleBean.class
Java-Bean: True


Creating a jar (Java Archive) file

As you are aware that javac.exe creates a separate .class file for every class defined in the program. Hence, if a program has five classes, the compiler will create five class file each with their respective name and a .class extension.

This proved to be drawback as the applet loader makes separate connections for each file while loading the applet. To overcome this problem, the engineers at Java Soft, the Sun Microsystems subsidiary for Java introduced the idea fo compressing all the files together which can be unzipped at the applet's client machine when the applet executes.

Jar is a zip or compression utility tailored to Java's needs. Pass the names of all the files in the project to the JAR utility, which compresses all of them together and creates a compressed file with a .jar extension.

Usage of JAR is as under:

jar [-cvfmOM][jar-file name][manifest-file name] list of .class file

Options:
Options:
Description
-cCreates a new archive.
-tList table of contents for archive.
-xExtracts named (or all) files from archive.
-fSpecify archive file name.
-mInclude manifest information from specified manifest file.
-OStore only: use no Zip compression.
-MDo not create a manifest file for the entries.


Creating a JAR file for the example code created eariler on.


This will create a jar file:SimpleBean.jar. Copy this file in the jars directory under the BDK folder and start the Bean Box. The ToolBox of the Bean Box now displays the bean created underline.





Diagram 25.10 The SimpleBean can be dragged and dropped in the BeanBox





Java Beans Programming




7 comments: