Java: Writing a Windows Forms application using Java

I was playing around with IKVM.NET (www.ikvm.net) and I found it very very interesting.

IKVM.NET is a Java Virtual Machine implemented on top of .NET; and though this is already kind of weird, the most interesting thing is that inside IKVM, the Java objects of your Java application are first class objects of the CLR. IKVM.NET runs on Microsoft .NET Framework and Mono as well.

IVKM.NET lets you to do several kind of things:

  • You can use the ikvm cli command as a java command replacement; that is, you can use ikvm to run Java applications on top of the CLR.
  • You can translate your .class file to a .NET assembly (.exe or .dll) using ikvmc.
  • You can access the Java clases from a .NET application.
  • You can access the .NET classes from your Java code.

Let’s see this piece of code:

using System;
using System.Windows.Forms;

namespace Test
{
  public class MainClass
  {
    public static void Main(string[] args)
    {
      Form f = new Form();
      f.Text = "From using C#";
      f.Width = 400;
      f.Height = 300;
      Button b = new Button();
      b.Text = "Click me";
      b.Width = 85;
      b.Height = 25;
      b.Top = 25;
      b.Height = 25;
      f.Controls.Add(b);
      Application.Run(f);
    }
  }
}

I want to implement the same program but using Java:

package test;

import java.util.*;
import cli.System.Windows.Forms.*;


public class Main
{
  public static void main(String... args)
  {
    Form f = new Form();
    f.set_Text("Hello");
    f.set_Width(400);
    f.set_Height(300);
    f.set_Visible(true);
    
    Button b = new Button();
    b.set_Text("Click me");
    b.set_Width(85);
    b.set_Height(25);
    b.set_Top(25);
    b.set_Left(25);
    
    f.get_Controls().Add(b);
    Application.Run(f);
  }
}

As you can see, the C# properties are mapped to Java methods with the “get_” and “set_” prefixes. The System.Windows.Forms namespace is mapped to a package called cli.System.Windows.Forms to avoid possible ambiguities with Java packages.

To compile this class, a stub (using ikvmstub) must be created to let the Java compiler know about the .NET classes.

For this example, I had to create the stubs for these DLLs:

mono ikvmstub.exe [ASSEMBLY_PATH]/mscorlib.dll
mono ikvmstub.exe [ASSEMBLY_PATH]/System.Windows.Forms.dll
mono ikvmstub.exe [ASSEMBLY_PATH]/System.dll
mono ikvmstub.exe [ASSEMBLY_PATH]/System.Runtime.Remoting.dll

After that, I had mscorlib.jar; System.Windows.Forms.jar; System.jar and System.Runtime.Remoting.jar created in my current directory.

With the stubs already created, I was able to compile my test (found in the test directory inside my current directory):

javac -cp System.Windows.Forms.jar:System.jar:System.Runtime.Remoting.jar:mscorlib.jar test/Main.java

This will create a test/Main.class file able to be run inside the ikvm Java virtual machine.

To run this class, I translated it to .NET using ikvmc:

mono ikvmc.exe test/Main.class -r:[ASSEMBLY_PATH]/System.Windows.Forms.dll

It will create a Main.exe file in my current directory that I can run directly in my Mono environment:

mono Main.exe

and we get:

Windows Forms app

Advertisements

3 thoughts on “Java: Writing a Windows Forms application using Java

  1. How can I access the user interface (click on buttons, insert strings into thextfields, etc.) from a third party application (from witch all I see is the user interface) using java. ( I used the same type of information in C# using UIAutomation but now I need for java).

    Thank you.

    1. Hi!

      It depends on the third party application you want to access. For example, if you have a Win32 application and you want to test it from Java, you should have some wrappers (using JNI) to the Win32 API able to hook to the Windows that are being activated, shown or released.

      If you have a Java Swing application you could create an application that actually calls the third party application, access to all windows created by your application [through java.awt.Frame.getFrames() or java.awt.Window.getOwnerlessWindows()] and automate the UI testing using the java.awt.Robot.

      I did not work with java.awt.Robot nor Frame.getFrames neither, but I think you could dig into them and see if they could be helpful for you.

      Thanks for reading me! ;)

  2. Hi Eto! Please I have a question about C#, How implementing contravariance and variance with Generic Colleccion! please some example!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s