Jar Bar guide to Install VirtualBox for Java development by Yucca Nel
After numerous years of toggling between development systems, the discovery of VirtualBox finally provided me with an ultimate solution to use when developing. After putting together a series of Java related guides , I am ready to demonstrate how we can combine a host and guest systems to work in Unison with regards to developing . Bare in mind that I am primarily a developer using Java as my foundation and because of Java's 'run anywhere' principle, Many of the tools I use, are by default abstract from a system as they run on the Java Runtime Environment (JRE).
What VirtualBox offers a developer:
Access to an additional system along with it's own benefits.
The ability to share entities between systems.
The ability to have numerous development profiles (Open or Proprietary).
The chance to bring host and guest systems together.
The ability to run an Operating System in a different Local.
A reduction in uneccessary software.
The ability to run software geared at OS x Y or Z
Some VirtualBox terminology
The host(s) is/are the systems that run inside VirtualBox. Guest systems are systems that run inside the application as a box.
Picking the Host
Most likely you're tied down to a system as your host due to warranty. the biggest factor I feel in choosing your base system is performance. Things like PhotoShop should run on a Windows base unless you have extreme resources at hand. I peronally keep Windows as my host as it has the support for the bulk of the tools I use.
Before you start
Consider your tools. As a Java developer, Maven will go a long way into aiding a system like this. It abstracts your Java projects from most IDE's and can be a great asset if you intend different build profiles on your host or guest . Pick an IDE that is available to both systems. As a Java web developer try picking test servers that run on the JDK, like Tomcat. This buys you the ability to share a single Tomcat from your base environment to your secondary system.
Organize & Tidy the VirtualBox host
I am running Windows 7 Ultimate. I make use of tools like Intellij IDEA, Oracle JDK (proprietary), Maven and Tomcat which I place in %USERPROFILE%\servers. If and only if you have the extra hardware resources, should you keep things that will most likely end up as bloatware. Things like The GIMP, Putty & Filezilla are amongst the first things I can scrap with a modern Linux guest such as Ubuntu, which has tools that match these by default and with superior support.
Keep your developer tools & projects in one directory
By default many of my tools I use create their most vital directories in my %USERPROFILE% (home) directory. If you have control of your repositories and servers, place them in this directory. The huge benefit is that we have a single directory to back up and a single directory to share to the guest . My Windows host uses directory names that for the most part can be shared or override my home directory in Ubuntu. You can however pick each file individually and provide user defined names when using the shared folder feature of VirtualBox . I create my SVN repositories for my Unix guests in Tortoise SVN on a per guest basis and leave them in a parent directory on my host and I can simply pack up all my servers, repositories, Maven repository and Projects directory and move it with me. The rest of this example uses a Windows host and a Ubuntu guest. The script included works flawlessly on Ubuntu and OpenSuse and probably other Linuxes too.
Prepare the VirtualBox host
Download and install all the tools that you need on the Windows host or that are only available on Windows. This may include things such like: PhotoShop, Oracle JDK (if you want proprietary JDK on Windows), Safari browser, Tortoise SVN, etc.
Choose a IDE with Maven support
Choose an IDE. I use IDEA and NetBeans and Maven to keep my projects IDE neutral.
Create a Random Empty Project
Create at least one "ghost" project in your IDE. Do this to initialise the default "Projects" directory in your home directory eg. (%USERPROFILE%\IdeaProjects) or (%USERPROFILE%\NetBeansProjects).
Structuring your Development Environment
Create a "servers", "brushes", "maven", "scripts" and "SVN", directories within your home (%USERPROFILE%) directory. These directories will contain our: 1)Development servers, 2)Photoshop and Gimp brushes, 3)Maven libraries, 4)"bash" and "cmd" scripts. Notice how everything is nicely contained in one directory. This is very convenient should we wish to backup/move/share this directory as needed. We have all our Projects, scripts, downloads, brushes and version control systems in one place. Unfortunately this directory contains your %USERPROFILE%\Music and %USERPROFILE%\Pictures directories if you happen to be developing on the same profile as is being used for regular everyday life. It is advised that you don't mix your development system with your personal life, but there are two methods you can circumvent this: 1)Create a new user on your system to do the everyday stuff in and leave the admin administrator account to develop with. In this tutorial, I am using the administrator account to develop with, so my Music and Picture folders so not contain my personal stuff as an example; Or, 2) Create a %USERPROFILE%\DEV directory as a replacement and create all the directories from the above step in this directory.
Install VirtualBox
Download and install VirtualBox.
Get an Operating System for the VirtualBox guest.
Grab a Unix system via distrowatch.com. If you're new to Unix, I advise one of the better documented and known systems like:
Setup the VirtualBox Guest
Setup the guest system as per my VirtualBox Setup tutorial.
VirtualBox Guest Additions
Set up the VirtualBox Guest Additions
VirtualBox Shared Folders
This next step is dependent on Step 4. Either setup your files needed to develop with to be shared. Create either a single or multiple shared directories to enable the following directories (or their equivalents) to be shared to the guest from your (%USERPROFILE%)or home directory to the guest system. In the case of my system I share files as follows and base the rest of this tutorial on my own system (this means that you will need to adjust the script and directories that follow, to reflect your own configurations. Also take note that it is very useful to make use of servers like Tomcat and Jboss as these have start scripts for both Windows and Linux : 1)servers (contains Tomcat and Jboss), 2)SVN (subversion repository), 3)maven(full access) (contains Maven libraries), 4).M2 (contains maven settings file and maven local repository) 5) scripts (contains "bash" and "cmd" sub directories and scripts) 6) IdeaProjects (contains projects), etc. All these directories are set up to allow the guest to have full access on these directories (read, write and execute).
Use Jar Bar script for your VirtualBox Guest
Run the following script on the VirtualBox Linux guest to: 1) Install Oracle JDK on your guest, 2) Install Maven, 3) Install Netbeans on your guest, 4) Install IDEA on your guest and 5)mount the shareable entities from your host to guest .Note that it is unlikely that you need two IDEs, but I personally use Netbeans for JEE development and IDEA for my non JEE requirements at times when I am not covered by a Jetbrains license. You can simply delete what you don't need from the script or look at the linked tutorials to do this manually. You can download the /script by clicking on it or the link below it:
#!/bin/bash
#Author: Yucca Nel http://thejarbar.org
#Modify these variables as needed...
tempWork=/tmp/work
defaultStartScript=/etc/init.d/rc.local
locBin=/usr/local/bin
javaUsrLib=/usr/lib/jvm
defaultMaven=3.0.4
defaultNetBeansVer=7.1
mavenUsrLib=/usr/lib/maven
defaultIDEA=11.0.1
sudo mkdir -p $javaUsrLib
mkdir -p $tempWork
sudo mkdir -p $mavenUsrLib
mkdir -p $HOME/.m2
mkdir -p $HOME/SVN
cd $tempWork
#Update this line to reflect newer versions of JDK...
wget --no-cookies --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2Ftechnetwork%2Fjava%2Fjavase%2Fdownloads%2Fjdk-7u3-download-1501626.html;" http://download.oracle.com/otn-pub/java/jdk/7u3-b04/jdk-7u3-linux-i586.tar.gz
#Extract the download
tar -zxvf $tempWork/*
rm -rf $tempWork/*.gz
#Move it to where it can be found...
sudo mv -f $tempWork/* $javaUsrLib/
export JAVA_HOME="$javaUsrLib/jdk1.7.0_02"
sudo ln -f -s $javaUsrLib/jdk1*/bin/* /usr/bin/
rm -rf $tempWork/*
#Update this line to reflect newer versions of JDK...
if ! grep "JAVA_HOME=$javaUsrLib/jdk1.7.0_02" /etc/environment
then
echo "JAVA_HOME=$javaUsrLib/jdk1.7.0_02" | sudo tee -a /etc/environment
fi
source /etc/environment
read -p "Please [Enter] full path name of your local startup script ($defaultStartScript is the default). Please
make sure on this before providing a value by consulting documentation for your system:" locStartScript
locStartScript=${locStartScript:-$defaultStartScript}
read -p "Please [Enter] Maven Version ($defaultMaven is default):" mavenVersion
mavenVersion=${mavenVersion:-$defaultMaven}
read -p "Please [Enter] NetBeans Version ($defaultNetBeansVer is default):" netbeansVersion
netbeansVersion=${netbeansVersion:-$defaultNetBeansVer}
read -p "Please [Enter] IDEA Version ($defaultIDEA is default):" ideaVersion
ideaVersion=${ideaVersion:-$defaultIDEA}
if [ ! -f $locStartScript ]
then
echo "The file you provided could not be found. Remember to include the full path and try again. Exiting in 7 secs..."
sleep 7
exit 1
fi
wget http://mirrors.powertech.no/www.apache.org/dist//maven/binaries/apache-maven-$mavenVersion-bin.tar.gz
tar -zxvf $tempWork/*
#Move it to a more logical location
sudo mv -f $tempWork/apache-maven-$mavenVersion $mavenUsrLib/
sudo ln -f -s $mavenUsrLib/apache-maven-$mavenVersion/bin/* /usr/bin/
#If you have Maven on Windows and use VirtualBox, you can set up the maven to be a virtualbox shared folder.
#The name must match the name used below (ignore if irrelevant to you).
if [ -f /sbin/mount.vboxsf ]
then
sudo /sbin/umount $HOME/.m2
sudo /sbin/umount $mavenUsrLib
sudo /sbin/mount.vboxsf .m2 $HOME/.m2
sudo /sbin/mount.vboxsf maven $mavenUsrLib
fi
if mountpoint -q $HOME/.m2 && mountpoint -q $mavenUsrLib
then
tar -zxvf $tempWork/*
sudo mv -f $tempWork/apache-maven-$mavenVersion $mavenUsrLib/
#Add it to the start script to automate process...
if ! grep "sudo /sbin/mount.vboxsf .m2 $HOME/.m2" $locStartScript
then
echo "sudo /sbin/mount.vboxsf .m2 $HOME/.m2" | sudo tee -a $locStartScript
fi
if ! grep "sudo /sbin/mount.vboxsf maven $mavenUsrLib" $locStartScript
then
echo "sudo /sbin/mount.vboxsf maven $mavenUsrLib" | sudo tee -a $locStartScript
fi
sudo chmod +x $locStartScript
#Create a mount and unmount script file...
rm -rf $tempWork/*
echo '#!/bin/bash' > $tempWork/maven-mount.sh
echo "sudo /sbin/mount.vboxsf .m2 $HOME/.m2" >> $tempWork/maven-mount.sh
echo "sudo /sbin/mount.vboxsf maven $mavenUsrLib" >> $tempWork/maven-mount.sh
echo "echo 'mounted maven'" >> $tempWork/maven-mount.sh
echo "exit 0" >> $tempWork/maven-mount.sh
echo '#!/bin/bash' > $tempWork/maven-umount.sh
echo "sudo umount $HOME/.m2" >> $tempWork/netbeans-umount.sh
echo "sudo umount $mavenUsrLib" >> $tempWork/netbeans-umount.sh
echo "echo 'unmounted maven'" >> $tempWork/maven-mount.sh
echo 'exit 0' >> $tempWork/maven-umount.sh
#Script for mounting ALL VirtualBox shared solders....
#If there isn't one create one...
if [ ! -f $locBin/mount-all-from-host.sh ]
then
echo '#!/bin/bash' > /mount-all-from-host.sh
echo "sudo /sbin/mount.vboxsf .m2 $HOME/.m2" | sudo tee -a $tempWork/mount-all-from-host.sh
echo "sudo /sbin/mount.vboxsf maven $mavenUsrLib" | sudo tee -a $tempWork/mount-all-from-host.sh
echo "exit 0" | sudo tee -a $tempWork/mount-all-from-host.sh
#Otherwise if there is one, but no mount, add one...
elif ! grep "sudo /sbin/mount.vboxsf .m2 $HOME/.m2" $locBin/mount-all-from-host.sh
then
sudo sed -ie '$d' $locBin/mount-all-from-host.sh
echo "sudo /sbin/mount.vboxsf .m2 $HOME/.m2" | sudo tee -a $locBin/mount-all-from-host.sh
echo "exit 0" | sudo tee -a $locBin/mount-all-from-host.sh
elif ! grep "sudo /sbin/mount.vboxsf maven $mavenUsrLib" $locBin/mount-all-from-host.sh
then
sudo sed -ie '$d' $locBin/mount-all-from-host.sh
echo "sudo /sbin/mount.vboxsf maven $mavenUsrLib" | sudo tee -a $locBin/mount-all-from-host.sh
echo "exit 0" | sudo tee -a $locBin/mount-all-from-host.sh
fi
#Script for unmounting ALL VirtualBox shared folders...
#If there isn't one create one...
if [ ! -f $locBin/umount-all-from-host.sh ]
then
echo '#!/bin/bash' > $tempWork/umount-all-from-host.sh
echo "sudo umount -a -t vboxsf" | sudo tee -a $tempWork/umount-all-from-host.sh
echo "echo 'unmounted all VirtualBox shared folders'" | sudo tee -a $tempWork/umount-all-from-host.sh
echo "exit 0" | sudo tee -a $tempWork/umount-all-from-host.sh
fi
chmod +x $tempWork/*
sudo mv -f $tempWork/* $locBin/
rm -rf $tempWork/*
fi
sudo ln -f -s $mavenUsrLib/apache-maven-$mavenVersion/bin/* /usr/bin/
rm -rf $tempWork/*
wget http://dlc.sun.com.edgesuite.net/netbeans/$netbeansVersion/final/bundles/netbeans-$netbeansVersion-ml-javase-linux.sh
sh $tempWork/*sh;
#Add Netbeans launcher to your PATH. Doing so allows you to run 'netbeans' command from the terminal
#This line will need to be changed if you changed the default install location (IOW Netbeans is not in ~/)
sudo ln -f -s ~/netbeans-7.1/bin/netbeans /usr/bin/;
#If you use VirtualBox , you can share your projects between Host and guest. Name of shared
#folder must match 'NetBeansProjects'
mkdir -p $HOME/NetBeansProjects
if [ -f /sbin/mount.vboxsf ]
then
sudo /sbin/umount $HOME/NetBeansProjects
sudo /sbin/mount.vboxsf NetBeansProjects $HOME/NetBeansProjects
fi
if mountpoint -q $HOME/NetBeansProjects
then
#Add it to the start script to automate process...
if ! grep "sudo /sbin/mount.vboxsf NetBeansProjects $HOME/NetBeansProjects" $locStartScript
then
echo "sudo /sbin/mount.vboxsf NetBeansProjects $HOME/NetBeansProjects" | sudo tee -a $locStartScript
fi
chmod +x $locStartScript
#Create a mount and unmount script file...
rm -rf $tempWork/*
echo '#!/bin/bash' > $tempWork/netbeans-mount.sh
echo "sudo /sbin/mount.vboxsf NetBeansProjects $HOME/NetBeansProjects" >> $tempWork/netbeans-mount.sh
echo "echo 'mounted NetBeansProjects'" >> $tempWork/netbeans-mount.sh
echo "exit 0" >> $tempWork/netbeans-mount.sh
echo '#!/bin/bash' > /netbeans-umount.sh
echo "sudo umount $HOME/NetBeansProjects" >> $tempWork/netbeans-umount.sh
echo "echo 'unmounted NetBeansProjects'" >> $tempWork/netbeans-mount.sh
echo 'exit 0' >> $tempWork/netbeans-umount.sh
#Script for mounting ALL VirtualBox shared solders....
#If there isn't one create one...
if [ ! -f $locBin/mount-all-from-host.sh ]
then
echo '#!/bin/bash' > $tempWork/mount-all-from-host.sh
echo "sudo /sbin/mount.vboxsf NetBeansProjects $HOME/NetBeansProjects" | sudo tee -a $tempWork/mount-all-from-host.sh
echo "exit 0" | sudo tee -a $tempWork/mount-all-from-host.sh
#Otherwise if there is one, but no mount, add one...
elif ! grep "sudo /sbin/mount.vboxsf NetBeansProjects" $locBin/mount-all-from-host.sh
then
sudo sed -ie '$d' $locBin/mount-all-from-host.sh
echo "sudo /sbin/mount.vboxsf NetBeansProjects $HOME/NetBeansProjects" | sudo tee -a $locBin/mount-all-from-host.sh
echo "exit 0" | sudo tee -a $locBin/mount-all-from-host.sh
fi
#Script for unmounting ALL VirtualBox shared folders...
#If there isn't one create one...
if [ ! -f $locBin/umount-all-from-host.sh ]
then
echo '#!/bin/bash' > $tempWork/umount-all-from-host.sh
echo "sudo umount -a -t vboxsf" | sudo tee -a $tempWork/umount-all-from-host.sh
echo "echo 'unmounted all VirtualBox shared folders'" | sudo tee -a $tempWork/umount-all-from-host.sh
echo "exit 0" | sudo tee -a $tempWork/umount-all-from-host.sh
fi
chmod +x $tempWork/*
sudo mv -f $tempWork/* $locBin/
rm -rf$tempWork/*
fi
wget http://download-ln.jetbrains.com/idea/ideaIC-$ideaVersion.tar.gz;
tar -zxvf $tempWork/*;
#Move it to a better location...
mv -f $tempWork/idea-IC-* $HOME/;
rm -rf $tempWork/*
sudo ln -f -s $HOME/idea-*/bin/* /usr/bin/;
#If you use VirtualBox , you can share your projects between Host and guest. Name of shared
#folder must match 'IdeaProjects'
mkdir -p $HOME/IdeaProjects
if [ -f /sbin/mount.vboxsf ]
then
sudo /sbin/umount $HOME/IdeaProjects
sudo /sbin/mount.vboxsf IdeaProjects $HOME/IdeaProjects
fi
if mountpoint -q $HOME/IdeaProjects
then
#Add it to the start script to automate process...
if ! grep "sudo /sbin/mount.vboxsf IdeaProjects $HOME/IdeaProjects" $locStartScript
then
echo "sudo /sbin/mount.vboxsf IdeaProjects $HOME/IdeaProjects" | sudo tee -a $locStartScript
fi
chmod +x $locStartScript
#Create a mount and unmount script file...
rm -rf $tempWork/*
echo '#!/bin/bash' > $tempWork/idea-mount.sh
echo "sudo /sbin/mount.vboxsf IdeaProjects $HOME/IdeaProjects" >> /idea-mount.sh
echo "echo 'mounted IdeaProjects'" >> $tempWork/idea-mount.sh
echo "exit 0" >> $tempWork/idea-mount.sh
echo '#!/bin/bash' > $tempWork/idea-umount.sh
echo "sudo umount $HOME/IdeaProjects" >> $tempWork/idea-umount.sh
echo "echo 'unmounted IdeaProjects'" >> $tempWork/idea-mount.sh
echo 'exit 0' >> $tempWork/idea-umount.sh
#Script for mounting ALL VirtualBox shared solders....
#If there isn't one create one...
if [ ! -f $locBin/mount-all-from-host.sh ]
then
echo '#!/bin/bash' > $tempWork/mount-all-from-host.sh
echo "sudo /sbin/mount.vboxsf IdeaProjects $HOME/IdeaProjects" | sudo tee -a $tempWork/mount-all-from-host.sh
echo "exit 0" | sudo tee -a $tempWork/mount-all-from-host.sh
#Otherwise if there is one, but no mount, add one...
elif ! grep "sudo /sbin/mount.vboxsf IdeaProjects" $locBin/mount-all-from-host.sh
then
sudo sed -ie '$d' $locBin/mount-all-from-host.sh
echo "sudo /sbin/mount.vboxsf IdeaProjects $HOME/IdeaProjects" | sudo tee -a $locBin/mount-all-from-host.sh
echo "exit 0" | sudo tee -a $locBin/mount-all-from-host.sh
fi
#Script for unmounting ALL VirtualBox shared folders...
#If there isn't one create one...
if [ ! -f $locBin/umount-all-from-host.sh ]
then
echo '#!/bin/bash' > $tempWork/umount-all-from-host.sh
echo "sudo umount -a -t vboxsf" | sudo tee -a $tempWork/umount-all-from-host.sh
echo "echo 'unmounted all VirtualBox shared folders'" | sudo tee -a $tempWork/umount-all-from-host.sh
echo "exit 0" | sudo tee -a $tempWork/umount-all-from-host.sh
fi
chmod +x $tempWork/*
sudo mv -f $tempWork/* $locBin/
rm -rf $tempWork/*
fi
if [ -f /sbin/mount.vboxsf ]
then
sudo /sbin/umount $HOME/SVN
sudo /sbin/mount.vboxsf SVN $HOME/SVN
fi
if mountpoint -q $HOME/SVN
then
#Add it to the start script to automate process...
if ! grep "sudo /sbin/mount.vboxsf SVN $HOME/SVN" $locStartScript
then
echo "sudo /sbin/mount.vboxsf SVN $HOME/SVN" | sudo tee -a $locStartScript
fi
chmod +x $locStartScript
#Create a mount and unmount script file...
rm -rf $tempWork/*
echo '#!/bin/bash' > $tempWork/svn-mount.sh
echo "sudo /sbin/mount.vboxsf SVN $HOME/SVN" >> /svn-mount.sh
echo "echo 'mounted SVN'" >> $tempWork/svn-mount.sh
echo "exit 0" >> $tempWork/svn-mount.sh
echo '#!/bin/bash' > $tempWork/svn-umount.sh
echo "sudo umount $HOME/SVN" >> $tempWork/svn-umount.sh
echo "echo 'unmounted SVN'" >> $tempWork/svn-mount.sh
echo 'exit 0' >> $tempWork/svn-umount.sh
#Script for mounting ALL VirtualBox shared solders....
#If there isn't one create one...
if [ ! -f $locBin/mount-all-from-host.sh ]
then
echo '#!/bin/bash' > $tempWork/mount-all-from-host.sh
echo "sudo /sbin/mount.vboxsf SVN $HOME/SVN" | sudo tee -a $tempWork/mount-all-from-host.sh
echo "exit 0" | sudo tee -a $tempWork/mount-all-from-host.sh
#Otherwise if there is one, but no mount, add one...
elif ! grep "sudo /sbin/mount.vboxsf SVN" $locBin/mount-all-from-host.sh
then
sudo sed -ie '$d' $locBin/mount-all-from-host.sh
echo "sudo /sbin/mount.vboxsf SVN $HOME/SVN" | sudo tee -a $locBin/mount-all-from-host.sh
echo "exit 0" | sudo tee -a $locBin/mount-all-from-host.sh
fi
#Script for unmounting ALL VirtualBox shared folders...
#If there isn't one create one...
if [ ! -f $locBin/umount-all-from-host.sh ]
then
echo '#!/bin/bash' > $tempWork/umount-all-from-host.sh
echo "sudo umount -a -t vboxsf" | sudo tee -a $tempWork/umount-all-from-host.sh
echo "echo 'unmounted all VirtualBox shared folders'" | sudo tee -a $tempWork/umount-all-from-host.sh
echo "exit 0" | sudo tee -a $tempWork/umount-all-from-host.sh
fi
chmod +x $tempWork/*
sudo mv -f $tempWork/* $locBin/
rm -rf $tempWork/*
fi
sudo rm -rf $tempWork
sudo /sbin/reboot
exit 0
jar-bar-full-java-unix-virtualbox-install.sh
That's it!
Using most of this tutorial, or linked tutorials, you should be able to not only run a single script to install most of what you need to develop with in Java on Windows & Linux, but also way to pack up your development files from the Windows home directory (NetBeansProjects, IdeaProjects, servers, maven, M2 and SVN) to universal Unix home (~/) by using this tutorial as a guide. I'll add to the tutorial as I find more ways to make the Unix installation/sharing process less verbose. So check back later. You can view everything I am doing here in the YouTube playlist below and download theJar Bar VirtualBox Bash Scripts which will aid this tutorial.