Linux, Tomcat and Netbeans

I recently decided that it would be a good idea to add JSP and Servlet programming to my repertoire.  Well, you never know…

So I bought a copy of Murach’s Java Servlets and JSP (2nd Edition).  I’m about half way through it, and it appears to be fine book; but it’s very Windows-centric.  That’s understandable, I suppose, because I imagine that most readers of the book will be using Windows.  However, I decided I’d have a go at following the book on Linux, rather than using any of the Windows installations I have kicking around in virtual machines for the sole purpose of running IE to check out the Web pages I develop.  It turned out not to be as easy as I thought.

The difficulties center around creating a development environment using Tomcat, MySQL and Netbeans.  Getting all these talking to each other properly under Linux proved to be something of a challenge.

As you will no doubt be aware, if you’re used to Linux, software installation under Linux is a very different procedure from that used by Windows.  Under Windows, you search for a downloadable installation file, on a CD or on the Web somewhere, run it, answer some configuration questions and the application is installed.  Under Linux, you are much more likely to search your package manager for the application you want and install it from there.  I hadn’t had to do a Windows-style installation on Linux for many a year, until now.

The Linux method is usually far superior.  The package manager checks for dependencies and installs them automatically for you, whereas under Windows, if there are any prerequisites the first you know about them, probably, is when the Windows installation file informs you that it can’t go on until you’ve downloaded and installed something else.  That’s what happens, for example, if you download the latest version of the MySQL Workbench for Windows and try to install it before you’ve installed Microsoft’s C++ runtime environment and version 4 of the .NET Framework, assuming you don’t have them installed already.

However, when it comes to Linux, Tomcat and Netbeans things are a bit different.

The first problem you’ll encounter if you’re running an Ubuntu-based distribution, is that Netbeans is not in the latest repositories.  So for Netbeans, you have two choices: specify an earlier Ubuntu repository and install Netbeans from there, or install it from the installation file downloaded from the Netbeans site.  If you do the former, you may have to then remove the repository from the list to avoid conflicts, and if you do that I suppose there’s a small risk that you won’t get some important updates; although Netbeans does update itself, if you let it.  You’ll also have an older version of Netbeans, of course.

I run Linux Mint on my Desktop and Ubuntu 11.10 on the laptop, so in the end, I decided to install Netbeans from the download site.  I installed it to my home directory in order to keep it private.  I’m not a fan of installing stuff to /usr/share or /usr/local or whatever: you have to do it as root (via sudo) which means you’ll more than likely run into permissions problems down the line.  Unless you have a whole crew of people accessing your machine and needing to run Netbeans I can’t think of a good reason not to install it locally.

I checked with Fedora, too.  They do have Netbeans in their repository, but it’s an old version.  Still, I think I’d go with the repository version if I was running Fedora as my main distribution.

Anyway, one way or another, Netbeans is installed and it wasn’t too hard, was it?  Unfortunately, it’s only the start of the problems.  Next you need to install Tomcat.

Now, Tomcat is in the repositories of all the distributions that I know of, and this includes the repositories of Ubuntu-based distributions.  So you’d think you could just install it from there and everything would be fine.  That’s what I thought.  No such luck.

On Ubuntu-based distributions at least, the way that Tomcat’s files are installed spread all over the file system in /var/lib and /usr/share does not play at all well with Netbeans.  When you open Netbeans and try to add the Tomcat server there is just nothing you can do to get Netbeans to recognise your instance of Tomcat.  There are various solutions posted on the Web, but none of them worked for me.  You just have to install Tomcat from the Tomcat download site (Note you may have to update that URL to the latest Tomcat version.  Tomcat version. 6.0.35 was the most recent when I downloaded it, so that’s what I’ve linked to).

I’m not sure what the situation is with other distributions in respect to getting Netbeans to recognise the Tomcat server.  I suspect they’re similar, but I haven’t checked.

This time, for the Tomcat installation, I did decide to install to /usr/local.  This may have been a mistake, since I have had a few permissions problems on logs and things: but nothing that’s stopped anything from working.  If I did it again, I’d install it locally, though.

I imagine I don’t have to say that you need to uninstall the repository version of Tomcat before you install another one, unless you want to have two versions of Tomcat running using different ports; which seems a bit excessive to me and also runs the risk of having the two different installations overwriting each others’ files if you’re not careful.  I did a “complete uninstall” of Tomcat from the package manager, which also gets rid of the configuration files.

Once you have Tomcat installed in the proper way, Netbeans will finally be satisfied when you configure it for use.  Hooray!

The next problem is MySQL.  And the good news here is that you can, and probably should, install MySQL from your distribution’s repository.  You should also install the various GUI tools for MySQL whilst you’re at it.

Once that’s done, and fairly obviously, you need to set up your database in MySQL if you haven’t done so already, creating the database you’re going to use and the tables and any relationships between them; and adding any initial data you’ll need.  You can’t connect to a database that doesn’t exist, after all.  If you’re following the Murach book and have downloaded its code, you can just run the SQL commands you’ll find in the file setup_db.sql.

Assuming you’ve downloaded the book’s files to ~/murach, you’ll just need to do the following in a terminal window:

~ $ cd ~/murach/db
~/murach/db $ mysql -u root -p < setup_db.sql

Enter the password for the root user of your MySQL installation, and the commands will be executed almost instantaneously.  You’ll probably want to just check your MySQL installation, though, to ensure that all the databases and tables have been created, and that the tables contain the initial data.  You can use the MySQL command line to do that, or use whatever GUI tools you have installed.  I use MySQL Query Browser.

Once I’d got that done, I found that most of my remaining problems with the database turned out to be wetware problems.  For one thing, I’d misunderstood, I think, the structure of the connection string used for Connector/J.  In case anyone else is similarly mentally challenged, this is the structure you need to use:

jdbc:mysql://<servername>:<port>/<dbname>[?autoReconnect=true|false]

Replace <servername> with the name of your server, usually localhost
Replace <port> with the port number Tomcat is using, usually 8080
Replace <dbname> with the name of the database you want to connect to

Optionally end the connection string with:

?autoReconnect=true

or:

?autoReconnect=false.

You need the connection string when you configure the connection for your application.  The connection is given in the application’s context.xml file.  When you open a project with such a connection specified, Netbeans will ask you to resolve it, assuming you’re not using a connection that Netbeans already knows about.  You just select to resolve the database connection from the context menu you get when you right-click on the project; and then you add a new connection.  Which brings us to the next problem…

Actually, it’s not a problem, but it looks like one.  When you resolve the database connection and close the dialog, Netbeans (up to and including 7.0.1, anyway) will keep on telling you that the connection is not resolved.  It is, though.  Just ignore the error message and the warning icon on the project name in the Project window, compile and run your project.  All should be well.

Phew!  That’s quite a lot, but I hope it helps someone.  I’m not the best server administrator in the world, it’s true, but finding out all the stuff above took me three days solid; and I’m left with the feeling that it really shouldn’t be that hard.  Maybe the Netbeans people and some representatives of the major distributions should get together.  If not, people are just going to use Windows, unfortunately.

I installed everything on Windows in a couple of hours.  It wasn’t entirely problem free, but the problems were not hard to solve.  No googling, for example.  But having said that, the Murach book gives instructions for Windows, so it was always going to be easier, in a way.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>