Archive

Author Archive

Installing Oracle JDK on Ubuntu

This tutorial will cover the installation of 64-bit Oracle Java 7 (7.0_75)

  1. Check to see if your Ubuntu system architecture is 32-bit or 64-bit, mine is 64 bit. Open up a terminal and run the following command below.
    bluething@ubuntu:~$ file /sbin/init
    /sbin/init: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=7a4c688d009fc1f06ffc692f5f42ab09e68582b2, stripped
    bluething@ubuntu:~$
    
  2. Check if you have Java installed on your system. Mine doesn’t have any Java installed.
    bluething@ubuntu:~$ java -version
    The program 'java' can be found in the following packages:
    * default-jre
    * gcj-4.8-jre-headless
    * openjdk-7-jre-headless
    * gcj-4.6-jre-headless
    * openjdk-6-jre-headless
    Try: sudo apt-get install <selected package>
    bluething@ubuntu:~$
    

    If you have Java installed on your system, the command should return like this, for Ubuntu default Java is OpenJDK.

    java version "1.7.0_15"
    OpenJDK Runtime Environment (IcedTea6 1.10pre) (7b15~pre1-0lucid1)
    OpenJDK 64-Bit Server VM (build 19.0-b09, mixed mode)
    

    If you have Java installed on your system, remove the OpenJDK/JRE with the following command.

    sudo apt-get purge openjdk-\*
    
  3. Create a directory to hold your Oracle Java JDK/JRE binaries.
    bluething@ubuntu:~$ sudo mkdir -p /usr/local/java
    [sudo] password for bluething:
    bluething@ubuntu:~$ ls -l /usr/local
    total 36
    drwxr-xr-x 2 root root 4096 Apr 16 2014 bin
    drwxr-xr-x 2 root root 4096 Apr 16 2014 etc
    drwxr-xr-x 2 root root 4096 Apr 16 2014 games
    drwxr-xr-x 2 root root 4096 Apr 16 2014 include
    drwxr-xr-x 2 root root 4096 Mar 8 05:44 java
    drwxr-xr-x 4 root root 4096 Apr 16 2014 lib
    lrwxrwxrwx 1 root root 9 Mar 8 01:56 man -> share/man
    drwxr-xr-x 2 root root 4096 Apr 16 2014 sbin
    drwxr-xr-x 7 root root 4096 Apr 16 2014 share
    drwxr-xr-x 2 root root 4096 Apr 16 2014 src
    bluething@ubuntu:~$
    
  4. Download the Oracle Java JDK/JRE for Linux, you can find here. Make sure you select the correct compressed binaries for your system architecture 32-bit or 64-bit. I put on home directory.
  5. Unpack the compressed Java binaries to the directory /usr/local/java.
    bluething@ubuntu:~$ sudo tar xvzf jdk-7u75-linux-x64.tar.gz -C /usr/local/java
    bluething@ubuntu:~$ sudo tar xvzf jdk-8u40-linux-x64.tar.gz -C /usr/local/java
    bluething@ubuntu:~$ ls -l /usr/local/java
    total 8
    drwxr-xr-x 8 uucp 143 4096 Dec 18 17:04 jdk1.7.0_75
    drwxr-xr-x 8 uucp 143 4096 Feb 10 21:42 jdk1.8.0_40
    bluething@ubuntu:~$
    
  6. Edit the system PATH file /etc/profile and add the following system variables to your system path. I’m using gedit.
    JAVA_HOME=/usr/local/java/jdk1.7.0_75
    JRE_HOME=$JAVA_HOME/jre
    PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
    export JAVA_HOME
    export JRE_HOME
    export PATH
    
  7. Inform your Ubuntu Linux system where your Oracle Java JDK/JRE is located.
    bluething@ubuntu:~$ sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jdk1.7.0_75/bin/java" 1
    update-alternatives: using /usr/local/java/jdk1.7.0_75/bin/java to provide /usr/bin/java (java) in auto mode
    bluething@ubuntu:~$ sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk1.7.0_75/bin/javac" 1
    update-alternatives: using /usr/local/java/jdk1.7.0_75/bin/javac to provide /usr/bin/javac (javac) in auto mode
    bluething@ubuntu:~$ sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jdk1.7.0_75/bin/javaws" 1
    update-alternatives: using /usr/local/java/jdk1.7.0_75/bin/javaws to provide /usr/bin/javaws (javaws) in auto mode
    bluething@ubuntu:~$
    
  8. Inform your Ubuntu Linux system that Oracle Java JDK/JRE must be the default Java.
    bluething@ubuntu:~$ sudo update-alternatives --set java /usr/local/java/jdk1.7.0_75/bin/java
    bluething@ubuntu:~$ sudo update-alternatives --set javac /usr/local/java/jdk1.7.0_75/bin/javac
    bluething@ubuntu:~$ sudo update-alternatives --set javaws /usr/local/java/jdk1.7.0_75/bin/javaws
    bluething@ubuntu:~$
    
  9. Reload your system wide PATH /etc/profile.
    bluething@ubuntu:~$ . /etc/profile
    bluething@ubuntu:~$
    

    Reboot Ubuntu

  10. Test to see if Oracle Java was installed correctly on your system.
    bluething@ubuntu:~$ java -version
    java version "1.7.0_75"
    Java(TM) SE Runtime Environment (build 1.7.0_75-b13)
    Java HotSpot(TM) 64-Bit Server VM (build 24.75-b04, mixed mode)
    bluething@ubuntu:~$
    

 

