tags:

views:

978

answers:

2

Hello this is Lonnie Ribordy,

I have a program i am trying to write and part of it uses a 3rd party api called JDom, when i compile my program the it compiles perfectly fine.. but, when i try to run it i get the Exception in thread "main" java.lang.noClassDefFoundError org/jdom/input/SAXBuilder

my program is as below...


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;

public class COS extends JPanel implements ActionListener{
    static JFrame f=new JFrame();
    Image bgImage=null;
    String message="";
    public COS(){
     try{
      String xml="background.xml";
      SAXBuilder builder=new SAXBuilder();
      Document doc=builder.build(new File(xml));
      Element root=null;
      Element img=null;
      String fimg=null;
      try{
       root=doc.getRootElement();
       img=root.getChild("bgimage");
       fimg=img.getText();
      } catch(Exception e){
      }
      getFileImage(fimg);
     } catch(Exception e){
      message="File load failed: "+e.getMessage();
     }
    }
    public void paintComponent(Graphics g){
     if(bgImage!=null){
      g.drawImage(bgImage,0,0,this);
     }
     else{
      g.drawString(message,40,40);
     }
    }
    public void getFileImage(String filein) throws IOException, InterruptedException{
     FileInputStream in=new FileInputStream(filein);
     byte[] b=new byte[in.available()];
     in.read(b);
     in.close();
     bgImage=Toolkit.getDefaultToolkit().createImage(b);
     MediaTracker mt=new MediaTracker(this);
     mt.addImage(bgImage,0);
     mt.waitForAll();
    }
    public void actionPerformed(ActionEvent e){

    }
    public static void main(String[] args){
     COS newcos=new COS();
     f.setSize(825,640);
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     f.getContentPane().setLayout(null);
     newcos.setBounds(5,5,800,600);
     f.setLocation(10,5);
     f.getContentPane().add(newcos);
     f.setVisible(true);
    }
}

could anybody tell what's wrong?
A: 

I believe the issue is that you have an old version of JDom in your classpath, and that is being loaded before the one you want.

Firstly, make sure that you don't have another version of JDom (besides the one you've downloaded) kicking about in your classpath. To find out where the

org.jdom.input.SAXBuilder

class is being loaded from download JWhich and use that to check where the class is being loaded from.

Secondly, if you're using maven check that another version of JDOM isn't including it as a dependency, to do this use the mvn dependency:tree command.

Jon
what are some parsers i should look for? i know java have some built but JDom is the first one i included my self it is JDom 1.1 from Jdom's binary page.. and i write all my stuff by hand so i don't have maven or anything
Find out which JAR org.jdom.input.SAXBuilder is loaded from first...
Jon
well apparently it's not even being found.. i deleted all of the jdom jars to reinstall them, which jars from JDom 1.1 do i put in? is it just jdom.jar? or must include the xerces.jar..etc?
Hmm, ok it's difficult to see what's going on with your project... see attach file which demonstrates your source code above working with JDom added as a dependency via Maven, just import the Maven project into Eclipse (make sure you have the m2eclipse plugin installed) - http://www.jonathanholloway.co.uk/jdomexample.zip
Jon
It looks like you just need the jdom 1.1 jar. You need to make sure that it's on your classpath when you run your COS example, i.e. java -classpath .;jdom-1.1.jar COS Hope that helps
Jon
+1  A: 

i found out my problem, when i installed JDom into my java i forgot to include it in the jre lib/ext

and now everything works just like it is supposed to work, thankyou very much for the time you spent helping me

Lonnie Ribordy
Cool, but this should be in your classpath, and not added to the jre's lib/ext ideally... see http://mindprod.com/jgloss/classpath.html
Jon