Generating the SWT JNI Code for SWT 3.4

All of the C code used by SWT is generated by the JNIGeneratorApp application from the project in Eclipse CVS. This page describes how to use this tool when fixing bugs or adding features to SWT.

Adding native methods

  1. Add the function prototype in, copying the style and structure of the other functions in that file.

  2. Run JNIGeneratorAppUI from This is a GUI application for editing the metadata and a front end to the generation phase. Select the appropriate class in the list and then find the method you added in


  3. Set any flags and modify the casts for any primitive types.

  4. Hit Generate All to generate the C code and metadata files.

  5. Refresh the org.eclipse.swt project to tell Eclipse about the C changes, and refresh the project to see the metadata changes.

  6. Compile the new C code and copy the new libraries to the appropriate fragment. To compile the code, right-click on the build.xml file in the Eclipse SWT PI/ws/library directory for your window system, and choose "Run As -> Ant Build...". Select the JRE tab, and check "Run in the same JRE as the workspace". Select the Refresh tab, and check "Refresh resources upon completion" to refresh your workspace after running the build to ensure Eclipse picks up the fresh binaries. Now select the "Run" button.


Adding structs

Often, methods return values or require parameters as a C struct. To add a new structure, simply add a new class in the same package as the file which matches the definition of the C structure. This will be picked up and correctly handled by JNIGeneratorApp.

If a struct is input-only or output-only, this information can be used by JNIGeneratorApp to generate more efficient C code. Use the flags attribute of the native function to indicate this in the metadata.

More Hints

  • For GTK+ and Motif, all native functions must be wrapped in locking code. When adding functions, simply copy the style and structure of the other native functions.
  • Many functions on Mac OS X return structs which use unions. For an example of how to add the markup for a union, see the ControlFontStyleRec structure and its metadata.

And there you go! If you're having trouble, please post to the SWT mailing list or file a new bug in bugzilla.