The disputes between Microsoft and Sun
Microsystems are famous. These led to the dismissal of Java from
the Microsoft platform (but you can still install it as an add-on)
and Microsoft dumping its Java clone Visual J++ product.
Microsoft decided to take another approach by
luring existing Java developers to the .NET platform. One
component of this strategy is the J# programming language. Let’s
take a closer look at this language and the many associated
Microsoft tools.
The Java clone
In late 2001, Microsoft announced its JUMP
initiative. The campaign’s goal was to enable existing Java
developers to easily jump from Java to the .NET platform. J# was a
key component in the campaign.
The
J# syntax is strikingly similar to Java–it’s even identical in
many instances. The following code sample contains a simple Java
console application for reading the contents of a URL:
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL builder = new URL(“http://www.techrepublic.com/”);
BufferedReader in = new BufferedReader(new
InputStreamReader(builder.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
} }
One amazing aspect of this code is that it
compiles with both the Sun Java and .NET J# compilers. This example
utilizes base Java packages.
However, you may run into problems when the
application gets more complicated. Here’s a brief list of what J#
doesn’t include:
- The ability to create class bytecode files.
- The ability to develop Java applets for browsers.
-
The ability to create applications that will run in a Java Virtual
Machine (JVM). -
Java Native Interface or Remote Method Invocation (RMI)
support. - Support for the sun.io.*, sun.net.*, and netscape.* packages.
The .NET Framework includes tools for working
with your Java/J# code.
J# toolset
The .NET Framework includes the following
features to help with J# development:
-
J# compiler to convert your Java language source code to Microsoft
intermediate language (MSIL) - A converter to convert Java-language bytecode into MSIL
-
Class libraries that provide some of the capabilities of the JDK
1.1.4 packages that are in Visual J++ 6.0 (Visit Microsoft’s site for a more detailed explanation of what
packages/classes are supported.) -
WFC and many of the com.ms.* packages (com.ms.lang, com.ms.dll,
com.ms.com, com.ms.win32).
The J# compiler (vjc.exe) and bytecode
converter (jbimp.exe) are located in the .NET Framework
installation directory. The default directory path for these files
is:
C:\WINNT\Microsoft.NET\Framework\v1.1.4322
J# files are saved with the .jsl file
extension, but the compiler doesn’t restrict input to only these
files. For this reason, you can easily compile a Java source file
with it. For example, I compiled the Java sample using the
following line:
vjc URLReader.java
The J# compiler contains numerous options that
you may utilize with command-line switches. This list contains a
sample of these options:
- /o–enable compiler optimization
- /debug–emit debugging information
-
/help–display help and description of available command-line
options - /out–write compiled output to specified file
The default compilation output (this may be
changed) is an .exe file containing MSIL. This executable may be
run from the command line.
Another command-line tool allows you to convert
a compiled Java file (bytecode) to its MSIL equivalent. This is
useful when the Java source code isn’t available.
The command-line is only one option for J#
development. The Visual Studio .NET IDE provides full J# support,
so it’s possible to build a full-featured application (albeit
ASP.NET, Windows Forms, command-line, Web service, and so forth)
using J#.
J# seems to be a Java clone, but C# has been
called this as well. The C# syntax closely resembles both J# and
Java.
Another avenue
C# provides another approach for Java
developers wanting to jump into .NET development. Microsoft offers
the Java Language Conversion Assistant (JLCA). It’s a tool that
converts Java code into C# for developers who want to move existing
applications to the .NET Framework. It provides a quick, low-cost
method of converting Java-language applications to C#. The current
version is 3.0 beta, and it offers the following features:
-
The conversion of J2EE 1.3 and JDK 1.3 libraries, as well as
Enterprise Java Beans, Java Authentication and Authorization
Service, Java Cryptography Extension, Java Message Service, Java
Naming and Directory Interface, and RMI. - Deep support for migrating SWING applications.
- JavaServer Pages (JSP) and servlet application conversion.
It’s fully integrated with Visual Studio .NET,
so the developer doesn’t have to leave the comforts of the IDE to
perform the conversion. The JLCA helps you leverage existing
investments, thus avoiding some of the risks of new development and
decreasing your time to market. Once the conversion is complete,
you can immediately begin using the power of the .NET Framework and
the component-oriented programming to extend the code.
Here’s a quick example of the conversion. The
following Java code is used as the input:
class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello World!”); //Display the string.
} }
The C# produced by JCLA follows:
using System;
public class HelloWorld {
[STAThread]
public static void Main(System.String[] args) {
System.Console.Out.WriteLine(“Hello World!”);
} }
TechRepublic’s free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET. Automatically sign up today!