Friday, 15 April 2016

Developing and running Java Swing applications in the cloud with Codenvy

Developing and running Java Swing applications in the cloud with Codenvy

Last September I blogged about creating a C++ program on the web with the Codenvy IDE and running it in the cloud inside Docker containers. Link to blog post

This worked fine for applications that just display a text or work on the command line. But what about applications that have a Graphical User Interface? It turns out that Codenvy has a special kind of runner which includes a Virtual Network Computer (VNC) display in a browser window. All you have to do is pick the correct Runner and open a hyperlink.

So let's get started:

Create a new Codenvy Project:

  • Open https://codenvy.com
  • Click on the "New Project" button after you have signed up or logged in.
  • Pick an "Empty Project"
  • Click on the Java square
  • Inside the Java square click on the dropdown. It has Ant Project, Maven Project, Java and Google App Engine Project as options. Chose Java.
  • Give the Project a Name, for instance HelloWorldSwing
  • Describe the Project: Hello World in Java Swing
  • Then hit the "Create Project" button.




You land on the Project Page:




The open the project in the IDE:


Create a Hello.java program:

  • File > New > File 
  • Give it the name HelloWorld.java
  • Paste this code:


import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;


public class HelloWorld {
  
 private static void createAndShowGui() {
   JFrame frame = new JFrame("Hello World");
   frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
   JLabel label = new JLabel ("Hello World");
   label.setBorder(new EmptyBorder(20, 20, 20, 20));
   frame.getContentPane().add(label);
   frame.pack();
   frame.setLocationRelativeTo(null);
   frame.setVisible(true);
 }
  
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        
        @Override
        public void run() {
            createAndShowGui();
        }
    });
    
  }
}

Save the file:



Choose the "VNC + Java 7" Runner:



Press the green go button:
The lower part of the screen scrolls up showing the Console, Terminal and Properties tabs.
The Docker output scrolls through the Console window and when it's ready a hyperlink appears underneath the Console scrollpanel with the heading "Application:"


Click on the hyperlink to open up the grey X Window VNC tab in your browser:


Now right-click in the grey area and choose Terminal








Compile the program with the command
javac HelloWorld.java



Run the program with the command:
java HelloWorld




Observations:

1. This terminal has apt-get installed

You can therefore install any extra package you like

sudo apt-get update
sudo apt-get install <<packagename>>

2. The run container is not linked to the IDE container 

So the source files do not update in the X-Window when you update them. You need to kill the runner and start a new runner to have changes reflected.

3. Automate your runner 

The relevant lines in the VNC + Java 7 runner are:

FROM codenvy/jdk7_vnc
RUN mkdir /home/user/app
WORKDIR /home/user/app
ADD HelloWorldSwing.zip /home/user/HelloWorldSwing.zip
RUN unzip -q /home/user/HelloWorldSwing.zip -d /home/user/app

Try the following:

FROM codenvy/jdk7_vnc
RUN mkdir /home/user/app
WORKDIR /home/user/app
ADD HelloWorldSwing.zip /home/user/HelloWorldSwing.zip
RUN unzip -q /home/user/HelloWorldSwing.zip -d /home/user/app
RUN javac HelloWorld.java

This will compile the code in the target container.
But you can't RUN java HelloWorld because while the runner is starting you don't yet have a X environment. You have to wait for the hyperlink and then open the X Window tab. There you have to open the terminal and do the java HelloWorld as shown above

4. Codenvy doesn't yet have runners for Java 8

These instructions guide you through installing Java 8 into the runner: http://tecadmin.net/install-java-8-on-debian/#
I tried them and it worked. The default java command doesn't update so you need to make sure you are picking up java 8 which is lurking in the directory /usr/lib/jvm/java-8-oracle/bin/java


3 comments: