Skip to content

Archive

Category: Created by Tyler Burton

There has been quite a bit of activity on The Linux Experiment over the past little while. Check out the site here or quickly jump to the post that I wrote below.

Big distributions, little RAM 3

How do the ‘big time’ distributions handle on constrained hardware? Take a look.

How to install sun-java6-jdk and Netbeans in Ubuntu 11.10

A simple process to install the official SunOracle Java JDK and Netbeans IDE in the latest Ubuntu.

Ubuntu 11.10′s WiFi crashes my router

The new Ubuntu release is pretty good. Unfortunately it also causes my router to crash.

Gentoo (A.K.A. “Compiling!”)

My first post for the second Linux Experiment where I speak about my Gentoo first impressions.

How to enable reboot/shutdown in KDE on Gentoo

Closed source AMD/ATI drivers, wireless networking and Flash in Gentoo

How to update your (whole) Gentoo system

A trio of small posts that walk new Gentoo users through setting up and doing some basic things in their new desktop.

How to play Red Alert 2 on Linux

I managed to get this classic game to run great on Linux. It even includes a bit of a hack that allows you to play LAN games. I don’t think you can even do that on Windows any more.

Oh Gentoo

My final post of the second Linux Experiment. Includes my conclusions on running Gentoo as a day-to-day desktop system.

With no real alternative to Xcode on non-Mac platforms there is a real lack of a genuine development environment for Objective-C. With projects like GNUstep picking up the Objective-C runtime portion of the equation I’ve decided to try my hand at filling the other gap by creating a very simple IDE strictly for Objective-C. My goals were simple: create a basic IDE written in Objective-C that provides syntax highlighting, one button program compilation, and (if I could get it to work) some form of auto-complete or a suggestion system.

For my project I decided to target the cross-platform GTK+ because while Objective-C has the AppKit GUI libraries they just don’t look native enough for my liking. In addition because Objective-C is backwards compatible with all C code I could easily embed the GTK+ calls within wrapped objects and methods.

With a simple press of a button the program is compiled and run in the terminal just as you would expect.

As you can see I’ve so far succeeded in my simple goals. Although this project is still very much a work in progress I do hope to be able to release the source to it once I get it to a point where it is cleaned up and feature complete.

As sort of follow-up-in-spirit to my older post I decided to share a really straight forward way to use Objective-C to build GTK+ applications.

Objective-what?

Objective-C is an improvement to the iconic C programming language that remains backwards compatible while adding many new and interesting features. Chief among these additions is syntax for real objects (and thus object-oriented programming). Popularized by NeXT and eventually Apple, Objective-C is most commonly seen in development for Apple OSX and iOS based platforms. It ships with or without a large standard library (sometimes referred to as the Foundation Kit library) that makes it very easy for developers to quickly create fast and efficient programs. The result is a language that compiles down to binary, requires no virtual machines (just a runtime library), and achieves performance comparable to C and C++.

Marrying Objective-C with GTK+

Normally when writing a GTK+ application the language (or a library) will supply you with bindings that let you create GUIs in a way native to that language. So for instance in C++ you would create GTK+ objects, whereas in C you would create structures or ask functions for pointers back to the objects. Unfortunately while there used to exist a couple of different Objective-C bindings for GTK+, all of them are quite out of date. So instead we are going to rely on the fact that Objective-C is backwards compatible with C to get our program to work.

What you need to start

I’m going to assume that Ubuntu will be our operating system for development. To ensure that we have what we need to compile the programs, just install the following packages:

  1. gnustep-core-devel
  2. libgtk2.0-dev

As you can see from the list above we will be using GNUstep as our Objective-C library of choice.

Setting it all up

In order to make this work we will be creating two Objective-C classes, one that will house our GTK+ window and another that will actually start our program. I’m going to call my GTK+ object MainWindow and create the two necessary files: MainWindow.h and MainWindow.m. Finally I will create a main.m that will start the program and clean it up after it is done.

Let me apologize here for the poor code formatting; apparently WordPress likes to destroy whatever I try and do to make it better. If you want properly indented code please see the download link below.

MainWindow.h

In the MainWindow.h file put the following code:

#import <gtk/gtk.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>

//A pointer to this object (set on init) so C functions can call
//Objective-C functions
id myMainWindow;

/*
* This class is responsible for initializing the GTK render loop
* as well as setting up the GUI for the user. It also handles all GTK
* callbacks for the winMain GtkWindow.
*/
@interface MainWindow : NSObject
{
//The main GtkWindow
GtkWidget *winMain;
GtkWidget *button;
}

