FusionCharts for Flex > Special Cases > Using Java Flex Compiler API

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 that the MXML file is present at the 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:

Creating the Java File

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. In the following code, we demonstrate how to accomplish this.

      

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 declared a new MXML application associated with the file "demo_SWC_app.mxml". After this we have declared the necessary library files in the application configuration. For our case, the library file is "FusionCharts.swc" present at the "libs" directory. Finally, we declare the output file "demo_SWC_app.swf" as the target compilation file.

You should copy the above code and save it in a file called "Main.java".

Importing FusionCharts Files to your Project

In order to use FusionCharts with the Flex SDK, you must import the libraries to your project.

  1. Copy the FusionCharts.swc ShockWave Component from DISTRIBUTION_ROOT/Charts to PROJECT_ROOT/libs folder. The DISTRIBUTION_ROOT root is the location where you have downloaded and extracted the FusionCharts for Flex archive.
  2. Copy the content of the fusioncharts folder from DISTRIBUTION_ROOT/Charts to the PROJECT_ROOT. This folder holds all the chart Flash objects.
  3. Move the Main.java file to the project root.

Compiling your Application using FusionCharts SWC

Compiling the MXML file using the Flex Compiler API is a two step process. In the first step you need to compile the Main.java file to create a customized version of a MXML compiler. After this, we need to run the compiler to actually compile the MXML file into a SWF file. For us to begin you must navigate to your PROJECT_ROOT directory and set it as the present working directory.

The following code block defines the first step where you use the javac compiler to 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 the result of the compilation. We will run this file using the java command to actually compile the MXML file. The following command achieves this,

java -classpath c:\flex\lib\flex-compiler-oem.jar Main

The result will be an SWF file called "demo_SWC_app.swf". This is your final file with FusionCharts object within it.