Recipe 2.2.
Determining Where to Save a Class
Problem
You want to determine where to save a class file.
Solution
Save the file in a directory path corresponding to the
package. Then, if necessary, add the top-level directory to the
classpath.
Discussion
Class files must always be saved in a directory
path that corresponds to the class package. For example,
com.examplecorp.net.messaging.email.MessageManager must be
saved in
com/examplecorp/net/messaging/email/MessageManager.as. The
compiler knows to look for classes where the path corresponds to
the package. However, the compiler also must know where to look
for the top-level directory containing the subdirectories. In the
example, the compiler needs to know where the com directory
is on the system. The compiler knows where to look because of
something called the classpath.
The default classpath for any Flex or Flash project includes the
project directory. For example, if the com directory is
saved in the same directory as the .fla file (Flash) or the main
MXML or ActionScript file (Flex), then
the compiler will find the classes. However, you may want to save
files in a different directory. For example, if you have a common
library used by many projects, you may want to save that library in
one location rather than making copies for each project. You can
add to and edit the classpath so the compiler knows where to look
for all your custom classes.
For Flash, you can edit the classpath either at
the project level or globally. At the project level, select
FilePublish Settings, and
select the ActionScript Settings button in the Flash tab. For the
global classpath, select EditPreferences, and click the ActionScript Settings button. Both
open a similar dialog that allows you to edit the classpath. You
can click the + button to add a new directory to the classpath. For
example, if the com directory for a common library is stored
in C:\libraries, then you would add C:\libraries to
the classpath.
For Flex, you can only set the classpath for a
project. Using Flex Builder, right-click a project in the project
navigator, select Properties, and then select Flex/ActionScript
Build Path from the left menu. In the Source Path tab, you can add
to and edit the classpath. If you're using the SDK rather than Flex
Builder, you have to set the classpath when you're compiling your
project. Using mxmlc
(the command-line compiler
included in the Flex SDK), you can add a -source-path option, followed by a
list of classpath directories, as shown here:
mxmlc -source-path . C:\libraries ExampleApplication.as
See Also
Recipe
2.1
|