ONDotNet.com    
 Published on ONDotNet.com (http://www.ondotnet.com/)
 See this if you're having trouble printing code examples


A Visual J# .NET Primer

by Brian Jepson
10/15/2001

Microsoft's JUMP to .NET promises tools to migrate Java programs to C#, as well as support for the Java SDK class library. When news of Visual J# (J-Sharp) reached my ears, I had to check it out.

Hello, World Example

The first thing I tried was Hello, World:

public class Hello {
  public static void main() {
    System.out.println("Hello, World");
  }
}

The compiler that comes with Visual J# is jc.exe, and it compiled the program with no complaints:

C:\FirstLook>jc Hello.java
Microsoft (R) Visual J# Compiler Version 1.00.3327
for Microsoft (R) .NET CLR version 1.0.2914
Copyright (C) Microsoft Corp 2000-2001. All rights reserved.
  
C:\FirstLook>Hello.exe
Hello, World

Curious to see what's going on under the hood, I examined Hello.exe using ildasm:

C:\FirstLook>ildasm /text Hello.exe

[...]

.method public hidebysig static void  main() cil managed
{
  .entrypoint
  // Code size       21 (0x15)
  .maxstack  8
  IL_0000:  ldsfld     class [BJLIB]java.io.PrintStream [BJLIB]java.lang.System::'out'
  IL_0005:  ldstr      "Hello, World"
  IL_000a:  call       class [BJLIB]java.lang.String [BJLIB]java.lang.String::fromConstantPoolString(string)
  IL_000f:  callvirt   instance void [BJLIB]java.io.PrintStream::println(class [BJLIB]java.lang.String)
  IL_0014:  ret
} // end of method Hello::main

  [...]

Well, there's some Java types in there, no doubt about that. A peek at \Program Files\Microsoft Visual J# .NET\Framework\sdk\bjlib.dll shows there is a whole lot of Java-compatibility going on:

Screen shot.
Figure 1. Some familiar package names.

NervousText Applet Example

I figured I'd try compiling one more program, so I grabbed the NervousText applet from the Java distribution and added some scaffolding so I could compile it into an executable. For good measure, I called into the .NET Framework Class Libraries to determine the operating system version:

public static void main() {

  System.OperatingSystem os = System.Environment.get_OSVersion();
  String title = "Nervous Text - " + os.ToString();

  Frame f = new Frame(title);
  NervousText n = new NervousText();
  n.init();
  n.start();
  f.add(n);
  f.pack();
  f.show();
}

It worked -- freaky! Although the .NET Framework Class Libraries are similar in some ways to Java, this seems like quite a feat to me. At the very least, it was probably a ton of work; writing hundreds of wrapper classes doesn't seem like the most exciting chore to me. Still, this is weird, a bit useful, and sure to stir up some healthy controversy. For some insight into the work that went into this, see this link. The Microsoft India site has more details.

Screen shot.
Figure 2. Jumpin' Java!

No Applet Support in .NET

One of the tools that comes with Visual J# is jbimp, the Java Binary to .NET Assembly Converter. It turns Java bytecode into MSIL (Microsoft Intermediate Language). Sounds like an innocent tool to help Visual J++ developers expose their .jars and .classes to .NET applications.

Programming C#Programming C#
By Jesse Liberty
July 2001
0-596-00117-7, Order Number: 1177
680 pages, $39.95

As of this writing, .NET doesn't support applets: I can't develop a .NET application and run it in my browser. But I am sure that such a thing will come to pass. A .NET applet would look more like an ActiveX control than a Java applet, since there is no VM per se (IL is JIT-compiled to native code and enjoys extensive runtime support from .NET).

Consider this:

  1. J# only supports JDK 1.1.4. This may seem like a major shortcoming, but see #2 and #3.
  2. Microsoft is already shipping versions of Windows without a Java VM. If you want to, you can install Microsoft's VM into IE. That Java VM is also limited to JDK 1.1.4 (the plot thickens).
  3. When someone writes a Java applet for general consumption, they want it to run on Internet Explorer and Netscape. Per #2, that applet is going to be targeted to JDK 1.1.4, since that is the latest Java version that supports both browsers!

So why does Microsoft need a Java VM for Internet Explorer anymore? They can just add .NET runtime support to IE, and incorporate jbimp into IE. When IE downloads an applet, it can convert it to MSIL, pre-JIT it into native code, and then cache it. There will be a penalty the first time an applet is ever loaded on a computer, but subsequent visits to the same applet will be very fast, even if those visits occur between reboots.

Again, for more Visual J# .NET, visit JUMP to .NETsite.

Brian Jepson is an O'Reilly editor, programmer, and co-author of Mac OS X Panther for Unix Geeks and Learning Unix for Mac OS X Panther. He's also a volunteer system administrator and all-around geek for AS220, a non-profit arts center in Providence, Rhode Island. AS220 gives Rhode Island artists uncensored and unjuried forums for their work. These forums include galleries, performance space, and publications. Brian sees to it that technology, especially free software, supports that mission. You can follow Brian's blog here.


Return to the .NET DevCenter.

Copyright © 2009 O'Reilly Media, Inc.