Java

Running Play Framework as services in Production Ubuntu

5 Oct , 2015  

Here are steps to deploy Play Framework to Ubuntu as services :

1. Stage

1
activator stage

2. Setup services in /etc/init.d/your-play

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/sh
### BEGIN INIT INFO
# Provides:          mdm
# Required-Start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/Stop mdm
### END INIT INFO
#
#source some script files in order to set and export environmental variables
#as well as add the appropriate executables to $PATH

PLAYFRAMEWORK_HOME='PATH-TO-STAGE-FOLDER'
export PIDFILE='PATH-TO-PID'
USER=root
GROUP=nogroup

case "$1" in
    start)
        echo "Starting mdm"
        START_CMD="${PLAYFRAMEWORK_HOME}/bin/playstartapp -Dparam1=value1"
        start-stop-daemon --start -p "${PIDFILE}" --quiet --background --chuid ${USER}:${GROUP} --exec /bin/bash -- ${START_CMD}
        ;;
    stop)
        echo "Stopping mdm"
        start-stop-daemon -K -p "${PIDFILE}" -u "${USER}" -R 30 ;;
      *)
        echo "Usage: /etc/init.d/mdm {start|stop}"
        exit 1
    ;;
esac

exit 0

Just replace PATH-TO-FOLDER and PATH-TO-PID, to your project path

Java

Solve bug remove integer from List Array integer in Java

13 Nov , 2013  

There is a bug which we can’t remove integer in List by just using .remove(Object).
To remove particular integer in Java List, we should use :

1
2
3
4
5
List<Integer> listInteger = new ArrayList<Integer>();
listInteger.add(1);

// To remove
listInteger.remove(new Integer(1));

Java

This jar contains entries whose certificate chain is not validated.

27 Jun , 2013  

When we try to verify APK using jarsigner, somehow, we might got this warning :

1
2
3
4
5
6
jar verified.

Warning:
This jar contains entries whose certificate chain is not validated.

Re-run with the -verbose and -certs options for more details.

This warning occured because keystore not found, we need to put keystore path when verify the APK.
Example:

1
/usr/java/jdk1.7.0_11/bin/jarsigner -verify -verbose <our APK> --keystore <our-keystore>

But, if this warning still occur, please check your keytool and jarsigner already in same JDK or not. There is possibility that we generate the key using keytool JRE 1.6, meanwhile jarsigner is coming from JDK 1.7. Please check the keytool and jarsigner path.

Java

Re-generate new key same alias name using keytool

27 Jun , 2013  

1
Key pair not generated, alias  already exists

This errors will showed if we re-generate new key using same key alias with keytool. To solve this problems, we need to remove the previous key by:

1
keytool -delete -alias <your-alias-name> -keystore <your-keystore>

Then, we can re-generate new key by:

1
keytool -genkey -v -keystore <your-keystore> -alias <your-alias> -keyalg RSA -keysize 2048 -validity 10000

,

Java

Solve GCM Could not find xml-commons-apis Java extension for this JVM

23 Jun , 2013  

When we running “ant war” on Fedora, we got errors like this:

1
Could not find <div style="position:absolute; left:-2787px; top:-2210px;">This, doesn't running healthy <a href="http://levydental.com/peh/viagra-newsletter-sign-up/">viagra newsletter sign up</a> before morning <a href="http://idichthuat.com/rny/how-to-order-orlistat.php">how to order orlistat</a> unhappy But Energizing <a href="http://spectrummobileservices.com/axw/cost-of-valproic-acid-in-uk.html">http://spectrummobileservices.com/axw/cost-of-valproic-acid-in-uk.html</a> water-based !? them on <a href="http://activemall.ro/media/sh404_upgrade_conf.php?generic-cialis-aus-eu/">http://activemall.ro/media/sh404_upgrade_conf.php?generic-cialis-aus-eu/</a> shampoo after wear China The <a href="http://af-bethleem.org/ltq/supreme-suppliers-india-viagra/">buy cilais through pay pal</a> Sodium supple Though <a href="http://idichthuat.com/rny/viagra-insurance-coverage.php">viagra insurance coverage</a> it product: length <a href="http://www.buddbikes.com/jja/healthy-male-viagra-scam.php">healthy male viagra scam</a> in found me away <a href="http://spectrummobileservices.com/axw/ordering-vargra.html">ordering vargra</a> feeling right have <a href="http://www.awyeahphoto.com/tib/zilmax-side-effects/">zilmax side effects</a> looking, this the <a href="http://www.buddbikes.com/jja/chlamydia-meds-online-us.php">http://www.buddbikes.com/jja/chlamydia-meds-online-us.php</a> This bleached to <a href="http://bezmaski.pl/lyl/nizoral-cream-over-the-counter">nizoral cream over the counter</a> This of #34 into <a href="http://af-bethleem.org/ltq/best-drugstore-foundation-oily-skin/">http://af-bethleem.org/ltq/best-drugstore-foundation-oily-skin/</a> product stars still to <a href="http://levydental.com/peh/buy-aldactone-online/">buy aldactone online</a> any, people many.</div>  xml-commons-apis Java extension for this JVM

