Creating an Executable JAR in Intellij IDEA

I’ve been using IDEA for a while now and have been very happy with it. While Eclipse is a great free IDE, IDEA does bring a lot more to the table and is worth the price IMHO. That said, I’ve always felt that creating a JAR in IDEA is not very intuitive. Well, everything is complicated until you figure it out so today I decided to take a few minutes and un-complicate it. Without further ado, let’s get started. The following steps were performed on IDEA 2016.1

Step 1: Select File –> Project Structure

Step1

Step 2: In the Project Structure dialog box, select Artifacts. Then click the + symbol to add a new artifact and select Empty from the popup menu. You could also go with From module with dependencies and have IDEA configure it all for you. I think it is also good to the manual process so you are not dependent on any undercover magic.

Step2

Step 3: In the settings for the new artifact, select Other from the Type dropdown box. This is important so don’t forget to do that.

Step3

Step 4: Then update all other settings like Name, Output directory, Build on make etc. In the Output Layout tab, under the Available Elements panel, select any 3rd party JAR files that you’d like to include with your JAR file and choose Put into Output Root in the context which appears when you right click on your selection. These JAR files will now be present in the same directory as the compiled output of your code. This is slightly different from how Eclipse does it where all JARs are extracted into a single runnable JAR. Both methods work but I like this better as it keeps your code separate from 3rd party JAR files.

Step4

Step 5: Once you’ve included all the 3rd party JARs, it is time to include your code. To do that, click the Create Archive button in the Output Layout tab.

Step5

Step 6: In the popup dialog that appears, enter the desired name of your JAR.

Step6

Step 7: Now right click on the compiled output of your code under the Available Elements panel and choose Pack into /your-name.jar option in the context menu that appears. Here your-name would the value that you entered in the previous step.

Step7

Step 8: If you did everything correctly, your final settings dialog should look something like this with both the 3rd party JARs and your JAR configured correctly.

Step8

Step 9: Now it is time to create the MANIFEST.MF file that will link all these 3rd party JARs to your JAR and create an executable JAR as the end result. Here you can use an existing MANIFEST.MF file from the project if it already has one or create a new one. In my case, I went with creating a new one.

Step9

Step 10: The 3 main steps here are to: select an output folder for the MANIFEST.MF file, select the main class and enter the class path. I always go with the src folder as the output folder. As for the class path, I just enter the name of each JAR file that I’ve included in the project. So your end result of this step should look like this:

Step10

Step 11: At this point you are all set. Go ahead and generate the artifact. You will now see IDEA create the artifcat in the ../out/artifacts/.. folder. When you execute the JAR file from the command line, you can see that everything works perfectly.

Step11

Cheers and happy coding!

Allowing Users to List Databases

I’ve been playing around with mongoDB’s v3. Before you had to create users inside every single database for restricting access. I can see various scenarios where such a model would have become a user management nightmare.

In the new version, I like how we can have all users in the admin DB and can manage access and privileges right from within there. A single point of management is always easy to keep straight.

That said, one extra step that I’d like to take for each of these users I create, is to allow them access to list the available databases in the server.

Why you ask? The reason is in some GUI based clients, you are unable to switch to a DB after authenticating using the admin DB. So in such cases, all you’ll have to do is create a role that just does that and assign it to the users you create.

Is this still secure? Yes, apart from listing the DBs, the user is unable to do much else if there are no other rights given.

To set this role up, first switch over to the admin DB by typing use admin; in the shell. Then run the following command.

Using HttpClient (System.Net.Http) in .Net 4.0

To use HttpClient from System.Net.Http in a project targeting .Net 4.0, run the following command in the NuGet Package Manager Console:

Visit the official NuGet pages for more info about the package.

Either another instance of LibreOffice is accessing your personal settings or your personal settings are locked.

With error messages like that, you can forget getting my mom to use LibreOffice :). That said, I’m not my mom and LibreOffice is the best free alternative to MS Office IMHO so I should not really complain!

This all started when I tried to open a 200+ MB CSV file on my Mac. I usually right click and select Sublime Text but I was busy and ended up double clicking it triggering LibreOffice Calc to start opening it up. LibreOffice froze my machine. I killed LibreOffice, proceeded to open the file in Sublime Text, did my due and forgot about it. Today when I’m started LibreOffice, I got the following error message:

Either another instance of LibreOffice is accessing your personal settings or your personal settings are locked.
Simultaneous access can lead to inconsistencies in your personal settings. Before continuing, you should make sure user 'karthicr' closes LibreOffice on host 'LA14001.local'.

Do you really want to continue?

I sure as hell know LibreOffice is not running but you can verify the same via the Activity Monitor or via the Terminal using the following command:

ps -A | grep soff

If LibreOffice is running, you should see something along the lines of :

695 ?? 0:03.16 /Applications/LibreOffice.app/Contents/MacOS/soffice
710 ttys000 0:00.00 grep soff

If LibreOffice is not running, you should not see the first line, only, the second line. The second line with grep represents the process you were running to search for a running instance of LibreOffice.

If LibreOffice is in fact running, kill that process right from within the Activity Monitor or do it from the Terminal via the following command:

kill -9 695

where 695 is the process id of the instance you get from the earlier command.

If LibreOffice was not running, as in my case, it usually means that a dirty shutdown left behind a .lock file which is usually created by LibreOffice and removed on a clean shutdown. In this case, all we need to do is locate that .lock file and remove it.

Common places where you will find the .lock file include:

On a Mac:

~/Library/Application Support/LibreOffice/4

On a Linux Machine:

~/.config/libreoffice/4

The 4 in indicates the version of LibreOffice and will change depending on the version of LibreOffice you are running. Delete that .lock file and run LibreOffice again, you will see no more scary errors.

Cheers!