Categories: JAVA, Ubuntu

Handshake

February 22, 2015 Leave a comment

Problem Statement

At the annual meeting of Board of Directors of Acme Inc, every one starts shaking hands with everyone else in the room. Given the fact that any two persons shake hand exactly once, Can you tell the total count of handshakes?

Input Format
The first line contains the number of test cases T, T lines follow.
Each line then contains an integer N, the total number of Board of Directors of Acme.

Output Format

Print the number of handshakes for each test-case in a new line.

Constraints

1 <= T <= 1000
0 < N < 106

This problem is about combinations. The formula is

For factorial calculation I’m using recursive with memoization to optimization process. Read more…

The Love-Letter Mystery

February 22, 2015 Leave a comment

James found a love letter his friend Harry has written for his girlfriend. James is a prankster, so he decides to meddle with the letter. He changes all the words in the letter into palindromes.

To do this, he follows 2 rules:

  1. He can reduce the value of a letter, e.g. he can change ‘d’ to ‘c’, but he cannot change ‘c’ to ‘d’.
  2. In order to form a palindrome, if he has to repeatedly reduce the value of a letter, he can do it until the letter becomes ‘a’. Once a letter has been changed to ‘a’, it can no longer be changed.

Each reduction in the value of any letter is counted as a single operation. Find the minimum number of operations required to convert a given string into a palindrome.

Read more…

Categories: Umum Tags: , ,

MongoDB Learning Notes – Simulate automate failover

February 22, 2015 Leave a comment

To simulate automate failover I kill the primary node with Ctrl+C or use command db.shutdownServer(). When the primary killed, the secondary become primary because a majority of the original nodes (the arbiter and the secondary) are still able ping each other.

myapp:PRIMARY> db.shutdownServer()
shutdown command only works with the admin database; try 'use admin'
myapp:PRIMARY> use admin
switched to db admin
myapp:PRIMARY> db.shutdownServer()

And in secondary node log:

2015-02-22T13:58:33.558+0700 [rsHealthPoll] replSet member bluething-PC:20000 is now in state DOWN
2015-02-22T13:58:34.391+0700 [conn501] replSet info voting yea for bluething-PC:20001 (1)
2015-02-22T13:58:36.565+0700 [rsHealthPoll] warning: Failed to connect to 192.168.19.1:20000, reason
: errno:10061 No connection could be made because the target machine actively refused it.
2015-02-22T13:58:36.565+0700 [rsHealthPoll] replset info bluething-PC:20000 heartbeat failed, retrying
2015-02-22T13:58:36.632+0700 [rsHealthPoll] replSet member bluething-PC:20001 is now in state PRIMARY

