Currently viewing the category: "How To"

Ubuntu 12.04 is my primary OS, however, there doesn’t appear to be many guides on installing the Sun/Oracle JDK on Ubuntu.

In case someone else finds it useful, here’s what I did on my 64 bit Ubuntu 12.04 install:

1. Download the Java SE Development Kit 6 Update 38 for Linux x64 from http://www.oracle.com/technetwork/java/javase/downloads/jdk6u38-downloads-1877406.html (The filename should be jdk-6u38-linux-x64.bin and NOT jdk-6u38-linux-x64.rpm.bin)

2. Make the file executable: chmod +x jdk-6u38-linux-x64.bin

3. Extract the file ./jdk-6u32-linux-x64.bin

4. Create a directory called jvm in /usr/lib: sudo mkdir /usr/lib/jvm

5. Move the extracted JDK directory into /usr/lib/jvm: sudo mv jdk1.6.0_38 /usr/lib/jvm/jdk1.6.0_38

6. Make it the default JDK in the system:

sudo update-alternatives –install /usr/bin/javac javac /usr/lib/jvm/jdk1.6.0_38/bin/javac 1
sudo update-alternatives –install /usr/bin/java java /usr/lib/jvm/jdk1.6.0_38/bin/java 1
sudo update-alternatives –install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.6.0_38/bin/javaws 1

sudo update-alternatives –config javac
sudo update-alternatives –config javaws
sudo update-alternatives –config java

 

If you want, you can add the JAVA_HOME environment variable to point to your new JDK installation as:

export JAVA_HOME=/usr/lib/jvm/jdk1.6.0_38/

and now you can put the JDK binaries like java, javac etc. as:

export PATH=$PATH:$JAVA_HOME/bin

Sticking those two statements in your .bashrc would ensure they always stay in your path across.

 

Edit: You can also enable the Java plugin for Chrome and Firefox as:

sudo ln -s /usr/lib/jvm/jdk1.6.0_38/jre/lib/amd64/libnpjp2.so /usr/lib/mozilla/plugins

Tagged with:
 

Some of the developers I have worked with on smaller projects have never used Git (shocking, I know), and I always end up having to give them a very basic tutorial just to help them get up to speed.

Since I’ve already done it more than a couple of times, I thought I should write a post about the absolute minimum every developer should be aware of when starting to use git to collaborate with other developer(s) on a software development project.

Note: I prefer using git from the command line. Most linux distributions should come with git pre-installed. If you don’t have it or you’re on a mac, you can download it from the official git downloads page. If you’re on windows, you can use mysysGit, which comes with bash. (Secret: if you’ve always missed bash on Windows, this can replace cmd.exe for most command-line work)

First, you should know how to clone a repository. Most of the times, if the repository is hosted on a hosting service like GitHub or Bitbucket, this consists of copying and pasting one git command from the website, like in the screenshot I took from bitbucket below:

Now, you should have the files for your project in the directory you cloned your repository.

Then, once you’ve worked on your project (i.e. added/modified/removed files), you can do:

git add -A

which basically tells git “hey, I’ve modified stuff and I want you to know about them”.

Then you can commit your changes as:

git commit -m "message"

where ”message” is a little note describing what you’ve changed.

After this step, you want to pull for any new changes that may have been made. This is important, and is often forgotten by beginners. Between the last time you cloned/pulled from a remote repository, there may have been changed committed to the files by another member of your team, and you want to get those changes and deal with any conflicts at this step. I thought this tutorial had a great discussion on handling merge conflicts in Git.

After you’ve taken care of any merge conflicts you may have had, it’s push time. This is as easy as

git push origin master

which tells git now that you know what I changed (using git add -A) and how I described what I did (using git commit -m “message”), I want you to take my changes and send it to the remote server where everything is hosted.

This, in my opinion, is the bare minimum that every developer should know about git. While what I’ve described is in no way comprehensive, it should be enough to get started on a project that’s using Git for source control.

Once you feel comfortable with all this, I recommend you check out some tutorials available online. If you like videos, O’Reilly has a great one hour video tutorial on Git. If you like traditional books, the Git Community Book is probably the most authoritative, comprehensive and up-to-date guide, and it’s available for free (I had their chapter on branching and merging bookmarked for many months when I was first learning this stuff).

I also have a printed copy of this Git Cheat Sheet on my desk, and I find myself frequently looking up stuff at Everyday GIT with 20 Commands Or So. I also found the Git Workflow essay by Benjamin Sandofsky a good introduction to basic workflow.

Update: The Git Basics Tutorial by CodeForAmerica/Skillshares is really good for beginners as well.

Tagged with:
 

I’ve been trying to switch to Vim from TextMate as my default text editor, and I’m constantly stumbling and learning in the process.

One quirk I dealt with recently was spaces. Well, yeah, spaces.

I liked using Monaco as my default font on TextMate and Xcode when I’m working on Python scripts or on iOS apps, mainly because characters are distinct, and it is difficult to confuse 0 (figure zero) and O (uppercase O), or 1 (figure one) | (Vertical bar) I (uppercase i) and l (lowercase l) – which are all characters that occur a lot in programming. So I wanted to set it up as my default font on GVim on Ubuntu. I got a copy of it and installed it just fine, and set it as my font on GVim using the menu.

To make it my default font, I went ahead and opened my .vimrc file, and when I looked it up on GVim (using set guifont?), I saw this:

