'Exception in thread "main" java.lang.NoClassDefFoundError - Coursera Princeton Algorithm Course

I can see that this question has been asked before, but I have not found previous answers useful - specifically my issue is following Princeton University's 'Algorithms' course on Coursera here and using their algs4.jar file to access ostensibly necessary utilities. I have never used Java before but this seemed like such a good course, I'm trying to muddle through rather than switching to a 'worse' course in a language I know. I have also found a thread specifically on this issue here and a Reddit question here, which were equally as useless as the other questions which had this problem, but not with the Princeton's algs4.jar file.

This is my code, I haven't bothered to finish it but it should run. Before anybody asks, I've commented out everything except the first if (StdIn.isEmpty()) check and it still gives me all the same compiler/interpreter errors, so this is nothing to do with it being half-finished - StdIn.isEmpty() and StdIn.readString() are supposed to be provided by algs4.jar:

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;

class RandomWord
{
    public static void main(String[] args)
    {
        if (StdIn.isEmpty())
        {
            System.out.println("Nothing to read.");
            return;
        }

        String curr_selected_word;
        double curr_probability = 0.0;
        int i = 0;

        while (!StdIn.isEmpty())
        {
            String current_string = StdIn.readString();
            System.out.println("> " + current_string);
        }
    }
}

Following the advice of previous threads, I compile like this:

javac -cp "./algs4.jar" RandomWord.java

Which works fine. Running java RandomWord, I get this large messy error:

Exception in thread "main" java.lang.NoClassDefFoundError: edu/princeton/cs/algs4/StdIn
        at RandomWord.main(RandomWord.java:13)
Caused by: java.lang.ClassNotFoundException: edu.princeton.cs.algs4.StdIn
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        ... 1 more

The accepted answer on the other question was to reference the .jar file explicitly, like this:

java -cp "C:\Coursera\Algorithms\Part 1\Java Solutions\HelloWorld;C:\Coursera\Algorithms\Part 1\Java Solutions\algs4.jar" RandomWord

Which gives me the exact same error. There is also a comment on the Reddit thread that uses the -classpath instead of -cp flag, which at least gives a different but stranger error:

>java -classpath ./algs4.jar RandomWord
Error: Could not find or load main class RandomWord
Caused by: java.lang.ClassNotFoundException: RandomWord

Putting quotes around the .jar filepath, or providing the absolute filepath, all produce the same error. I Googled this and predictably it is caused by not having a main entry-point, but... see code above?

Lastly, the installer for the Coursera course apparently installs some Bash commands (see here) called javac-algs4 and java-algs4 which is supposed to sort out all this classpath nonsense for me. So I crack open Bash, and:

>javac-algs4 RandomWord.java
'javac-algs4' is not recognized as an internal or external command,
operable program or batch file.

I have restarted since running the installer. I would add these to my PATH manually, but I don't even know where they are; the C:\Program Files\LIFT-CS folder that their installer installs into only contains a pissing uninstaller. So I'm absolutely losing my rag and at my wits' end after spending over 7 hours just trying to start this course, not even getting stuck on the problems or the content. When will content creators learn we don't want your in-house IDE with no dark mode and libraries that do nothing but rename every function to camel-case?

Anyway, if anyone has encountered this or knows what I could do to fix it, help would be appreciated.



Solution 1:[1]

You can try this - javac -cp .;<insert class path> RandomWord

I was facing the exact same issue, I ran javac -cp .;.lift/algs4.jar RandomWord and it works. (My compiled class was in the same directory as .lift)

Solution 2:[2]

This worked for me on a Mac. Copy algs4.jar to the root of your project, then:

javac -cp ".:./algs4.jar" RandomWord.java 
java -cp ".:./algs4.jar" RandomWord

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PyWalker2797
Solution 2 jbnunn