In this article I discuss VirtualBox as a useful tool for Java development and I cover how to configure and use the different available network adapters.

VirtualBox

VirtualBox is a virtualization software package. It can be installed on almost any operating system(distributions for Windows, Mac and Linux are available). The operating system it is installed on is called the host. Within the application, additional operating systems, each known as a guest OS, can be added and run.
For example, if you are on a Ubuntu host machine, you can add a Windows Vista guest OS to your VirtualBox, which will enable you to run Windows Vista from within your Ubuntu OS.

VirtualBox in Java development

VirtualBox has a lot of uses. A popular one is Linux users running a Windows instance when they need the occasional Windows program that doesnt have a Linux equivalent. Another popular one is server admins setting up a new server configuration, for example when moving from Ubuntu Server to CentOS, on a virtual machine first.

For development, VirtualBox has some interesting uses as well.
VirtualBox enables developers to share development environments.
In particular, it is a great way to share databases for development.
I’ve worked on a Java web application that connected to a MySql database, a DB2 database, an OpenLdap and a Alfresco document management system. Sharing one VirtualBox image on which all of those were already installed, configured and filled in with testdata turned out to be a lot less work than installing and configuring everything on each developer machine seperately.

VirtualBox Network Adapters

One of the hassles with Virtualbox is configuring the network adapter. You usually want the guest OS to be reachable by the host OS and you usually want to have the guest OS to have network/internet access too.
With different network adapters come different options.

Host-Only

This is one of the simplest interfaces. As the name states, the guest only knows about the host and the host knows about the guest(for example, a database on the guest could be reached by the host by just using the ip of the guest in the connection string). No physical network interface is used. The guest wont have network/internet access.
Although this is supposed to work fine if you dont need internet access, i have ran into problems when trying to reach an oracle database installed on the host machine(Mac or WIndows Vista Host, Ubuntu or WinXp Guest). The command tnsping does not work, regular ping does. So it seems like the Oracle instance does not create an own interface for this type of network adapter.

Internal

Used to create a software based network, that is not visible to applications on the host or the outside world, but visible at most to other virtual machines. No physical network interface is used. I have never used this.

Bridged

This is probably the most advanced option. With this network adapter, Virtualbox connects to one of your installed network cards and exchanges network packets directly, circumventing the whole host operating system. This means that the guest will ask the available dhcp server for his own ip address and appear on the network, reachable by the outside world, as if it were a seperate physical machine.
However, this option is not always available. If the host does not have network access, Bridged mode will never work. Also, some internet providers only allow for one ip address. In this case, when the guest asks for an ip, it wont get one since the host already has the one ip, and the guest wont have network access at all. It wont be able to reach the host nor will the host be able to reach it.

NAT

Network Address Translation(NAT) is the default networking mode in VirtualBox. A guest with NAT enabled acts much like a real computer connecting to the Internet through a router. The router in this case being the VirtualBox application on the host machine. The disadvantage of NAT is that, just as with a private network behind a router, the virtual machine is invisible to the outside world, including to the host. So, the guest has network access, but no one has access to the guest.

To make a database on a NAT guest available on the network, you will need to setup port forwarding. Luckily, this is quite easy.
Imagine that we have a Oracle Express Edition database at port 1521 on the guest OS with NAT enabled and we want to access it from the host.
If we configure port forwarding like this(VirtualBox->Selected Guest Os->Settings->Network->Network Adapter NAT->Advanced->Port Forwarding):

Port forwarding with NAT on VirtualBox

(where 10.0.2.15 is the ip address on the guest OS)
the database will be reachable through jdbc from the host with a connection string like “jdbc:oracle:thin:@localhost:1521:xe”.
In the above configuration, I also added the option to ssh the guest through port 2222 on the host machine.