For those that remember the very early days of Java, the language was conceived with embedded systems in mind (anyone still remember the concept of Java powered toasters?). While as far fetched as that seemed at the time the emerging field of the Internet of Things (IoT) and Machine-to-Machine (M2M) communication is promising to finally make that dream a reality. It may or may not also surprise you that Java EE has a key part to play in this area as you need many of the Java EE services in order to use embedded devices effectively. Fortunately modern Java EE application servers like GlassFish can run well on very small embedded platforms like the Raspberry Pi (if you still live in the "J2EE era" this probably also seems like science fiction).
Oracle's own Mark Heckler has a very well written and maintained blog post on how to do this almost from scratch. If you are an IoT and GlassFish enthusiast it is an excellent resource worth checking out.