Posts Tagged ‘Ubuntu’
Setting Up Tomcat on Ubuntu
Prerequisites
Tomcat 6.0 requires JRE 5.0. Read the RELEASE-NOTES and the RUNNING.txt file in the distribution for more details.
Installation
1. Download binary core from apache site.
wget http://apache.tradebit.com/pub/tomcat/tomcat-6/v6.0.18/bin/apache-tomcat-6.0.18.tar.gz
2. Extract the tarball under /usr/local/
Note: Locally installed software must be placed within /usr/local rather than /usr unless it is being installed to replace or upgrade software in /usr.
tar xvzf apache-tomcat-6.0.18.tar.gz
3. Tomcat requires that you set the JAVA_HOME variable. If you haven’t done it yet, you can do so by adding the following line in your .bashrc
export JAVA_HOME=/usr/lib/jvm/java-6-sun
4. We can test if Tomcat is running by running the startup script then opening a browser and accessing http://localhost:8080
sudo /usr/local/tomcat/bin/startup.sh
Details on troubleshooting can be found on RUNNING.txt
Setting Up MySQL/JDBC Driver on Ubuntu
Assuming that you already have MySQL installed, the next step is to install the connector driver. You can do this easily on the CLI by using the following command:
sudo apt-get install libmysql-java
The next step is to make sure that the classpath is set. You can have this set automatically by adding this command to you bashrc file.
export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar
If you want to set this for all users, you should modify the /etc/environment instead.
For those using Eclipse, you can also do this by going through the following steps:
- Select Project Properties > Java Build Path
- Select Libries tab
- Click Add External Jars
- Choose the jar file, in this case mysql-connector-java.java
Once you’re done, you can test the connection using the following snippet:
import java.sql.Connection;
import java.sql.DriverManager;
class JDBCTest {
private static final String url = "jdbc:mysql://localhost";
private static final String user = "username";
private static final String password = "password";
public static void main(String args[]) {
try {
Connection con = DriverManager.getConnection(url, user, password);
System.out.println("Success");
} catch (Exception e) {
e.printStackTrace();
}
}
}