We will use the following meta-custom/recipes-java/java-helloworld/java-helloworld-1.0/HelloWorldSwing.java graphical Swing hello world example:
import javax.swing.JFrame; import javax.swing.JLabel; public class HelloWorldSwing { private static void createAndShowGUI() { JFrame frame = new JFrame("Hello World!"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label = new JLabel("Hello World!"); frame.getContentPane().add(label); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
To integrate this HelloWorldSwing application, we can use a Yocto meta-custom/recipes-java/java-helloworld/java-helloworld_1.0.bb recipe, as follows:
DESCRIPTION = "Simple Java Swing hello world application" SECTION = "examples" LICENSE = "MIT" LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4
f302" RDEPENDS_${PN} = "java2-runtime" SRC_URI = "file://HelloWorldSwing.java" S = "${WORKDIR}" inherit java-library do_compile() { mkdir -p build javac -d build `find . -name "*.java"` fastjar cf ${JARFILENAME} -C build . } BBCLASSEXTEND = "native"
The recipe is also buildable for the host native architecture. We can do this either by providing a separate java-helloworld-native recipe that inherits the native class or by using the BBCLASSEXTEND variable as we did earlier. In both cases, we could then use the _class-native and _class-target overrides to differentiate between native and target functionality. Even though Java is byte-compiled and the compiled class will be the same for both, it still makes sense to add the native support explicitly.