Connect to secondary and check status Read more…

MongoDB Learning Notes – Replication Overview and Setup replica set

February 22, 2015 Leave a comment

MongoDB provides two flavors of replication: master-slave replication and replica sets. For both, a single primary node receives all writes, and then all secondary nodes read and apply those write to themselves asynchronously. Replica sets additionally ensure automated failover: if the primary goes offline for any reason, then one of the secondary nodes will automatically be promoted to primary, if possible.

The minimum recommended replica sets configuration consists of three nodes. Two of these nodes serve as first-class, persistent mongod instance. Either can act as the replica set primary, and both have a full copy of the data. The third node in the set is an arbiter, which doesn’t replicate data, but merely acts as a kind of neutral observer, when failover is required, the arbiter helps to elect a new primary node.

1. Creating a data directory
I’m using windows and data directory is C:\data

mkdir C:\data\node1
mkdir C:\data\node2
mkdir C:\data\arbiter

2. Start each node as separate mongod (in separate terminal) Read more…

Categories: MongoDB, NoSQL Tags: , ,

Send logs by Email Notification using Apache log4j SMTPAppender

February 15, 2015 Leave a comment

Apache log4j provides log Appender (called SMTPAppender) to send email alerts for the log level configured in log4j configuration file. For example I using simple struts 2 application.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.bluething</groupId>
 <artifactId>SimpleStruts2</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>SimpleStruts2 Maven Webapp</name>
 <url>http://maven.apache.org</url>

 <dependencies>

 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>3.8.1</version>
 <scope>test</scope>
 </dependency>

 <dependency>
 <groupId>org.apache.struts</groupId>
 <artifactId>struts2-core</artifactId>
 <version>2.3.16.3</version>
 </dependency>
 <dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.17</version>
 </dependency>

 <dependency>
 <groupId>javax.mail</groupId>
 <artifactId>mail</artifactId>
 <version>1.4</version>
 </dependency>

 </dependencies>

 <build>
 <finalName>SimpleStruts2</finalName>
 <plugins>
 <plugin>
 <artifactId>maven-compiler-plugin</artifactId>
 <configuration>
 <source>1.7</source>
 <target>1.7</target>
 </configuration>
 </plugin>
 </plugins>
 </build>
</project>

Read more…

Categories: JAVA

Pangrams

Problem:

Roy wanted to increase his typing speed for programming contests. So, his friend advised him to type the sentence “The quick brown fox jumps over the lazy dog” repeatedly because it is a pangram. ( pangrams are sentences constructed by using every letter of the alphabet at least once. ) After typing the sentence several times, Roy became bored with it. So he started to look for other pangrams. Given a sentence s, tell Roy if it is a pangram or not.

From Wikipedia, A pangram or holoalphabetic sentence for a given alphabet is a sentence using every letter of the alphabet at least once. Check both uppercase and lowercase(+32).

Using Java String function indexOf() to check if the character appear in the sentence.

Java code:


import java.util.Scanner;

public class Pangram {

public static void main(String[] args) {
 Scanner in = new Scanner(System.in);
 String input = in.nextLine();
 if(testPangram(input)) {
 System.out.println("pangram");
 } else {
 System.out.println("not pangram");
 }
 in.close();
 }

 static boolean testPangram(String input) {
 for(char a = 'A' ; a <= 'Z' ; a++) {
 if(input.indexOf(a) < 0 && input.indexOf((char) a+32) < 0) {
 return false;
 }
 }
 return true;
 }
}

Result for input “We promptly judged antique ivory buckles for the prize “:

pangrams2

Result for input “ABCDEFGHIJKLMNOPQSTUVWXYZ”:

pangrams1

Source:

  1. https://www.hackerrank.com/challenges/pangrams
  2. http://en.wikipedia.org/wiki/Pangram
Categories: JAVA Tags: , ,
Follow

Get every new post delivered to your Inbox.