/*
* Constructs the object and initializes GTK and the GUI for the
* application.
*
* *********************************************************************
* Input
* *********************************************************************
* argc (int *):    A pointer to the arg count variable that was passed
*             in at the application start. It will be returned
*            with the count of the modified argv array.
* argv (char *[]):     A pointer to the argument array that was passed in
*            at the application start. It will be returned with
*            the GTK arguments removed.
*
* *********************************************************************
* Returns
* *********************************************************************
* MainWindow (id):    The constructed object or nil
* arc (int *):        The modified input int as described above
* argv (char *[]):    The modified input array modified as described above
*/
-(id)initWithArgCount:(int *)argc andArgVals:(char *[])argv;

/*
* Frees the Gtk widgets that we have control over
*/
-(void)destroyWidget;

/*
* Starts and hands off execution to the GTK main loop
*/
-(void)startGtkMainLoop;

/*
* Example Objective-C function that prints some output
*/
-(void)printSomething;

/*
********************************************************
* C callback functions
********************************************************
*/

/*
* Called when the user closes the window
*/
void on_MainWindow_destroy(GtkObject *object, gpointer user_data);

/*
* Called when the user presses the button
*/
void on_btnPushMe_clicked(GtkObject *object, gpointer user_data);

@end

MainWindow.m

For the class’ actual code file fill it in as show below. This class will create a GTK+ window with a single button and will react to both the user pressing the button, and closing the window.

#import “MainWindow.h”

/*
* For documentation see MainWindow.h
*/

@implementation MainWindow

-(id)initWithArgCount:(int *)argc andArgVals:(char *[])argv
{
//call parent class’ init
if (self = [super init]) {

//setup the window
winMain = gtk_window_new (GTK_WINDOW_TOPLEVEL);

gtk_window_set_title (GTK_WINDOW (winMain), “Hello World”);
gtk_window_set_default_size(GTK_WINDOW(winMain), 230, 150);

//setup the button
button = gtk_button_new_with_label (“Push me!”);

gtk_container_add (GTK_CONTAINER (winMain), button);

//connect the signals
g_signal_connect (winMain, “destroy”, G_CALLBACK (on_MainWindow_destroy), NULL);
g_signal_connect (button, “clicked”, G_CALLBACK (on_btnPushMe_clicked), NULL);

//force show all
gtk_widget_show_all(winMain);
}

//assign C-compatible pointer
myMainWindow = self;

//return pointer to this object
return self;
}

-(void)startGtkMainLoop
{
//start gtk loop
gtk_main();
}

-(void)printSomething{
NSLog(@”Printed from Objective-C’s NSLog function.”);
printf(“Also printed from standard printf function.\n”);
}

-(void)destroyWidget{

myMainWindow = NULL;

if(GTK_IS_WIDGET (button))
{
//clean up the button
gtk_widget_destroy(button);
}

if(GTK_IS_WIDGET (winMain))
{
//clean up the main window
gtk_widget_destroy(winMain);
}
}

-(void)dealloc{
[self destroyWidget];

[super dealloc];
}

void on_MainWindow_destroy(GtkObject *object, gpointer user_data)
{
//exit the main loop
gtk_main_quit();
}

void on_btnPushMe_clicked(GtkObject *object, gpointer user_data)
{
printf(“Button was clicked\n”);

//call Objective-C function from C function using global object pointer
[myMainWindow printSomething];
}

@end

main.m

To finish I will write a main file and function that creates the MainWindow object and eventually cleans it up. Objective-C (1.0) does not support automatic garbage collection so it is important that we don’t forget to clean up after ourselves.

#import “MainWindow.h”
#import <Foundation/NSAutoreleasePool.h>

int main(int argc, char *argv[]) {

//create an AutoreleasePool
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

//init gtk engine
gtk_init(&argc, &argv);

//set up GUI
MainWindow *mainWindow = [[MainWindow alloc] initWithArgCount:&argc andArgVals:argv];

//begin the GTK loop
[mainWindow startGtkMainLoop];

//free the GUI
[mainWindow release];

//drain the pool
[pool release];

//exit application
return 0;
}

Compiling it all together

Use the following command to compile the program. This will automatically include all .m files in the current directory so be careful when and where you run this.

gcc `pkg-config –cflags –libs gtk+-2.0` -lgnustep-base -fconstant-string-class=NSConstantString -o “./myprogram” $(find . -name ‘*.m’) -I /usr/include/GNUstep/ -L /usr/lib/GNUstep/ -std=c99 -O3

Once complete you will notice a new executable in the directory called myprogram. Start this program and you will see our GTK+ window in action.

If you run it from the command line you can see the output that we coded when the button is pushed.

Wrapping it up

