I have been working through a java textbook. I have a reasonable grasp of the basic concepts of java programming but one area I am always struggling with is getting the system to recognise 3rd party jar files. I just never seem to be able to get them to work.
I set up the classpath and add them to the directory in the classpath but I always get compiler errors or run time errors not finidng the class. For example, in order to make the apache xml-RPC jar files work I read that all I had to do was add xmlrpc-2.0.jar and xmlrpc-2.0-applet.jar to the classpath. I set the classpath to c:\program files\java\jdk1.6.0_13\lib\ext and put theses files in there, I then tried to compile a piece of example code out of the textbook which worked. However, when I ran the program I got:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/codec/DecoderException
at org.apache.xmlrpc.XmlRpc.createTypeFactory(XmlRpc. java:238)
at org.apache.xmlrpc.XmlRpc.<init>(XmlRpc.java:193)
at org.apache.xmlrpc.XmlRpcClientResponseProcessor.<i nit>(XmlRpcClientResponseProcessor.java:48
at org.apache.xmlrpc.XmlRpcClientWorker.<init>(XmlRpc ClientWorker.java:43)
at org.apache.xmlrpc.XmlRpcClient.getWorker(XmlRpcCli ent.java:347)
at org.apache.xmlrpc.XmlRpcClient.execute(XmlRpcClien t.java:190)
at org.apache.xmlrpc.XmlRpcClient.execute(XmlRpcClien t.java:184)
at org.apache.xmlrpc.XmlRpcClient.execute(XmlRpcClien t.java:177)
at SiteClient.getRandomSite(SiteClient.java:27)
at SiteClient.main(SiteClient.java:14)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.codec.DecoderException
at java.net.URLClassLoader$1.run(URLClassLoader.java: 200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:188)
at sun.misc.Launcher$ExtClassLoader.findClass(Launche r.java:229)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 52)
at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:320)
... 10 more
The program itself connects to the author of the books test server and calls a remote procedure called getRandomSite() which returns 3 string variables - url, title and description and outputs them to the console.
The code from the application is below, it is taken from SAMS Teach Yourself Java in 21 Days:
import java.io.*; import java.util.*; import java.net.*; import org.apache.xmlrpc.*; public class SiteClient{ String url; String title; String description; public static void main(String[] args){ SiteClient sClient = new SiteClient(); try { Vector response = new Vector(); response = sClient.getRandomSite(); if (response.size() > 0) { sClient.url = response.get(1).toString(); sClient.title = response.get(2).toString(); sClient.description = response.get(3).toString(); System.out.print("Url: " + sClient.url + "\nTitle: " + sClient.title + "\nDescription: " + sClient.description); } } catch (IOException ioe) { System.out.print("IO Exception: " + ioe.getMessage()); } catch (XmlRpcException xrpc) { System.out.print("XML Remote Procedure Call Error: " + xrpc.getMessage()); } } public Vector getRandomSite() throws IOException, XmlRpcException{ Vector params = new Vector(); XmlRpcClient client = new XmlRpcClient("http://cadenhead.org:4413"); Vector result = (Vector) client.execute("dmoz.getRandomSite", params); return result; } }
Can anyone tell me what I'm doing wrong, I've had this problem with most 3rd party jars but was able to get the XOM API working, I haven't done anything differently here.