The Flex compiler API lets you compile Flex applications from Java applications. You can also create Flex applications in memory and compile them to SWF files without ever having an MXML file on disk. In addition, the compiler API lets you output linkage reports and other details about your applications.
We will illustrate an example where we compile an existing MXML file using an external SWC library. For this example case we will assume that the folder libs is present within the project root and the MXML file is also at its default location in project root. We will also assume that the Flex Compiler API library file flex-compiler-oem.jar is located at c:\flex\lib\flex-compiler-oem.jar. Assuming these default settings, we compile the file as follows:
To compile an application with the API, you need to create a main thread with necessary declarations. These declarations include, setting library paths, declaring the MXML file to be compiled, and setting various compiler flags. The following code demonstrates the declaration process.
import flex2.tools.oem.*; import java.io.*;
public class Main {
public static void main(String[] args) {
try {// This declration defines a new MXML application that will be compiled // into an SWF file Application application = new Application(new File("demo_SWC_app.mxml"));
// The array of SWC library files that will be used while compiling File[] libFile = new File[] {
// Declares an instance of the FusionCharts SWC library file // present at "libs" folder new File("libs", "FusionCharts.swc") };// The default configuration of the application Configuration c = application.getDefaultConfiguration();
// The library file specifications are added to the configuration c.addLibraryPath(libFile); application.setConfiguration(c);
// Defines the target file for the compiler application.setOutput(new File("demo_SWC_app.swf"));} catch (Exception ex) { ex.printStackTrae();
}
}
}
In the above code, we have a new MXML application associated with the file demo_SWC_app.mxml. Following which, we declared the necessary library files in the application configuration. In this case, the library file FusionCharts.swc is present in the libs directory. Finally, we declare the output file demo_SWC_app.swf as the target compilation file.
You are requested to copy the above code and save it in a file named Main.java.
In order to use FusionCharts with Flex SDK, you must import the libraries to your project by following these steps:
Compiling the MXML file using Flex Compiler API is a two step process:
The following code block describes the first step in which javac compiler is used for compile the Main.java file.
javac -classpath c:\flex\lib\flex-compiler-oem.jar Main.java
The classpath declaration in the above command specifies the location of the Java Flex compiler libraries.
A Main.class file should have been created as a result of the compilation. We will now run this file using java command to actually compile the MXML file. This is done through following command:
java -classpath c:\flex\lib\flex-compiler-oem.jar Main
The result is an SWF file named demo_SWC_app.swf. It is your final file, which contains the FusionCharts object.