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 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:

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. 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.

Importing FusionCharts Files to your Project

In order to use FusionCharts with Flex SDK, you must import the libraries to your project by following these steps:

  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 downloaded and extracted the FusionCharts for Flex archive.
  2. Copy the content of the fusioncharts folder from DISTRIBUTION_ROOT/Charts to 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 Flex Compiler API is a two step process:

  1. You need to compile the Main.java file to create a customized version of the MXML compiler.
  2. You have to run the compiler and compile the MXML file into a SWF file. To begin with, you must navigate to your PROJECT_ROOT directory and set it as the present working directory.

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.