Tuesday 20 July 2010

Fixing NullPointerException in EJB serialization in GlassFish

Getting this: org.omg.CORBA.MARSHAL: vmcid: SUN minor code: 211 completed: Maybe ?
Look at your EJB implementation. If it looks like this:

@Remote
...
public class SomeEJBImpl implements SomeEJB {
...
}


Change it this way:

@Remote(SomeEJB.class)
...
public class SomeEJBImpl implements SomeEJB {
...
}

Tuesday 4 May 2010

Eclipse WTP and Maven NullPointerException workaround

When you are trying to enable Maven support in Eclipse project, it's most probably because of WTP and Maven integration bug.

Symptoms:
An internal error occurred during: "Updating classpath container: project name".
java.lang.NullPointerException
 at org.eclipse.wst.common.componentcore.internal.resources.VirtualResource.removeLink(VirtualResource.java:300)
Solution is to edit .project file by hand and to include these natures in appropriate tag (you'll find it):
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>

Monday 18 January 2010

Proper way to compare java.math.BigDecimal

import java.math.BigDecimal;
import java.math.MathContext;

public class BigDecimalTest {

 public static void main(String[] args) {
  
  System.out.println(new BigDecimal(0).equals(BigDecimal.ZERO));
  System.out.println(new BigDecimal("0").equals(BigDecimal.ZERO));
  System.out.println(new BigDecimal(0.0d).equals(BigDecimal.ZERO));
  System.out.println(new BigDecimal(0.0f).equals(BigDecimal.ZERO));
  
  //WTF
  System.out.println(new BigDecimal("0.0").equals(BigDecimal.ZERO)); // false!!!
  System.out.println(new BigDecimal("0.0").equals(new BigDecimal("0"))); // false!!!
  System.out.println(new BigDecimal("0.0").equals(new BigDecimal(0.0))); // false!!!
  
  //same is with "1" and "1.0", and so on

  //That's not a precision problem
  MathContext mc = new MathContext(2);
  System.out.println(new BigDecimal("0.0", mc).equals(new BigDecimal(0.0, mc))); // false!!!
  
  //So, the proper way to compare two big decimals is:
  System.out.println(new BigDecimal("0.0").compareTo(BigDecimal.ZERO) == 0); 
 }
The answer lies in API docs: BigDecimal#equals.

Wednesday 13 January 2010

Make Eclipse faster by limiting number of open editor tabs

Eclipse can close tabs automatically. This improves performance, saves memory and keeps your workspace cleaner.

  1. Open Preferences
  2. Go to General > Editors 
  3. Set "Number of opened editors before closing" to something small. I use 8.
  4. Apply and enjoy 

Friday 8 January 2010

Importing self signed certificate into Java's cacerts keystore

Symptoms

  •  Getting an exception:
    javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
Possible cause of problem
  • Self-signed SSL certificate is used on remote server.

Possible solution
  1. Open remote page with Firefox. You should see something like a warning page.
  2. Click on "I Understand the Risks". You should really understand them.
  3. Click "Add Exception".
  4. Click "View" to view certificate.
  5. Go to "Details" tab and click "Export".
  6. Save certificate as X.509 Certificate (PEM) somewhere to your file system
  7. Don't forget to click "Confirm Security Exception" in Firefox.
  8. Execute command: keytool -keystore $JAVA_HOME/jre/lib/security/cacerts -import -file /path/to/cert.cer
  9. When you are prompted for a password and you haven't changed it, use the default: changeit
References

Thursday 7 January 2010

Introduction

Java WTF is...
  • a collection of short and concrete tips about Java development
  • a collection of Java developer's problems along with short and concrete solutions 
  • no bullshit 
Simple as that.