1

I'm trying to parse a bit of JSON in Java by following this tutorial. Because it mentions nothing about org.apache.commons.io.IOUtils other than it imports it, I figured all it was used for it to convert an InputStream into a String. I have my own code for that, so I omitted it. Here is my code:

import java.io.*;
import java.io.InputStream;
import java.net.*;
import java.util.Scanner;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

public class Net {
    static String returnParsedIp() throws UnsupportedEncodingException,    MalformedURLException, IOException {
    String url = "http://httpbin.org/ip";
    URLConnection connection = new URL(url).openConnection();
    connection.setRequestProperty("Accept-Charset", "UTF-8");
    InputStream response = connection.getInputStream();
    String responseString = new Scanner(response,"UTF-8").useDelimiter("\\A").next();
    //return responseString;

    JSONObject json = (JSONObject) JSONSerializer.toJSON(responseString);
    String Ip = json.getString("origin");

    return Ip;
}
}

I then call the method from my main class with:

public class DDNS {

    public static void main(String[] args) throws Exception {
        String Ip = Net.returnParsedIp();
        System.out.println(Ip);
    }   
}

However, it throws the following error:

run:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at ddns.Net.returnParsedIp(Net.java:19)
    at ddns.DDNS.main(DDNS.java:6)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 14 more
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Any help is appreciated.

achingfingers
  • 1,827
  • 5
  • 24
  • 48
Novicode
  • 275
  • 2
  • 3
  • 12
  • I try to do the tutorial and the dependency need a classifier: net.sf.json-lib json-lib 2.4 jdk15 – pdem Jun 17 '14 at 12:59
  • @pdem Is this something for eclipse? I use Netbeans, and in Netbeans you can just add the jar as a dependency through file browsing. – Novicode Jun 17 '14 at 13:06
  • Yes this this a problem for eclipse: the m2e plugin find the library, but doesn't have the classifier, we have to add it either in properties or in pom.xml source – pdem Jun 17 '14 at 13:25

2 Answers2

1

Either you don't have the Apache Commons lang and io JARs in your CLASSPATH or you have the wrong version.

Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException

This is Java 101. You need all the dependencies in the CLASSPATH in order to compile and run.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • You were right, I was using the wrong version. I'm going to find an alternative to json-lib anyways, as it needs at least 6 more dependencies, which is ridiculous. – Novicode Jun 17 '14 at 13:43
0

You have to add the commons-lang jar in yout project

Pracede
  • 4,226
  • 16
  • 65
  • 110