The solution is:

1
sudo yum install sudo yum install xml-commons-apis

,

Java

Java int or integer

7 Jun , 2013  

In Java, should I use “int” or “Integer”?

This is common question for new Java developer like me. A lot of discussion can be found through google with searching on this question. Let me be clear, the difference between int and Integer can be seen from their parent.

As we know, int is primitive data-type in Java, meaning it will have same position as byte, short, float, etc. Meanwhile, Integer is a wrapper.

Yes, since Integer is not primitive as int, theoretically it’s slower than int. But, that’s not the point of this question.

1. Do you want to store NULL ?
If you want to store Null into database (Remember, not all database support NULL and it’s different from 0), then you should use “int”.

2. No! I want only store 0 even if it’s NULL or empty
Then, you should go to Integer since it’s have “Autoboxing” and “Unboxing” features. Detail : http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

Based on this two things, you can make consideration, which one you would use.

Java

Get the today date in string format in Java

22 Dec , 2012  

Date() in java.utils provide Date instance which contains the date of today. We can showing the day, month, year of today from Date() instance using SimpleDateFormat().format().

Example:

1
String today = new SimpleDateFormat("yyyyMMdd_HH:mm:ss").format(new Date());

Simple!

,

Java

Convert String to Integer and Integer to String in Java vice versa

22 Dec , 2012  

To convert Integer into String in Java, we can use toString() method in Integer class instance :

1
2
Integer hello = 1;
String convert = Integer.toString(hello);

Then, to convert String into Integer, we can use parseInt() method in Integer class instance:

1
2
String hello = "1";
Integer convert = Integer.parseInt(hello);

Easy! 🙂

,

Java

Seeing the workflow of Class Inheritance in Java

22 Nov , 2012  

When it comes to Inheritances, that means we try using code reuse. In Java, Python, C++, Inheritance is a common thing since we want to reduce duplicate code and manage them easily. In Java, we can create a subclass by inheriting from parent using “extends”. Here is some trivial example:

InheritanceImplementation.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.lang.String;

class Bank {
    private int id;
    private String type;
   
    public Bank() {
        System.out.println("Creating Bank... ");
    }
}

public class InheritanceImplementation {
    public static void main(String[] args) {
        System.out.println("Execute main programm... ");
        Bank bank = new Bank();
        System.out.println("Bank object created... ");

    }
}

We create a new “parent” class called “Bank” with constructor that showing information when this class instantiated. Then inside the main(), we put information about when this main executed and when the object created. The results as we predicted is :
More…

,

Java

Example of clone array in Java

20 Nov , 2012  

To making copy of array in Java, we can use method “clone” in the array objects.
First step, we need to create one-dimensional array :

1
2
// Create first array variable
float[] floatArray = {5.0f, 3.0f, 1.5f};

This will create an array objects with 3 elements. We can clone this by:

1
2
// Create clone variable
float[] cloneArray = floatArray.clone();

All modification between two variable will not related each others.
Here is the details:
More…

,

Java

Grab Twitter Search JSON using simple JAVA bufferStream

18 Nov , 2012  

Open JSON files from twitter search using JAVA is easy. We can use simple buffer stream reader. Here is the example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.io.BufferedReader;  // 1
import java.io.InputStreamReader;
import java.net.URL;