There you have it. We now have a program that is written in Objective-C, using C’s native GTK+ ‘bindings’ for the GUI, that can call both regular C and Objective-C functions and code. In addition, thanks to the porting of both GTK+ and GNUstep to Windows, this same code will also produce a cross-platform application that works on both Mac OSX and Windows.

Source Code Downloads

Source Only Package
File name: objective_c_gtk_source.zip
File hashes: Download Here
File size: 2.4KB
File download: Download Here

Java Web Start is a technology that allows easy deployment of Java based software through a web browser. The advantages of this framework are numerous but one nice thing is that it allows you far more freedom then the completely sandboxed Java applet. In this post I will detail how I converted my Hash Verifier application to run right from the browser.

Java Network Launching Protocol (JNLP)

The javax.jnlp libraries provide the functionality needed to launch the application from within a web browser. This is done through the use of a specially crafted XML file called a jnlp file. This file specifies the application information including the location of the executable jar and any class libraries associated with it. The nice thing about this is that it allows you to specify different class files spending on the platform it is being run on. This is exactly what I needed in order to get the correct SWT libraries loaded on the target platform.

As an example of what a jnlp file looks like here is the file I created for my Hash Verifier application:

<?xml version=”1.0″ encoding=”utf-8″?>
<jnlp spec=”1.0+” codebase=”http://www.tylerburton.ca/webstart” href=”hv.jnlp”>
<information>
<title>Hash Verifier</title>
<vendor>tylerburton.ca</vendor>
<homepage href=”http://tylerburton.ca” />
<description>A graphical file digest generator</description>
<description>Hash Verifier</description>
</information>

<security>
<all-permissions />
</security>

<resources>
<j2se version=”1.5+” />
<jar href=”HashVerifier.jar” />
</resources>

<resources os=”Windows” arch=”x86″>
<jar href=”http://www.tylerburton.ca/swt/3.6.1/swt-win32-windows-x86.jar” />
</resources>

<resources os=”Windows” arch=”x86_64″>
<jar href=”http://www.tylerburton.ca/swt/3.6.1/swt-win32-windows-x86_64.jar” />
</resources>

<resources os=”Windows” arch=”amd64″>
<jar href=”http://www.tylerburton.ca/swt/3.6.1/swt-win32-windows-x86_64.jar” />
</resources>

<resources os=”Linux” arch=”ppc”>
<jar href=”http://www.tylerburton.ca/swt/3.6.1/swt-gtk-linux-ppc.jar” />
</resources>

<resources os=”Linux” arch=”ppc64″>
<jar href=”http://www.tylerburton.ca/swt/3.6.1/swt-gtk-linux-ppc64.jar” />
</resources>

<resources os=”Linux” arch=”x86_64″>
<jar href=”http://www.tylerburton.ca/swt/3.6.1/swt-gtk-linux-x86_64.jar” />
</resources>

<resources os=”Linux” arch=”amd64″>
<jar href=”http://www.tylerburton.ca/swt/3.6.1/swt-gtk-linux-x86_64.jar” />
</resources>

<resources os=”Linux” arch=”x86″>
<jar href=”http://www.tylerburton.ca/swt/3.6.1/swt-gtk-linux-x86.jar” />
</resources>

<resources os=”Linux” arch=”i386″>
<jar href=”http://www.tylerburton.ca/swt/3.6.1/swt-gtk-linux-x86.jar” />
</resources>

<resources os=”Linux” arch=”i686″>
<jar href=”http://www.tylerburton.ca/swt/3.6.1/swt-gtk-linux-x86.jar” />
</resources>

<resources os=”Mac” arch=”x86_64″>
<jar href=”http://www.tylerburton.ca/swt/3.6.1/swt-cocoa-mac-x86_64.jar” />
</resources>

<resources os=”Mac” arch=”amd64″>
<jar href=”http://www.tylerburton.ca/swt/3.6.1/swt-cocoa-mac-x86_64.jar” />
</resources>

<resources os=”Mac” arch=”x86″>
<jar href=”http://www.tylerburton.ca/swt/3.6.1/swt-cocoa-mac-x86.jar” />
</resources>

<resources os=”Mac” arch=”i386″>
<jar href=”http://www.tylerburton.ca/swt/3.6.1/swt-cocoa-mac-x86.jar” />
</resources>

<resources os=”Mac” arch=”i686″>
<jar href=”http://www.tylerburton.ca/swt/3.6.1/swt-cocoa-mac-x86.jar” />
</resources>

<resources os=”Mac”>
<jar href=”http://www.tylerburton.ca/swt/3.6.1/swt-carbon-mac.jar” />
</resources>