Django REST Framework: ‘module’ object is not callable

This post is more of a note to myself, but, I’m throwing it up here as a benefit to myself and my brethren who end up making the same mistake.

If you ever encounter an error that reads: 'module' object is not callable, while using the reverse() function, you are using the wrong import!

Wrong:

Correct:

Serving Django Admin over HTTPS behind a Nginx Proxy

I was deploying my first Django application today on a production server. Nginx is the proxy server and gunicorn is the WSGI web server. Supervisor is being used to ensure the gunicorn workers are always re-spawned if they die. The firewall is setup to only allow HTTPS connections. After a lot of tweaking the settings, I was finally able to see my page over the public internet.

An interesting thing was happening when I was trying to get to the admin. I would use access the admin over HTTPS and login. However I was being redirected to HTTP after the login and I would never see a page as the firewall would block the request. But when I reload the admin page using HTTPS I can see that I was logged in. So basically I needed to figure out what to do so Django admin would be served over HTTPS after login.

After some snooping around on the net, I find the answer on stackoverflow. So without further adieu:

Force Timezone in Javascript Date

So I was working on a project for which I needed to force the Eastern timezone while displaying dates. Though not relevant to the post, I thought I need to share a few words about the method I chose to accomplish the task. I discovered prototype object (not the framework) today. (You are welcome to skip this section and proceed to the implementation) Here is an excerpt from the excellent Mozilla Development Network pages:

All objects in JavaScript are descended from Object; all objects inherit methods and properties from Object.prototype, although they may be overridden (except an Object with a null prototype, i.e. Object.create(null)). For example, other constructors’ prototypes override the constructor property and provide their own toString methods. Changes to the Object prototype object are propagated to all objects unless the properties and methods subject to those changes are overridden further along the prototype chain.

The items we are interested in are:

  • they may be overriden
  • changes are propogated to all objects

This is applicable to both user defined types and built-in types in javascript. This is awesome! I decided to use the Object.prototype object to extend the built-in Date type to add some sugar to my code while keeping it readable.

Notice the extension of the built-in Date by the addition of getStandardTimezoneOffset() and isDst() methods. Also notice how getEastern(datetime) is not an extension but just a straight up javascript function since it is a very specific use case and it makes no sense to make this function available to all instances of Date.

The code is pretty self explanatory but if you have questions drop me a note and I’ll do my best to answer them. Accomplishing this task and this post would not be complete without posting references to the articles that guided me through this process.

  1. How to ignore user’s time zone and force Date() use specific time zone
  2. Check if Daylight Saving Time is in effect, and if it is for how many hours
  3. Daylight Saving Time
  4. Time Zone Offset Calculation

Shell Script Converting WAV files to MP3 files using SoX

Here is the script to convert a WAV file to a MP3 file quickly. This is my first foray into the world of shell scripts and I quite enjoyed it.

Cheers and stay classy!

Squirrel SQL, jTDS and Windows Authentication (SSO) for Microsoft SQL Server

If you are like me and you hate spinning up a virtual machine every time you need to run a few simple queries in MS SQL Server, you have reached the right place. Read on to find out how to setup up SQuirreL (a universal SQL client) to connect to MS SQL Server via Windows Authentication (SSO). The great thing is this process will work on a Mac and Windows machine as well if you’d like to keep things consistent and use a single tool across platforms to accomplish tasks.

The key point to note here is, unlike other posts on the web that have you add the dll file in some PATH or similar environment variable in the system, I show you how to add it within SQuirreL SQL. I like this approach as it keeps things clean and self contained and not to mention that is very easy to memorize and replicate in other machines and on other OSes.

Pre-Requisites:

Setting up the driver in SQuirreL:

  • Open up SQuirreL SQL Client and select Drivers on the left to pull up the drivers list
  • Scroll down to jTDS Microsoft SQL and select it. Then click Modify the Selected Driver at the top. The icon looks like a pen or a pencil for this function.
  • In the modal window that pops up, select the Extra Class Path tab.
  • Click Add and navigate to the folder where you have downloaded the jTDS-version.jar file. Select OK to add it to the list.
  • Click Add again and navigate to the folder where you have ntlmauth.dll downloaded to. Ensure you select the driver for the appropriate architecture (x86, x64, or ia64).
  • Here is a picture for some easy reference.
Configuring SQuirrel SQL Client.

Configuring SQuirrel SQL Client.

Setting up the connection:

  • Select Aliases on the left to pull up the aliases list.
  • Click Create a new Alias at the top. The icon looks like a thick plus symbol for this function.
  • In the modal window that pops up, give the connection a name.
  • Select jTDS Microsoft SQL in the Driver drop down list.
  • Enter your URL in the following format:
    • jdbc:jtds:sqlserver://your.server.or.ip.address.com/;domain=YOURDOMAIN;
  • Use your domain credentials for username and password and hit OK to save the connection

Now double click on the newly created connection and hit connect to log on.

Happy coding!!!!

You have to install development tools first.

It appears, at first, to be a very dignified error message, very poised in telling you what exactly needs to happen and here you are thinking, oh look, it tells me what I have to do. Don’t let that fool you, I spent the better part of a evening trying to get past the above message.

Here is my stack trace:

Long story short, I was trying to install adhearsion on my laptop running Ubuntu 12.04 LTS. I had never worked on ruby before so I set about installing:

  • rvm (using curl)
  • ruby (using rvm)
  • rubygems (using rvm)
  • adhearsion (using gem)

I was having trouble getting ruby_speech (an adhearsion dependency) compiled. After loads of googling and installing all sorts of packages you think will help you get past this error, I finally found the solution documented here: Can’t install ruby_speech gem, checking for pcre.h… no

All you need is this:

And you are scott free! Cheers and happy coding!