guifont = Monaco 11

:set guifont?

Then I added the same line to my .vimrc as

MonacoVimrcNoSpace

Incorrect vimrc settings

and it wouldn’t work. Whenever I started GVim, it would complain that it didn’t understand the .vimrc file.

After searching online and looking up a lot of forum posts, I learned that Vim does not recognize spaces in arguments. So I actually had to add a backslash (\) after the first word if I wanted vim to treat the next word as part of the same argument.

Monaco\ 11

Correct .vimrc settings

And it worked!

Now I’ve been using it in more places where it’s appropriate. For example, if I have a long piece of text and I wanted to replace apples with yellow oranges, I could just do

:%s/apples/yellow\ oranges/g

and voila, every occurrence  of apples will be replaced with yellow oranges.

Maybe this will help another struggling Vim user someday!

Tagged with:
 

I work from home for a company where we write enterprise Java web applications using Spring, Hibernate for Tomcat and use Maven. A couple of months ago, when I started working from home, my manager agreed to buy me a copy of IntelliJ IDEA so I could be more productive than when I was when I was using Eclipse (which is an IDE I love and still use for other projects, but is also a memory hog).

While I have been using IntelliJ for sometime now, I haven’t had a lot of success in configuring Tomcat to run through the IDE. I followed the steps on the official help page, but it didn’t seem to work with my Maven project. So I added a configuration descriptor to Tomcat to load the target directory of my Maven project each time it loads and when I want to run my code, I produce an exploded war through IntelliJ and then run Tomcat externally.

When I had to debug my code, I would print out debugging statements which would help me narrow down the places in my code where the bug was, and then I would just inspect it manually. Not really the best way to debug code, but I really didn’t have the time to integrate Tomcat into IntelliJ.

Last night, I had a NullPointerException in a Java Spring project I’ve been working on for a couple of months now, and this approach didn’t work, and I set out to integrate Tomcat with IntelliJ once and for all.

When I had first tried to integrate Tomcat with IntelliJ, I had followed the steps on the Jetbrains Wiki and that worked just how it was described, but when I tried to run or debug my Maven project through IntelliJ, it just would not work.

After a lot of Googling and Binging *wink*, I had found no single source that explained how this should be done, and I spent a major part of last night just playing with different combinations of the different settings and was finally able to set it up correctly (I hope). Now, I am able to run and debug Maven projects directly through Tomcat and trust me, life is better.

I’m not sure if there’s other people who’ve been through the same experience as I have, so I’ve decided to put together this guide. Hope someone who is in the same position as I was yesterday will get something out of it.

1. Add your Tomcat configuration to IntelliJ through Settings -> Application Servers -> Add. Both Tomcat Home and Tomcat Base Directory should be set to your tomcat installation directory (e.g. C:\Tomcat6.0.33)

Adding Tomcat 6 to IntelliJ Settings

Adding Tomcat 6 to IntelliJ Settings

2. Add a Run/Debug Configuration to your IntelliJ project.

Edit Run/Debug Configuration

Edit Run/Debug Configuration

3. Click the + and add a new Tomcat local default configuration. Give it a name and you can have it start your browser automatically and go to a startup page.

New Tomcat Configuration

New Tomcat Configuration

4. On the Before Launch frame, check Make.

Before Launch Make

Check Make

5. Click the ellipses (….) button next to Build Artifact and check exploded war.

Exploded war Build

Exploded war Build

6. Similarly for Maven, click the ellipses (…) button next to Run Maven Goal and select package.

Maven Package

Maven Package

7. On the Deployment tab, in the Deploy at Server Startup section, add a new Exploded war Artifact.

Deploy at Server Startup

Deploy at Server Startup

(Steps below show how to configure the IntelliJ debugger to connect to Tomcat)

8. On the Startup/Connection tab, Select Debug from the left pane and set the port to 8000.

Debug Port

Set Debug Port

9. Create two new environment variables (Directions here for *nix and Windows)

JPDA_ADDRESS=8000
JPDA_TRANSPORT=dt_socket

And you’re set. Now when you click Run or Build, you should see maven packaging the project and work it’s ‘magic’, and then Tomcat should run with the new app!

Maven doing its magic

Maven doing its magic

While now it seems simple, this was not at all intuitive when I was first tried to set all this up. But now that I have got this set up, my workflow is a lot improved and I can go back to focusing on the code!

Alternatively, if you wanted to run your project outside of IntelliJ, you can put a configuration descriptor into your tomcat/conf/Catalina/localhost directory to automatically look in a directory and deploy it every time it starts. For my QEF project, I have a file qef.xml that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="/programming/appQef/target/qef">
    <Manager className="org.apache.catalina.session.PersistentManager" maxInactiveInterval="600" saveOnRestart="false"/>
    <ResourceLink name="jdbc/qef" global="jdbc/qef" type="javax.sql.DataSource" />
    <ResourceLink global="config/machine" name="config/machine" type="java.lang.String"/>
    <ResourceLink global="config/envType" name="config/envType" type="java.lang.String"/>
    <ResourceLink global="config/envLocation" name="config/envLocation" type="java.lang.String"/>
</Context>

Now whenever I want to execute my program, I build an exploded war through IntelliJ and start Tomcat, which looks for the exploded war in the given directory and automatically deploys it.

Thanks to this and this, I was able to figure it all out. Thanks Matt and Piotr!

Hope this will help somebody out as well. :)

Tagged with: