Question

I'm working on a java class assignment. The program is supposed to assist a game of scrabble. It uses the source file Dictionary.txt containing all the official scrabble words and uses it generate an output file, output.txt based on the user's input. When I try to run the tester, I get the NoClassDefFoundError. Can someone help me figure out why, pls?

WordLists Class:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class WordLists
{



ArrayList<String> output;
File inFile;
String word;
Scanner input;

public WordLists(String fileName) throws FileNotFoundException
{
  inFile = new File(fileName);    
  input = new Scanner(inFile);
  output = new ArrayList<String>();
}

public ArrayList<String> lengthN(int n)
{
    while(input.hasNextLine())
    {
        word = input.nextLine();
        if (word.length() == n)
        {
           output.add(word);
           word = input.nextLine(); 
        }

    }
    return output;
}

public ArrayList<String> startsWith(int n, char firstLetter)
{
    while(input.hasNextLine())
    {    
        word = input.nextLine();
        if ((word.length() == n) && (word.charAt(0) == firstLetter))
        {
            output.add(word);
            word = input.nextLine();
        }  

    }    
    return output;
}

public ArrayList<String> containsLetter(int n, char included)
{
    while(input.hasNextLine())
    {
        word = input.nextLine();
        if ((word.length() == n) && (word.contains(String.valueOf(included)))) 
        {
           output.add(word);
           word = input.nextLine(); 
        }    
    }       
    return output;    
}

public ArrayList<String> vowelHeavy(int n, int m)
{
    int vowels = 0;
    while(input.hasNextLine())
    {    
        word = input.nextLine();
        if (word.length() == n)
        {
            for(int i = 0; i < word.length(); i++)
            {
                if(isVowel(word.charAt(i)) == true)
                {
                    vowels++;
                }
            }
            if (vowels == m)
            {
                output.add(word);
            }
        }
        word = input.nextLine();
    }
    return output;
}

public ArrayList<String> multiLetter(int m, char included)
{
     while(input.hasNextLine())
    {
        word = input.nextLine();
        if (word.contains(String.valueOf(included)))
        {
            int occurences = 0;
            for(int i = 0; i < word.length(); i++)
            {
                if( word.charAt(i) == included)
                {
                    occurences++;
                }
            }
            if (occurences == m)
            {
                output.add(word);
            }    
        }
        word = input.nextLine();
    }
    return output; 
}

public boolean isVowel(char c)
{
    if(c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O')     
    {    
        return true;
    }    
    else
    {
        return false;
    }    
}
}   

Tester Class:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

public class Tester
{
public static void main(String[] args)throws FileNotFoundException
{
    WordLists wl;
    Scanner in;
    int n;
    int m;
    char firstLetter;
    char included;

    try
    {
        wl = new WordLists("dictionary.txt");
    }
    catch (FileNotFoundException e)
    {
        System.out.println("file not found");
        return;
    }


    in = new Scanner(System.in);
    String outFile = "output.txt";
    PrintWriter writer = new PrintWriter(outFile);


    System.out.println("Search by length? Enter yes or no");
    String response = in.next();
    if(response.equals("yes"))
    {
        System.out.println("Enter word length");
        n = in.nextInt();
        wl.lengthN(n);
        for(String a : wl.output)
        {
            writer.println(a);
        } 
        writer.close();
    }
    else
    {
        System.out.println("Search by length and first letter?");
        response = in.next();
    }    
    if(response.equals("yes"))
    {
        System.out.println("Enter word length");
        n = in.nextInt();
        System.out.println("Enter the first letter");
        firstLetter = in.nextLine().charAt(0);
        wl.startsWith(n, firstLetter);
        for(String a : wl.output)
        {
            writer.println(a);
        } 
        writer.close();
    }
    else
    {
        System.out.println("Search by length and letter?");
        response = in.next();
    }
    if (response.equals("yes"))
    {
        System.out.println("Enter word length");
        n = in.nextInt();
        System.out.println("Enter the letter");
        included = in.nextLine().charAt(0);
        wl.containsLetter(n, included);
        for(String a : wl.output)
        {
            writer.println(a);
        } 
        writer.close();
    } 
    else
    {
        System.out.println("Search by number of vowels?");
        response = in.next();
    }
    if(response.equals("yes"))
    {
        System.out.println("Enter word length");
        n = in.nextInt();
        System.out.println("Enter the number of vowels");
        m = in.nextInt();
        wl.vowelHeavy(n, m);
        for(String a : wl.output)
        {
            writer.println(a);
        } 
        writer.close();
    }
    else
    {
        System.out.println("Search by number of occurences of a letter?");
        response = in.next();
    }
    if(response.equals("yes"))
    {
        System.out.println("Enter the number of occurences");
        m = in.nextInt();
        System.out.println("Enter the letter");
        included = in.nextLine().charAt(0);
        wl.multiLetter(m, included);
        for(String a : wl.output)
        {
            writer.println(a);
        } 
        writer.close();   
    }
}
}    

Here's the error I get:

Exception in thread "main" java.lang.NoClassDefFoundError: Tester/java
Caused by: java.lang.ClassNotFoundException: Tester.java
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: Tester.java.  Program will exit.
Was it helpful?

Solution

It looks like you forgot to compile your code, or if you're using an IDE you might have configured it to run a class named Tester.java instead of Tester.

Try the following from the directory where you keep WordLists.java and Tester.java:

javac WordLists.java Tester.java
java -cp . Tester
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top