0

I am working with java and I am trying to parse an xml document. The code works in eclipse but when I try to run it on the terminal I get this error. java.lang.ClassNotFoundException: org.jdom2.Document I am using the org.jdom2.Document as a data type which might be the problem. I don't know what can be used in place of it.

public class ReadXMLFile 
{

  public static org.jdom2.Document Read() 
  {

      String fileName = "test_file.xml";
      org.jdom2.Document jdomDoc = new org.jdom2.Document();
      try 
      {

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(new File(fileName));
        DOMBuilder domBuilder = new DOMBuilder();
        jdomDoc = domBuilder.build(doc);
    } 
    catch (Exception e) 
    {

    }

    return jdomDoc;
  }
}

My code example.

2 Answers2

0

ClassNotFoundException is a runtime exception that is thrown when an application tries to load a class at runtime using the Class.forName() or loadClass() or findSystemClass() methods, and the class with specified name are not found in the classpath.

Are you including in the classpath? which command are you using to build your jar file?

You can build your java app including the dependencies like:

<build>
    <plugins>
      <!-- any other plugins -->
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>
  </build>

other suggestions of how to include your dependencies you can find here.

Best Regards,

Renan Souza
  • 71
  • 2
  • 12
  • javac -cp /Users/Fukes14/Desktop/jdom-2/jdom-2.0.6.jar *.java -d . This is the command I used to compile my code. java term.tg53 This is the command I used to run it. Exception in thread "main" java.lang.NoClassDefFoundError: org/jdom2/Document at term.tg53.main(tg53.java:37) Caused by: java.lang.ClassNotFoundException: org.jdom2.Document at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) – fukes14 Sep 28 '18 at 01:12
  • try to put your jar file between quotes, like javac -cp ".:/Users/Fukes14/Desktop/jdom-2/jdom-2.0.6.jar;" *.java -d – Renan Souza Sep 28 '18 at 01:18
  • The java command isn't having any errors. I put the quotes in though and it still worked. my problem is running it. my new error is "Error: Could not find or load main class term.tg53". tg53 is the main class for my program and term is the folder all the classes are in. thank you for the answers I really appreciate it Renan. – fukes14 Sep 28 '18 at 01:25
  • @fukes14, I'm glad to help. Have you tried compiling your app using the IDE? Your project is on Github? – Renan Souza Sep 28 '18 at 01:55
  • I made it in eclipse and it works there. its just when I try to run it in the terminal that I get these errors. – fukes14 Sep 28 '18 at 02:07
  • have you installed maven? If so, try this mvn assembly:assembly -DdescriptorId=jar-with-dependencies and then run your app – Renan Souza Sep 28 '18 at 02:10
  • I have never heard of maven. – fukes14 Sep 28 '18 at 02:21
0

Your code is failing because JDOM2 is not on the classpath when you execute your code.

Some people are suggesting Maven as the solution to this problem. You don't actually need Maven. Maven is useful because it makes it easier to manage your dependencies, but it's another layer of technology to learn about. All you really need is to ensure that the JDOM2 library is on the classpath.

You put JDOM2 on the classpath when you compiled using javac, you also need to put it on the classpath when you run using java, again using

java -cp XXX/jdom-2.0.6.jar myclass
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • java -cp /Users/Fukes14/Desktop/jdom-2/jdom-2.0.6.jar term.tg53 This is the command I run to run my code. I have the class path and I'm getting Error: Could not find or load main class term.tg53 Thank you for the feedback michael – fukes14 Sep 28 '18 at 12:24
  • This time you've got JDOM on the classpath, but your own code isn't on the classpath. They both need to be. – Michael Kay Sep 28 '18 at 13:41
  • can you give me an example class path with my code on it please. – fukes14 Sep 28 '18 at 13:57
  • Use the classpath /Users/Fukes14/Desktop/jdom-2/jdom-2.0.6.jar:ZZZ where ZZZ is the directory that contains the "term" directory that contains the "tg53" class file. The two directory paths are separated by colon on Unix, semicolon on Windows. – Michael Kay Sep 28 '18 at 16:39