Wednesday, April 22, 2015

AWS EC2: Install Node.js on an Amazon Linux AMI instance

I wanted to install Node.js on my Amazon Linux EC2 instance. Unlike Ubuntu, I can't just apt-get install nodejs.

The solution appears to be very simple. I just need to enable the EPEL (Extra Packages for Enterprise Linux) repository to get yum install to work.

Steps:
1. Edit the repository config file
  sudo nano /etc/yum.repos.d/epel.repo

2. In the [epel] section, change the enabled flag to 1

3. Press Ctrl+O to save the file, and then Ctrl+X to exit

4. Update yum
  sudo yum update

5. Install npm and Node.js
  sudo yum install npm

6. Verify Node.js is installed
  node -v


Ref:
How to install node.js on AWS AMI Linux instance | SpiderSoft

Tuesday, April 14, 2015

Java: Send email through Gmail's SMTP server

There are various examples of using the JavaMail API for sending emails, e.g.
I was able to get my program up and running relatively quick. However, the code stopped working once I deployed it to my AWS EC2 instance. It threw an javax.mail.AuthenticationFailedException, and links to https://support.google.com/mail/bin/answer.py?answer=78754


Solution:
Following the link above, I was able to resolve the issue by enabling allow less secure apps and allow access from a new location or device.

Monday, April 6, 2015

AngularJS: issue with progressbar on IE 11

For:
AngularJS v1.3.14
UI Bootstrap 0.12.1

I used AngularJS UI Bootstrap to display a progress bar in my Web app. It works fine in Firefox and Chrome. But on IE 11, the progress is not getting updated. The weird part is that, if I enable the developer tools in IE, then the progress bar works as expected.

A quick search led me to this article which describe the exact same issue, but for IE 10. It was reported back in 2013, and it has been fixed in a newer version of UI Bootstrap. Then I checked the Internet Explorer Compatibility but I didn't find anything relevant. The progress bar demo on UI Bootstrap site is working fine as well.

My next thought was that, perhaps I should detect and add special handling just for IE 11 until I have more time to investigate the root cause. So I started to look for ways to detect IE 11, and the most common practice seems to be using conditional comments. Unfortunately, it does not work with IE 11. It turns out conditional comments are no longer supported since IE 10.

While I was struggling with IE, I noticed something weird. The way my Web app works is that, it polls a .json file through $http.get() to update the progress every 250ms. But looking at the server logs, only 1 GET request was made, which explains why the progress is not getting updated. It suggested that IE is being "smart" and caching the .json response. Searching online, it looks like other people ran into the same issue as well.

In my case, I used Node.js to run the server. I was able to resolve the issue by setting the following headers in my json response:
response.writeHead(200, {"Content-Type": "application/json",
                                     "Cache-Control": "no-cache, no-store, must-revalidate",
                                     "Pragma": "no-cache",
                                     "Expires": 0});

Monday, March 30, 2015

Java: Getting console output of an executable .jar file on Windows

Problem:
I added a few System.out.println() statements in my executable .jar for logging purposes. It works fine in Eclipse. But when I run it through cmd, I noticed there's no console output at all.


What's happening?
By default, .jar files are handled by javaw.exe instead of java.exe on Windows.

"The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you do not want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails."
http://docs.oracle.com/javase/8/docs/technotes/tools/windows/java.html

So this is by design.


Solution:
If you want to get the console output, there are 2 options.

#1. Manually call java.exe to execute your .jar file, e.g.
java -jar <your_jar_file_path>

- or -

#2. Change the .jar file association from javaw.exe to java.exe
a) Open regedit
b) Navigate to HKEY_CLASSES_ROOT\jarfile\shell\open\command
c) Change the path from javaw.exe to java.exe

Sunday, February 1, 2015

Java: Create executable .jar file with command line

Continue my journey on compiling Java program in command line, I wonder how to create a runnable .jar without an IDE. Turns out it is fairly simple as I'm not dealing with a complex project.

To create an executable .jar:
jar -cvfe <output_name> <entry point> <class file>
e.g. jar -cvfe output.jar HelloWorld *.class

-c  create new archive
-v  generate verbose output on standard output
-f  specify archive file name
-e  specify application entry point for stand-alone application bundled into an executable jar file


Ref:
How to create and execute JAR file in Java – Command line Eclipse Netbeans

Wednesday, January 28, 2015

Java: Compile and run your program in command line

A friend needed some help to debug a Java program. I have JDK installed, but no Java IDE. Instead of installing one, command line come to rescue! ;)


To compile the .java files:
javac *.java

You can use the -d option to specify where to place generated class files, e.g.
javac -d "build" *.java


To run the compiled code:
java <class_name>
e.g. java HelloWorld

You can use the -cp option to specify the class search path, e.g.
java -cp "build" HelloWorld

If you defined a package, you'll need to specify it, e.g.
java -cp "build" MyPackage.HelloWorld

Tuesday, January 20, 2015

Mac: Write .img file to a SD card

1. Use diskutil list to determine the driver identifier of the SD card

2. Unmount the SD card.e.g. diskutil unmount /dev/disk3s1

3. Write the .img file to the SD card, e.g.
sudo dd if=filename.img of=/dev/rdisk3 bs=1m


Ref:
osx - Writing .img file to SD Card from a Mac - Raspberry Pi Stack Exchange