0

I am enrolled in the Duke University course offered by coursera "Java-Programming-Arrays-Lists-and-Structured-data".

I am using Eclipse instead of using BlueJ for my own ease. But when I try to compile the program I get the following error.

Exception in thread "main" java.lang.NoClassDefFoundError: edu/duke/FileResource

I have imported the jar file edu.duke.* and trying to run the program from the main method. Can someone kindly help how to solve this problem?

import java.lang.*;
import edu.duke.*;

public class WordLengths {
    public void countWordLengths (FileResource resource, int[] counts) {
        
        for(String word:resource.words()) {
            int wordLength = word.length();
            for(int i = 0; i < wordLength; i++) {
                char curChar = word.charAt(i);
                if((i == 0) || (i == wordLength - 1)) {
                    if(!Character.isLetter(curChar)) 
                        wordLength--;
                }
            }
            counts[wordLength]++;
            System.out.println(" Words of length "+ wordLength +" "+ word);
        }
    }
    
    public void indexOfMax (int[] values) {
        
        int maxIndex = 0;
        int position = 0;
        for(int i = 0; i < values.length; i++) {
            if(values[i] > maxIndex) {
                maxIndex = values[i];
                position = i;
            }
        }
        System.out.println("The most common word is :"+ position);
    }
    
    public void testCountWordLengths () {
        
        FileResource f = new FileResource("C:\\Users\\Ramish-HP\\eclipse-workspace\\Assignment1\\smallHamlet.txt");
        int[] counts = new int[31];
        countWordLengths(f, counts);
        indexOfMax(counts);
    }
}


public class WordLengthsTest {

public static void main(String[] args) {
    
    WordLengths wl = new WordLengths();
    wl.testCountWordLengths();
}

}
James Z
  • 12,209
  • 10
  • 24
  • 44
  • You should probably add your code and show us your project structure – Matt Sep 09 '21 at 19:08
  • 1
    Ok I edited, kindly if you can check now – Gooner Coder Sep 09 '21 at 19:12
  • 1
    Make sure you have imported the jar files correctly, have you searched for questions on Stack Oveflow that could solve your problem? For instance, https://stackoverflow.com/questions/3280353/how-to-import-a-jar-in-eclipse It is not enough to just add the import statement when you are using external jars. – Matt Sep 09 '21 at 20:21
  • 1
    Import is not the problem; making sure that the JAR is on the CLASSPATH and visible to the JVM is the important thing. – duffymo Sep 09 '21 at 21:05
  • Does this answer your question? [Why am I getting a NoClassDefFoundError in Java?](https://stackoverflow.com/questions/34413/why-am-i-getting-a-noclassdeffounderror-in-java) – Danubian Sailor May 27 '22 at 12:23

1 Answers1

0

java.lang.NoClassDefFoundError commonly thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found. The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/NoClassDefFoundError.html

I will suggest to check run time libraries.

Rahul Rai
  • 1
  • 1