Java - Trouble Shooting

Updated: 2018-12-10

NoSuchMethodError vs ClassNotFoundException

Both are commonly seen at runtime, telling you something is missing, but the fixes are different:

  • java.lang.NoSuchMethodError:

    • cause: the version used to build is different from the one used to run. The internal interfaces may change from the version to version, the "Method" may be available in compile time but not in run time.
    • fix: make sure you are using EXACTLY the same versions in both environment.
  • java.lang.ClassNotFoundException or NoClassDefFoundError:

    • cause: some jar is missing in classpath
    • fix: add the jar to classpath(e.g. java -cp <classpath>)

java.net.UnknownHostException

Got the error message when using Mac OS X + Java 7

java.net.UnknownHostException: ComputerName: ComputerName: nodename nor servname provided, or not known

Solution: add

127.0.0.1  ComputerName

to

/etc/hosts

Symptom

/usr/bin/ld: cannot find -ljava

Problem: Libraries are missing. -l<sth> means lib<sth>.so. e.g. -ljava means libjava.so is missing. Check /path/to/jdk1.6.0_26/jre/lib/i386.

In my case: Install 64-bit jdk to a 32-bit machine.

Solution: Install the missing libraries

String Split Error

Symptom

String.split("|");

Could not split lines like "Account|Username|Age..."

Problem:"|" will be interpreted as "or"

Solution: use

String.split("[|]");