Tuesday, November 11, 2014

Find current directory in standalone Java Program

Sometimes, we have to load a properties file in a Java Program and specially when you have to export a Jar file out of the Java Project, this becomes little difficult. The reason is, depending upon how you run your Java Application, the default directory changes. For an example, if you run it from command-line, it looks under current directory from where you are running the app. But in eclipse, its looks under Project folder (where .classpath file resides). In order to find default directory in Java Program, we can execute following line of code:

                   System.getProperty("user-dir");

This will give you default directory under your current runtime environment.


Friday, November 7, 2014

Eclipse doesn't start with error - java was started but returned exit code=13

Once you install 64-bit JDK (usually in order to upgrade to latest JDK), you may face this issue that your eclipse doesn't start and it shows error screen with following line at the top:

java was started but returned exit code=13


This is because your latest JDK is 64-bin version and Eclipse would need 32-bit JDK. If you haven't uninstalled previous version of JDK which worked for your eclipse, all you have to do is - open the eclipse.ini file and enter following two lines:

-vm
C:/Program Files/Java/jdk1.6.0_43/bin


 
Once you do this, your eclipse will start just normally like it used to be :-)

Enjoy.


Wednesday, November 5, 2014

Convert JSON String into Java Object using Jackson APIs

Guys,

In one of my tasks, today I converted a JSON response (in form of a string) coming from REST service into a Java object so that I can then execute rest of my logic on that data.

It turned out to be really simple with JACKSON APIs. Here are two lines of code that you need to write:


       ObjectMapper mapper = new ObjectMapper();
       obj =  mapper.readValue(resp, DTestConnection.class);



Here is how the JSON looked like:


     {
        "success": true,
        "connected": true
     }


And here is how my Java class looked like:


public class DTestConnection {
    String success;
    String connected;
   
   
    public String getSuccess() {
        return success;
    }
    public void setSuccess(String success) {
        this.success = success;
    }
    public String getConnected() {
        return connected;
    }
    public void setConnected(String connected) {
        this.connected = connected;
    }

}