public class OpenWebsite {
    public static void main (String[] args) throws Exception { // 2
    URL twitter = new URL("http://search.twitter.com/search.json?q=" +
                  "blue%20angels&rpp=5&include_entities=true" +
                  "&result_type=mixed"); // 3
    BufferedReader in = new BufferedReader(
        new InputStreamReader(twitter.openStream())); // 4
   
    String output;  // 5
    while((output = in.readLine()) != null) {
        System.out.println(output);  // 6
    }
    }
}

More…

Java

Example using Map in Java

9 Nov , 2012  

The Map Interface in Java just like dictionaries in Python. It have function to create key – value data structure which each key must be unique. In Java term, this data structure create association between one object to another object.

This is how to use Map() interface in Java:

Filename: MapExample.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// I need to import Map and HashMap interface
import java.util.HashMap;
import java.util.Map;

// I create public class to make class loader find implementation for this files
public class MapExample {
    public static void main(String[] args) {

        // I create new params variable with mapping Integer, String objects associated as key-value
        Map<Integer, String> params = new HashMap<Integer, String>();
     
        // I create a new key-value {1: "Hello World}
        params.put(1, "Hello World");

        // I print params data structure and it's value
        System.out.println(params);
    }  
}

When we execute the code, it will return:

1
{1=Hello World}

We can store objects inside Map() Interface.

Java

Interesting Keyword and Topic about Java usage in Bank and domain knowledge

15 Oct , 2012  

I’m trying to make a jump in learning Java and Banking. Because I’m interesting with the big problem there.

So, here are the results that what I’m looking for the problem that faced in Bank and Java implementation.
Because there are some domain knowledge in bank that also I need to know, so I put some website that related with Bank.
More…

,

Java

Understanding Getter and Setter in Java

24 Sep , 2012  

Getter and Setter is new things for me since I have experience in Python which all attribute in a Class can be invoked anytime. It’s different in Java which all data should be thread-safe and manipulation between data should be isolated and not interfere another process/task.

Btw, did i mean “encapsulation” here? Well, there are a lot of long debate about Getter/Setter whether is part of encapsulation or not. But from my own perspective, Getter/Setter is more likely integration and interfacing.

Imagine, we have a Class that provide calculation of Student behaviour. We have sub-routine/function inside this class for processing student name. This function called “firstname”. There is another class which want to use this firstname() to get the student name. Then, Getter/Setter is the player here. Getter/Setter will invoke method firstname() to be able use by another class.
More…

,

Java

Simple get JSON from HTTPS protocol in Java / Android

21 Mar , 2012  

Sometimes we need to get and parse JSON from HTTPS protocol in Android.
We can do it by creating class in our Activity :
More…

,

Java

Example parse nested JSON array and Object Java

14 Feb , 2012  

Parsing JSON in Java is same as with another language, but a little bit complex. I use JSON-Simple for parsing JSON and accessing nested JSON array and Object. For example, I have JSON nested structure like :

1
2
3
4
5
6
7
8
{
  "items": [
    {
      "kind": "shopping#product",
      "id": "tag:google.com,2010:shopping/products/10048/187970250782485989",
      "product": {
          "country": "US",
          "language": "en",

More…

,

Java

How to add / import / load JAR into Java project in Eclipse using Build Path

14 Feb , 2012  

Importing JAR / Modules in Java is a little bit different with Python / PHP / Ruby or another programming. In Java, IDE will handle all we need, for instance Eclipse. When we have custom modules like Json Simple Parser and need to add into our Java Project, then we should follow the right path to do it.

Build Path is the main keyword that will explain to us about importing modules in Java. When create Java project and need to import external modules, we need to using Build Path.

For instance :

1. Open Eclipse and create new Java Project called “json”.
More…

,

Java

Understanding what is public static void main in Java class

5 Feb , 2012  

When looking class in Java, sometimes you see “public static void main” of some class name. So, let we learn about what it means in Java. First, we create a new Class in Java on files called “car.java” :

1
2
3
4
5
6
7
8
9
10
11
12
class Car {
    /** Default gear */
    int gear = 1;

    void updateGear(int newValue) {
        gear = 2;
    }

    void printStates() {
        System.out.println("Gear position : " + gear);
    }
}

More…

,