<resources os=”SunOS” arch=”sparc”>
<jar href=”http://www.tylerburton.ca/swt/3.6.1/swt-gtk-solaris-sparc.jar” />
</resources>

<resources os=”SunOS” arch=”x86″>
<jar href=”http://www.tylerburton.ca/swt/3.6.1/swt-gtk-solaris-x86.jar” />
</resources>

<application-desc main-class=”ca.tylerburton.hashverifier.HashVerifierMain” />

</jnlp>

As you can see I have outlined all of the platform configurations that I am going to be compatible with. Java Web Start takes care of the rest for me automatically.

Digital signatures

The final step is to digitally sign the executable jar. This is required because when deploying an application that is not sandboxed, or one that requires access to low-level functions and libraries (as is the case in SWT), the end user needs to know that the code they are receiving came directly from the source. The easiest way to do this is to follow the instructions here. Unfortunately unless you are willing to pay to have your certificate signed by a trusted certificate authority the end user will need to click OK through a big security warning before they can use your program. It’s a small issue to overcome but if Minecraft can do it without too much of a headache I’m sure you can too.

Easy deployment

It’s as simple as that. I didn’t have to re-write my code, heck I didn’t even have to adjust a single line and it all just works. To see the result on your platform click here.

Note: unfortunately there is an issue with the Mac version of Java Web Start & SWT that prevents it from working correctly. Until Apple fixes this, or OpenJDK takes over and does it, SWT applications cannot be started correctly on that platform through Web Start.

Back in this previous post I mentioned the possibility of putting up a sort of how-to guide on SWT programming. Well I’ve finally found some time to do so. I will try to make this as step-by-step as possible so that anyone reading this will completely understand what I’m doing.

What you’ll need

I’m making the following assumptions before starting:

  1. That you’ve already installed the Java Development Kit (not just the Java Runtime)
  2. That you’ve installed Eclipse. You can use an alternative but all of my examples will be based on Eclipse. I’m also going to assume that you are at least familiar enough with Eclipse to compile a Java program.
  3. That you have some understanding of Java or at least some programming experience.
  4. That you have the SWT library for your platform downloaded. You can find this on the download page here.
  5. That you’ve created a project in Eclipse (or your IDE) for this walk through.

Import SWT

The first thing you want to do is import the SWT libraries that you have downloaded as an Eclipse project. Start by creating a new project specifically for SWT. For instance I named mine org.eclipse.swt (following the Java standard namespace naming conventions). Next import the SWT files into that project. To do this simply click File > Import and then choose Archive File under the General folder.

Then browse to the zip file you downloaded and choose the project to import this to (again in my example this project is org.eclipse.swt). When all is said and done you should have a project in your workspace for SWT similar to the one I’ve got here:

SWTify your project

To finish adding the SWT libraries into your project we need to add them to your project’s class path. Right-click on your project and click properties. On the left hand side of the window you will see an item called Java Build Path. On that page there will be a number of tabs: Source, Projects, Libraries, etc. Click Projects and then click the Add button. Select the org.eclipse.swt project and click OK. You should see something like this indicating that the SWT libraries will be included in your project’s build path.

We are now ready to begin programming!

The default SWT program

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class MySWTProject {

     public static void main(String[] args) {

          Display display = new Display();
          Shell shell = new Shell(display);

          shell.open();

          while(!shell.isDisposed()) {
               if(!display.readAndDispatch()) {
                    display.sleep();
               }
          }

          display.dispose();
     }
}

OK so what does it mean. In SWT all of the graphical things that you can do are tied to a Display object. A Display is essentially a link to the OS’ underlying graphics output. What we are talking about is not a monitor – in fact a Display can contain multiple monitors – but rather a more abstracted concept of graphics. If this sounds confusing don’t worry, for the most part that’s all you’ll really need to know about the Display object.

A Shell should be a little easier to understand. It represents a window, the base application container. In fact if you run the above program you should see something like this:

A Shell can be manipulated in all of the ways that you think it would be. For instance you can give it a title by using the setText() method.

shell.setText(“Here is my window title”);

The open() method tells the Shell to start displaying itself. You can also do some interesting things like using the pack() method to size the Shell in order to make it just large enough to hold whatever you put into it.

The little while loop part first checks to make sure that our shell still exists and then executes any input that we may want our application to respond to (via the readAndDispatch() method). Finally we dispose of all resources we have requested from the OS by calling dispose() on the Display object. This is important because unlike regular Java programming we do not get automatic garbage collection in SWT. There is a silver lining however – calling dispose() on an object like Display will automatically call dispose() on any children that are registered with it. That is why we didn’t need to call dispose() on our Shell.

