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;
    }

}


 

Friday, October 31, 2014

Enable J2EE project or Java Web Project for Scala Code

Now a days, it has been very common to use SCALA programming language in Java based project. This may be chosen to get advantage of SCALA's multithreaded nature or this can also be done to slowly migrate Java based Web application from Java to Scala.

Recently, I had to integrate scala in a J2EE web project having JSP pages and Java classes. Here is what I could find out about integrating scala in J2EE project in Eclipse:

Pre-requisites:
1.) You have Eclipse platform with J2EE project setup.
2.) Your eclipse is enabled for SCALA

How to enable your J2EE project so that it can also run SCALA code?

Right click your Dynamic Web project --> Configuration --> Add Scala Nature.




Once you do this, the Eclipse will add SCALA libraries to your web project and your project will be able to compile and run the SCALA code just like Java Code.

Good Luck!

Tuesday, October 28, 2014

MDB did not pick message from queue with error: WebSphere MQ call failed with compcode '2' ('MQCC_FAILED') reason '2010' ('MQRC_DATA_LENGTH_ERROR')

You have a Message Driver Bean (MDB) which is not picking up message arriving on MQ queue and it gives following error?

MDB did not pick message from queue with error: WebSphere MQ call failed with compcode '2' ('MQCC_FAILED') reason '2010' ('MQRC_DATA_LENGTH_ERROR')


Caused by: com.ibm.msg.client.jms.DetailedJMSException: JMSWMQ2002: Failed to get a message from destination 'EVENT.OUT'. WebSphere MQ classes for JMS attempted to perform an MQGET; however WebSphere MQ reported an error. Use the linked exception to determine the cause of this error.


I also got this error. This error occurs because the default message length for any Queue or Channel is set to be 4194304 bytes. And this needs to be changed to accept messages of bigger size.

So, I changed the size to 100MB for my Channel (I was using SYSTEM.DEF.SVRCONN) and restarted the MQ server.



The MDB started picking up message. Let me know if this doesn't work.