Introduction
We all know and love the Visual Studio .NET® IDE, but did you know that you can use it to build server applications that run on Linux®? Discover how with Visual MainWin® for J2EE™ from Mainsoft.
Figure 1:Your ASP.NET application running on Linux ?
Take a look at Figure 1. It’s clearly an ASP.NET application, and it’s clearly running on Linux. That’s easy, you might think, you can build an ASP.NET application, run it on Microsoft® Windows® under Internet Information Services (IIS) and browse to it using a browser such as FireFox on a Linux client. You’d be right, but look again. In the screen shot, your ASPX is running on localhost, the Linux box itself. With Visual MainWin for J2EE, also known as Grasshopper, you can do this easily, without changing your existing .NET code.
Therefore, you can say that Visual Studio .NET + Grasshopper = Visual Studio.NET for Linux!
Extending your skill sets to Linux
Consider this – many companies have Linux on their radar for some form of inclusion in their strategy, be it on the desktop or in the data center. To build applications for Linux, you would probably have to learn a new skill set such as GTK or Java™. For server side or hosted applications, the logical candidate is usually J2EE, due to its cross platform nature and its well-known security, manageability, performance and scalability characteristics. However, to develop J2EE applications, you need to learn the Java language, Java Servlets, Java Server Pages, JDBC for database connection, and even Enterprise Java Beans for distributed applications.
What if, as an alternative, you could broaden the reach of your skills to Linux and other Java-enabled platforms, and as a result, extend yourself (and your resume) in a new and exciting area? What if you could do this without rewriting most of your code, and instead re-use your existing C# code? Not only that, but would you like to contribute to the Mono™ project – the creation of an open source .NET framework for Linux? Well you can, and you can do it today, with Grasshopper, a freely available download from Mainsoft.
What you can do with Visual MainWin for J2EE – A brief overview
Starting or converting projects
As you know, when you compile an application in Visual Studio .NET, it generates Microsoft Intermediate Language (MS IL), which executes on the Microsoft Common Language Runtime (CLR). Grasshopper is a plug-in to Visual Studio .NET, which takes this MS IL and converts it into Java Byte Code, which executes on a Java Virtual Machine. Grasshopper also includes J2EE implementations of ASP.NET, ADO.NET, and the most common .NET namespaces, so the required dependencies are available on your J2EE platform.
You’ll get your first indication that something new is available in Visual Studio .NET when you launch it after installing Grasshopper. This demonstrates how tightly the Visual MainWin tools are integrated within Visual Studio .NET.
Figure 2. Starting a new project in the Visual Studio .NET enivronment with Grasshopper installed.
After installing Grasshopper, launch Visual Studio, and then choose File > New to open the New Project dialog.
Notice there are two new Project Type folders here – Visual MainWin for C# and Visual MainWin for VB.NET.
In fact, there are two ways you can develop your J2EE applications for Linux using Grasshopper – either by creating a project directly from here and building it in C# or VB.NET, or by taking your existing .NET framework based project and generating a J2EE project from the .NET project using the Generate J2EE Project Wizard. You will see a little more on this later.
When you install Grasshopper, you also get a copy of Tomcat, saving you the time and trouble of installing and configuring an application server yourself. You can start or stop Tomcat via the Grasshopper group on the Start menu. Tomcat must be running for you to create any new projects, or to convert your existing .NET framework based projects to J2EE projects. By default, Tomcat runs on port 8080, which is why the New Project dialog (Figure 2) shows the default root for your Web application or Web service as localhost:8080. If you are not familiar with Tomcat, it is a cut-down J2EE application server that is used for the official reference implementation of the Java Servlet and Java Server Pages technologies.
If you have an existing .NET project that you want to convert to J2EE, it's easy too. Simply right-click your project in the Solution Explorer and select Generate J2EE Project. Grasshopper creates a new project for you, sets it up for Java, and associates your source files with this project. You can then edit, compile, deploy and debug this code on your J2EE server. You can also choose to have the original project and the converted project in the same solution and then implement a single source strategy, building your source code for both .NET and J2EE from one single Visual Studio .NET development environment instance.
Tomcat is also available for Linux, so the applications you build on Tomcat using Grasshopper will run perfectly happily on Linux too. Tomcat is the only application server that is supported by Grasshopper, so if you want to use WebLogic®, WebSphere® or JBoss®, you can with the Enterprise edition of Visual MainWin for J2EE. You can download a free 30-day evaluation of Mainsoft’s Visual MainWin v 1.6 for J2EE Enterprise Edition from Mainsoft.com.
To create a deployment Java Web Archive file (WAR), all you have to do is right-click on your project in the Solution Explorer, select Deployment Packager, choose the directory that you want to deploy to, and click OK. Visual MainWin then compiles your code and dependencies into a WAR file. To install the WAR file on your Linux-based Tomcat server, simply browse to the Tomcat Manager Console and install it from there. The Tomcat Manager Console at http://linuxboxaddress/manager/html and install it from there. Tomcat uploads and deploys the WAR file for you.
Cross-platform debugging
Figure 3. Debugging an application running on the Java platform.
One of the challenges you would expect to meet when developing an application of the .NET framework using Visual Studio .NET tools and deploying to a Java runtime, is debugging. Surely debugging cannot work when you cross compile? Well it does! In fact, it works transparently, so that you still believe you are debugging your application as if it were running on the .NET framework! Take a look at Figure 3 for an example of this. You can try this yourself: create a new Web application (using the Visual MainWin C# for J2EE projects folder), add a button to it, and enter some form behind the button event handler as shown. Don’t forget to put a breakpoint in the code. Once done, click Debug > Start or press F5. Your application will be compiled and deployed to Tomcat, and a browser will launch directing you to the ASPX page.
Click the button on the page, and you will be taken back to the Visual Studio .NET IDE, which has stopped at the breakpoint, as shown in Figure 3. As you can see, all the tools you are used to for debugging – watches, the call stack and so on – are still available. You can still step through the code and watch it execute, and if you look at the call stack, you can see which classes are running and where. It is particularly interesting to see how Grasshopper links the .NET framework and the Java specifications, though it doesn’t affect the execution of your program! In addition, you can track bugs wherever they arise, even in production servers, by connecting from Visual Studio .NET to the server, regardless of its operating system, and debug any problems from your preferred development environment!
Interfacing with existing Java or J2EE assets.
Many companies – and yours probably isn’t an exception – have assets already implemented in Java, and you will need to interface with these existing Java assets. Also, you may require using a third party add-on within your applications to implement some functionality. A good example of this is reporting, where most companies would use an add-on such as Crystal Reports to do the charting. In the Linux world, these would be implemented in Java, and available as Java Archive (JAR) files for you to include in your application. Native Java developers can use these easily by including them when they compile their code, but what about when you are building from C#?
Using Grasshopper, you are not left out. You can add references to the JAR files, and manipulate them in your code as you see fit. It’s analogous to adding references to third party assemblies. To do this, you simply right-click the References folder in your Solution Explorer, and you’ll see two new options above the existing Add Reference and Add Web Reference options:
-
Add Java Reference, which allows you to find a JAR file and create a reference to it that you can write code to. Java References are fully integrated with your development environment, so the Object Browser, the Intellisense on the code editor and the compilers all recognize the Java classes and their members in exactly the same way they recognize regular .NET ones. This allows you to code against them with the same level of productivity that you have when using the .NET framework class libraries.
-
Add EJB Reference (available in the Enterprise edition only), which allows you to find an Enterprise Java Bean (EJB) using JNDI lookups. JNDI is a directory service used to locate your EJBs and interface to them. This is not supported on Tomcat, because Tomcat does not support EJBs. If you are using Visual MainWin for J2EE Enterprise edition to build applications for EJB-enabled servers such as JBoss, WebLogic or WebSphere, you can find your EJBs using their JNDI entry, create references to them and consume them like any other object. If you need to consume EJBs, your J2EE developer who built them can give you this information.
Online help and samples
Figure 4: Grasshopper integrated help
Mainsoft found that the vast majority of Web application and Web service code transfers cleanly to Java without recoding. The places where you might have to write some new code are when you have Windows-specific dependencies, such as Platform Invoke or COM-interop, or if you have third party controls for which you don’t have the source code and which don’t have a Java implementation. To aid you in translation of your code or in understanding the issues of migration using Grasshopper, reference the online integrated help documentation, and a number of sample applications and tutorials that walk you through various tasks. The integrated help is shown in Figure 4.
The Samples and tutorials section include a broad range of application types to walk you through the steps involved to build and deploy them, or to translate and port existing applications. Samples include:
-
StockReader, which demonstrates a simple console application.
-
IBuySpy, which demonstrates a single tier Web application.
-
StocksPortfolio, which demonstrates a multi-tier Web application.
-
NorthWind, which shows you how to use ADO.NET and JDBC.
It’s important to note, particularly in the last example (NorthWind), that your ADO.NET applications that connect to SQL Server can move cleanly to Java without recoding, since the System.Data classes are implemented in J2EE too, on top of the JDBC interface. Of course, if you are porting everything, including the Database Server to Linux, then you’ll have to make some code changes, because Microsoft SQL Server is not available for Linux, and therefore you would have to use a different database and change your database code accordingly. Mainsoft certifies the following databases as compatible with Visual MainWin: PostgreSQL (bundled with Grasshopper), Oracle, IBM DB2, Sybase and MySQL.
In addition to the samples, the following tutorials walk you step-by-step through the entire process: StocksPortfolio and Commerce Starter Kit implementation from the ASP.NET resource kit.
With the full online developer guide, reference, samples and tutorials, you have the documentation as well as the tools to get you hopping towards building Linux applications in Visual Studio .NET! There are lots of resources available to help you with this on Grasshopper – Mainsoft’s developer site.
Database access with the System.Data namespace
It is easy to access data through ADO.NET using Grasshopper. This is because Visual MainWin provides an implementation of the System.Data namespace that is built on top of JDBC. You can therefore use the System.Data classes as you have always done without worrying about how JDBC handles it. The System.Data classes have been tested with leading Enterprise databases, including Microsoft SQL Server, Oracle, IBM DB2, Sybase, PostgreSQL and MySQL. The drivers for SQL Server and PostgreSQL are included with the platform should you need to work with them, and are automatically installed for you on your application server.
Conclusion
In this article, you took a whirlwind tour of some of the features of the Visual MainWin for J2EE toolkit and what they allow you to do. You have seen how you can use your existing C# or VB.NET skills to build applications in a whole new arena – for Linux! And you can do all this without learning a new language, a new dependency structure, or a new IDE. Imagine taking what you have today, and showing your boss how you ported it to Linux in just a few minutes! Try it for yourself and download the toolkit. You won’t regret it.
Legal Notice
Mainsoft and Mainwin are registered trademarks of Mainsoft Corporation. Microsoft, Visual Basic, Visual Studio, and Windows are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. WebLogic is a registered trademark of BEA Systems, Inc. WebSphere is a registered trademark of IBM Corp. Java, J2EE, and JavaBeans are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. All other company and product names may be the subject of intellectual property rights reserved by third parties.