OK I get it, now show me a button

Interaction is very easy to add. For instance we can add a button to our Shell above by simply adding the following code after the line where we create our Shell.

     Button button = new Button(shell, SWT.PUSH);
     button.setText("Push Me");
     button.pack();

Of course you will also need to import the Button library for this to work. In the constructor we pass in the shell (where we want to attach this button, similar to how we attached our shell to the Display object) and a flag called SWT.PUSH. SWT.PUSH tells the button to act as a push button. Next using setText() again we set the button label. Finally using the pack() method, as described above, we can resize the button to be just large enough. If you re-run the program now you will should see something like the following:

Pretty simple stuff. We can even add an action listener that will respond when the button is pressed by using code like this:

button.addSelectionListener(new SelectionListener() {
    public void widgetDefaultSelected(SelectionEvent e) {
          widgetSelected(e);
     }

     public void widgetSelected(SelectionEvent e) {
          System.out.println("The button was pressed!");
     }
});

Again very simple. I hope that this very brief introduction will help people realize that SWT programming is very similar to regular AWT/Swing GUI creation (although each has their own unique weirdness). If you would like to download the sample code file that I used to create this you can do so here. Please let me know if you have any questions about what I have said above, I’d be happy to help. On the other hand if you want a more in-depth look feel, free to hop over to my Hash Verifier post and grab a copy of the source there. The Eclipse Foundation also has an excellent code snippet section here that will show you how to use each GUI widget.

Happy programming.

After reading up on how you can use SWT to give Java a more native look and feel I was interested in giving it a shot first hand. I decided to break out old faithful (Hash Verifier) and re-write it completely in Java/SWT. The end result was an application that has essentially equivalent functionality (see below for differences) but is completely cross-platform. No matter what operating system you end up running this on (Windows, Mac OSX, Linux) it should have a native look and feel. I will explain how I accomplished this literal “write once, run anywhere” code below.

Easy deployment

The download consists of three different programs. The first, and main, named HashVerifier.jar is not actually the Hash Verifier program, but rather a simple sequential application launching system that I wrote to make cross-platform deployment even easier. When you run HashVerifier.jar, either by double-clicking on it or by running it from the terminal with java -jar HashVerifier.jar, it opens up the included rules file and begins to execute the lines one after another. If there is a special platform specific file (such as rules.mac) it will run that instead. Let’s take a look at rules:

java -jar AdaptiveSWT swt.jar
java -XX:+AggressiveOpts -XX:+UseParallelGC -jar HashVerifierApp

As you can see each line is a separate command to be run.

Getting that right look

When HashVerifier.jar starts it reads each line and runs that as a shell command. The line java -jar AdaptiveSWT swt.jar starts the second included program. AdaptiveSWT uses Java’s built in cross-platform UI toolkit, Swing, and displays a small dialog while it downloads the platform’s native SWT library. This is necessary because otherwise I would need to create separate downloads for each platform, each with their unique native SWT library. By using AdaptiveSWT I can instead let it handle all of the dirty work for me. This is really the trick that lets me release exactly one version of the program that looks correct for everyone. The AdaptiveSWT program is even smart enough to know not to re-download the library if you already have it. That being said if you ever do have an issue where you need to re-download the library either delete the swt.jar file in the program directory or change the first line to java -jar AdaptiveSWT -force swt.jar. This forces the download to happen regardless of it you already have it or not.

Hash Verifier

The next line logically starts the actual Hash Verifier application (with a couple of extra performance enhancing flags passed along as well). This version should work almost identically as the previous with a few key differences:

  • I have removed the Export to Hash File menu option as I simply just didn’t use it enough to warrant me spending the time to re-develop it.
  • I have removed the old Help menu option and replaced it with a visit website option.
  • RIPEMD-160 has been replaced with MD2, mostly because that is what the JVM supports “out-of-the-box”.

That’s about it. Please let me know if you have any issues running it on your platform of choice.

Requirements

  • OS: Windows, Mac OSX or Linux (Gtk+)
  • Internet connection
  • Java JRE (targeted at JavaSE-1.6 but might work on the previous version as well)

As before the binary only package contains just the executables, whereas the all package contains the source code as well.

Binary Only Package All Package
File name: hash_verifier_0_3_0_0_binary.zip hash_verifier_0_3_0_0_all.zip
File hashes: Download Here
GPG signature: Download Here Download Here
Screenshots:
License: (LGPL) View Here
Version: 0.3.0.0
File size: 68.6KB 107.1KB
File download: Download Here Download Here

NOTE: This software was tested on Windows Vista, Mac OS 10.6 and Linux Mint 9. While it may (and should) run on similar platforms I have not been able to test this. Please remember that this software is provided “as is” and without any sort of warranty. Use at your own risk.

