Anda di halaman 1dari 16

JAR files, packages, and CLASSPATH

Java ARchive (JAR) files

A single file containing all classes for a given project Utilizes the same compression algorithm as PKZIP If it contains a Java Application, it can be executed using java -jar filename.jar Can also contain source code jar cvf Lines.jar *.class *.java

(create new, verbose, specify the name) Type just jar to see usage notes.

JAR Manifest File

If you wish to have your jar be executable, you must include a manifest file It tells the Java interpreter where to find the class that contains the main() method.

Main-Class: HelloWorld

You can name the file whatever you want The file must be included using the m option

Jar cvmf mainifest.mf Hello.jar *.class

Applets can execute JAR files

In your HTML, you would use a tag like the one below to execute an applet that is contained in a jar file

<applet archive=Lines.jar code=RandomLines.class width=550 height=400 />

Using resources such as images in code


When using items such as images in code, you must tell the code where to find the file Since the file system is not available, you must utilize the getResource() method which returns a URL object This URL can be used to point to an image in a JAR file, just like it would to point to an image on the web:

URL url = This.getClass().getResource(x.jpg)

Packages

Represent folder structure You should utilize them for organization If you insist, you can put your code all in the root and have the java compiler build the package structure for the .class files by using the d switch

CLASSPATH Environment Tell java runtime where to look for Java Variable

packages, classes, and JAR files. You can modify the environment variable, or use a custom classpath for a given run: If you want a jar to be read by the compiler, it must be in the classpath JARs are not automatically included, even if in the same directory as your .java or .class files

javac classpath %CLASSPATH%;additionalPath MyClass.java

JavaBeans

Software Components

A software component is a reusable black box that encapsulates functionality Examples


Microsoft ActiveX controls Visual Basics RAD environment which is conducive to drag and drop JavaBeans

Components conform to some software component specification

JavaBeans component specifications

It should be serializable (implements java.io.Serializable), allowing the object to be serialized into a stream It must have a default constructor It must have a set of getters and setters to expose its properties It can have a specific set of methods it allows other components to call It can have a set of events it fires, as well as defined handlers for events

Use of JavaBeans

Since there are standards surrounding JavaBeans, this means

They implement certain interfaces that can be used by developers across multiple beans. They can communicate with each other to create an application of integrated components

JavaBeans examples

SpreadSheet bean tells BarChart bean to redraw itself when data changes DropDown bean tells SubmitBean to submit, which calls ValidationBean, which vetos the submit due to invalid data

PropertyChangeEvent

BoundProperty: a property bound to an event

Changing the property fires an event An action in the event listener that can be used to notify another JavaBean, resulting in changes to other components Other components can veto the changes, based on constrained properties (such as the user attempting to input 123 as their email address)

PropertyChangeSupport:

IDEs can take advantage of JavaBeans

Utilizing the JavaBeans standards defined in the java.beans package, IDEs can:

Use introspection (a.k.a. reflection, discovery) to figure out the beans public properties so you can use a properties editor instead of writing code Allow drag and drop capabilities Write lots of code for you (Why is this good? Why is this bad?)

Other Java Beans

In JSP, JavaBeans are simply a class with many get and set methods. Therefore, a bean is simply an object with properties EJBs (Enterprise Java Beans)

Resources
http://www.javaworld.com/javaworld/jw-08-1997/jw-08-beans.html

Anda mungkin juga menyukai