Search code examples
javaclassclassnotfoundexception

Error "java.lang.ClassNotFoundException" stopping me from running the app


Good day,

I have a small issue, that despite finding a couple of solutions online, I wasn't able to apply the proper outcome to my project.

This is my code:

package RandomKeyGenerator;

import java.util.Random;
import java.util.Scanner;

public class start {

    public static void main(String[] args) {
        GenerateKey();
    }

    public static void GenerateKey() {
        Scanner scanner = new Scanner (System.in);
        String key;
        int i, k, j = 0, sub;
        Random random = new Random();
        char[] characters = {'A', 'B', 'C', 'D', 'E'};
        System.out.println("How many keys?");
        int keys = scanner.nextInt();
        do{
            key = "";
            i = 0;
            do{
                k = 0;
                do{
                    sub = random.nextInt(characters.length);
                    k++;
                    key += characters[sub];
                }while (k < 4);
                if (i<3) key += "-";
                i++;
            }while (i < 4);
            System.out.println(key);
            j++;
        }while (j < keys);
        scanner.close();
    }
}

This should generate me some strings, but I receive this error, indicating some paths are not present:

Error: Could not find or load main class RandomKeyGenerator.start
Caused by: java.lang.ClassNotFoundException: RandomKeyGenerator.start

Structure in Eclipse IDE

Looking forward for the solution.


Solution

  • I figured it out. The code itself is correct, but the issue was, that I created this project in an old directory with JRE System Library [JavaSE-1.8]. After creating a completely new directory, now with JRE System Library [JavaSE-12] and again with the option "Build Automatically", I copied exactly the same .java file there and now it works fine!

    Lesson learned. I hope this would be of any help to somebody.