That’s right an update to your favourite hash verification program! :P

This update includes a few new features that some of you might find useful. It also includes help documentation which walks you through how to use it!

New Features

  • Menu strip for even easier use
  • Export features allows you to automatically write all of the hashes to a single file
  • About dialog that provides information about the program
  • Help documentation

Requirements:

  • All platforms: .NET 2.0+ / Mono, a graphical display
  • *nix platforms: WinForms (identified as System.Windows.Forms)

As always the binary only package contains just the executable, whereas the all package contains the source code as well.

Binary Only Package All Package
File name: hash_verifier_0_2_0_0_binary.zip hash_verifier_0_2_0_0_all.zip
File hashes: Download Here
GPG signature: Download Here Download Here
Screenshots: Screenshot 1 Screenshot 2
License: (LGPL) View Here
Version: 0.2.0.0
File size: 171.5KB 530.1KB
File download: Download Here Download Here

As someone who has recently begun to experiment with the Linux operating system I have also been introduced to .NET’s Linux’s cousin Mono. This has made me question what the best cross-platform program language to use is. I am familiar with both Java and various .NET languages (Visual Basic & C#) so I decided to run a few tests to see what the resource usage on my Linux laptop is like between these two competing platforms.

Hardware

CPU Information

  • Processor (CPU): Intel Core2Duo CPU P8600 @ 2.40GHz

Memory Information

  • Total Memory (RAM): 4GiB
  • Swap Partition Size: ~6GiB

OS Information

  • OS: Linux 2.6.30.9-102.fc11.x86_64
  • System: Fedora  11
  • Desktop Environment: KDE 4.3.3

Display Info

  • Model: ATI Mobility Radeon HD 4670
  • Driver: 2.1.9026 FireGL

Hard Drive Info

  • Memory: 320GiB 7,200 RPM SATA

Software

I am using Java version 1.6.0 and Mono version 2.4.2.3 for these tests.

Test Setup

For the following tests I have provided source code for both the Java and the C# implementations. I then ran the programs and checked their CPU and memory usage.

Test I

Simple write/read test to the console. CPU usage was recorded before the read.

Java code:

import java.io.IOException;

public class TestI {

     public static void main(String[] args) {

          System.out.println(“Hello World”);

          try {
               System.in.read();
          } catch (IOException e) {
               e.printStackTrace();
          }
     }

}

C# code:

using System;

namespace TestI
{
     class MainClass
     {
          public static void Main(string[] args)
          {
               Console.WriteLine(“Hello World!”);

               try {
                    Console.Read();
               }catch(Exception e){
                    Console.Write(e.StackTrace);
               }

          }
     }
}

Results:

Java C#
Memory Usage: 5.6MiB 2.2MiB
CPU Usage: 0% 0%

Test II

Simple primitive variable usage. CPU usage was recorded before the read.

Java code:

import java.io.IOException;

public class TestII {

     public static void main(String[] args) {

          byte b = 0;
          short s = 0;
          int i = 0;
          long l = 0;
          float f = 0;
          double d = 0;
          boolean bo = true;
          char c = ‘a’;

          //set them all to something
          b = 1;
          s = 1;
          i = 1;
          l = 1;
          f = 1;
          d = 1;
          bo = false;
          c = ‘b’;

          try {
               System.in.read();
          } catch (IOException e) {
               e.printStackTrace();
          }

     }

}

C# code:

using System;

namespace TestII
{
     class MainClass
     {
          public static void Main(string[] args)
          {
               byte b = 0;
               short s = 0;
               int i = 0;
               long l = 0;
               float f = 0;
               double d = 0;
               bool bo = true;
               char c = ‘a’;

               //set them all to something
               b = 1;
               s = 1;
               i = 1;
               l = 1;
               f = 1;
               d = 1;
               bo = false;
               c = ‘b’;

               try {
                    Console.Read();
               }catch(Exception e){
                    Console.Write(e.StackTrace);
               }
          }
     }
}

Results:

Java C#
Memory Usage: 5.6MiB 2.1MiB
CPU Usage: 0% 0%

Test III

Simple primitive variable usage, this time with arrays. Same as above but with arrays of 10,000 for each primitive. The arrays were set using loops similar in structure to the following:

for(int x=0; x<10000; x++)
{
     b[x] = 1;
}

Results:

Java C#
Memory Usage: 5.9MiB 2.4MiB
CPU Usage: 0% 0%

Test IV

Simple object usage. CPU usage was recorded before the read.

Java code:

import java.io.IOException;

public class TestIV {

     public static void main(String[] args) {

          SimpleClass sc = new SimpleClass();

          sc.setID(5);
          sc.setName(“Hello World”);

          System.out.println(sc.getString());

          try {
               System.in.read();
          } catch (IOException e) {
               e.printStackTrace();
          }
     }

}

public class SimpleClass {

     private int _ID;
     private String _name;

     public SimpleClass()
     {
          //default constructor
     }

     public SimpleClass(int ID, String name)
     {
          _ID = ID;
          _name = name;
     }

     public int getID()
     {
          return _ID;
     }

     public void setID(int ID)
     {
          _ID = ID;
     }

     public String getString()
     {
          return _name;
     }

     public void setName(String name)
     {
          _name = name;
     }
}

C# code:

using System;

namespace TestIV
{
     class MainClass
     {
          public static void Main(string[] args)
          {
               SimpleClass sc = new SimpleClass();

               sc.setID(5);
               sc.setName(“Hello World”);

               Console.WriteLine(sc.getString());

               try {
                    Console.Read();
               }catch(Exception e){
                    Console.Write(e.StackTrace);
               }
          }
     }
}

using System;

namespace TestIV
{

     public class SimpleClass
     {

          private int _ID;
          private String _name;

          public SimpleClass()
          {
               //default constructor
          }

          public SimpleClass(int ID, String name)
          {
               _ID = ID;
               _name = name;
          }

          public int getID()
          {
               return _ID;
          }

          public void setID(int ID)
          {
               _ID = ID;
          }

          public String getString()
          {
               return _name;
          }

          public void setName(String name)
          {
               _name = name;
          }

     }
}

Results:

Java C#
Memory Usage: 5.6MiB 2.2MiB
CPU Usage: 0% 0%

Test V

Simple object usage, this time with arrays. Same as above but with arrays of 10,000 for each object. The arrays were set using loops similar in structure to test III:

Java code:

import java.io.IOException;

public class TestV {

     public static void main(String[] args) {

          SimpleClass[] sc = new SimpleClass[10000];

          for(int x=0; x<10000; x++)
          {
               sc[x] = new SimpleClass();
               sc[x].setID(5);
               sc[x].setName("Hello World");

               System.out.println(sc[x].getString());
          }

          try {
               System.in.read();
          } catch (IOException e) {
               e.printStackTrace();
          }
     }

}

C# code:

using System;

namespace TestV
{
     class MainClass
     {
          public static void Main(string[] args)
          {
               SimpleClass[] sc = new SimpleClass[10000];

               for(int x=0; x<10000; x++)
               {
                    sc[x] = new SimpleClass();
                    sc[x].setID(5);
                    sc[x].setName("Hello World");

                    Console.WriteLine(sc[x].getString());
               }

               try {
                    Console.Read();
               }catch(Exception e){
                    Console.Write(e.StackTrace);
               }
          }
     }
}

Results:

Java C#
Memory Usage: 10.1MiB 2.6MiB
CPU Usage: 8% 2%

Test VI

Everyone’s favourite bubble sort with 10,000 integers.

Java code:

import java.io.IOException;

public class TestVI {

     public static void main(String[] args) {

          int[] array = new int[10000];

          //fill array
          for(int i=0;i           {
               array[i] = 10000-i;
          }

          boolean swapped;

          do
          {
               swapped = false;

               for(int i=0;i                {
                    if(array[i] > array[i+1])
                    {
                         int temp = array[i+1];
                         array[i+1] = array[i];
                         array[i] = temp;

                         swapped = true;
                    }
               }
          }while(swapped);

          System.out.println(“Sorted”);

          try {
               System.in.read();
          } catch (IOException e) {
               e.printStackTrace();
          }
     }

}

C# code:

using System;

namespace TestVI
{
     class MainClass
     {
          public static void Main(string[] args)
          {
               int[] arr = new int[10000];

               //fill array
               for(int i=0;i                {
                    arr[i] = 10000-i;
               }

               bool swapped;

               do
               {
                    swapped = false;

                    for(int i=0;i                     {
                         if(arr[i] > arr[i+1])
                         {
                              int temp = arr[i+1];
                              arr[i+1] = arr[i];
                              arr[i] = temp;

                              swapped = true;
                         }
                    }
               }while(swapped);

               Console.WriteLine(“Sorted”);

               try {
                    Console.Read();
               }catch(Exception e){
                    Console.Write(e.StackTrace);
               }
          }
     }
}

Results:

Java C#
Memory Usage: 6.3MiB 2.2MiB
CPU Usage: 6% 12%

All Results

Java C#
Test I Memory Usage: 5.6MiB 2.2MiB
Test I CPU Usage: 0% 0%
Test II Memory Usage: 5.6MiB 2.1MiB
Test II CPU Usage: 0% 0%
Test III Memory Usage: 5.9MiB 2.4MiB
Test III CPU Usage: 0% 0%
Test IV Memory Usage: 5.6MiB 2.2MiB
Test IV CPU Usage: 0% 0%
Test V Memory Usage: 10.1MiB 2.6MiB
Test V CPU Usage: 8% 2%
Test VI Memory Usage: 6.3MiB 2.2MiB
Test VI CPU Usage: 6% 12%

Conclusions

Well there you have it. It seems that at this point in time Mono is not only mature enough to compete against a seasoned veteran programming language like Java, but in some cases good enough to even supersede it. This is wonderful news for people looking for alternative cross-platform high level languages.

Some of you may remember an old Windows program of mine called Hash Verifier. It was a graphical utility that allowed people to generate hashes of their files, and then compare those to known hashes, ensuring that their files had not been corrupted. Well in recent months my foray into the world of Linux has finally taken me into the realm of programming on that platform. Being primarily a .NET developer on Windows I have found the Mono project on Linux to be an absolute breath of fresh air.

“Monkey” project

The Mono project is an open source implementation of Microsoft’s .NET common language runtime and a C# compiler. On Linux the easiest way to program in a Mono language is within the project’s own integrated development environment called MonoDevelop.

C is a sharp language

C# is a very powerful programming language that falls somewhere between C and Java in terms of syntax. While my experience with C# has been limited in the past, I was easily able to pick it up quickly thanks to my background in both C and Java, as well as fellow .NET language Visual Basic.

The challenge

Digging up an old .NET project of mine, Hash Verifier, I decided to challenge myself to port the application to Mono. In order to do this I needed to accomplish the following:

  • The original application ran on Microsoft’s .NET on the Windows platform. The new application must run on both .NET on Windows and Mono on supported platforms.
  • The original application was written in Visual Basic. The new application must be written in C#.
  • The original application has a GUI powered by the native Windows.Forms. The new application needs to have a GUI that works in a similar way on all platforms.
  • The new application must be able to fully re-create all of the old application’s features and functions.

Porting = easy

I must say that porting this old application to C#/Mono was a relatively straightforward task. Although I had plenty of GUI toolkits to choose from I ended up sticking with the existing Windows.Forms. Once I had decided on using Windows.Forms as the basis for my GUI (WinForms is a free and open source implementation for non-Windows users!) I set out to create my new application. I was literally able to open the old Visual Basic GUI designer file, copy the code into my Mono workspace, change the syntax to C# and voila it worked!

In fact the only tricky part was trying to figure out a compatibility issue that .NET/Mono 2.0 seem to have with the new Windows Presentation Foundation (WPF). I’ll save you the gory details but basically drag and drop functionality would not work. I eventually rectified this issue by including a compiler flag telling .NET/Mono to execute the form in single thread apartments mode. You can see where I did this in my code by looking right above my static main function:

[STAThreadAttribute]
public static void Main()
{

}

Final result

With the application complete I must say I am impressed. Crafting and running applications for Mono is extraordinarily simple to do, seems very powerful, and the application itself only takes up a couple of MiB to run. In the future I definitely plan on doing more of this type of development now that I am using different operating systems every day.

Hash Verifier

If you are still using the old version of Hash Verifier, or if you would just like to try it out you can download the new Hash Verifier in two different ways. The package marked binary only contains just the program itself and the relevant documentation. The package marked all contains both the program, documentation as well as the source code.

Requirements:

  • All platforms: .NET 2.0+ / Mono, a graphical display
  • *nix platforms: WinForms (identified as System.Windows.Forms)
Binary Only Package All Package
File name: hash_verifier_0_1_0_0_binary.zip hash_verifier_0_1_0_0_all.zip
File hashes: Download Here
GPG signature: Download Here Download Here
License: (LGPL) View Here
Version: 0.1.0.0
File download: Download Here Download Here

[Update: A new version has been released! Click here to go to the new software page]

On the old version of this site I had provided a popular application called Hash Verifier which did, of all things, verify file hashes on Windows. I know some people are still trying to link to it’s old location so I figured I might as well include it somewhere in the new site. So here it is!

Note: this program requires .NET to run.

Disclaimer:

This software is no longer maintained. Do not e-mail me requesting changes, updates or support. I am providing this “as is” and will not be held responsible for it’s use. Please read the “read me.txt” file included in the download for more information.

File name: hash_verifier.zip
File Hashes: Download Here
GPG signature: Download Here
File download: Download Here