gaoxinge
gaoxinge

Reputation: 461

java error: Exception in thread "main" java.lang.NoClassDefFoundError

I am a beginner in java and taking the course Algorithm, which is provided by Princeton. In the course, professor asked us to download algs4.jar to a folder and add algs4.jar to the classpath.[1]

I followed it step by step, and try to program a HelloWorld like

import edu.princeton.cs.algs4.StdOut;

public class HelloWorld {
     public static void main(String args[]) {
     StdOut.print("Hello World!");
}
} 

However when I compile the file, console reminds me that

NPP_EXEC: "java_Compile_Run"
NPP_SAVE: G:\java\helloworld\HelloWorld.java
javac -encoding UTF-8 "G:\java\helloworld\HelloWorld.java"
Process started >>>
<<< Process finished. (Exit code 0)

==========编译成功后开始运行==========
java -cp "G:\java\helloworld" "HelloWorld"
Process started >>>
Exception in thread "main" java.lang.NoClassDefFoundError:
edu/princeton/cs/algs4/StdOut
at HelloWorld.main(HelloWorld.java:5)
Caused by: java.lang.ClassNotFoundException: edu.princeton.cs.algs4.StdOut
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)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
<<< Process finished. (Exit code 1)
================ READY ================ 

I have checked my classpath, and my programming file. What does this error mean? And how can I fix it?

Any advice is helpful. Thank you.


[1] http://algs4.cs.princeton.edu/code/

Upvotes: 5

Views: 6409

Answers (6)

Ina Tiganas
Ina Tiganas

Reputation: 71

For Mac, using M1, cd in your folder:

javac -classpath ".:./algs4.jar" HelloWorld.java

java -classpath ".:./algs4.jar" HelloWorld

Upvotes: 4

kchak
kchak

Reputation: 8062

I just included a classpath argument in both javac and java commands like so:

javac -classpath ".;drive\path\to\algs4.jar" Hello.java

and

java -classpath ".;drive\path\to\algs4.jar" Hello

Also if you are manually adding the CLASSPATH environment variable, then remember to close and restart the cmd console.

Upvotes: 3

Buqian Zheng
Buqian Zheng

Reputation: 341

According to the instructions of this course you should use javac-algs4 to compile and java-algs4 to execute if you imported their library.

If you want to know the differences between these two commands and original commands javac and java, you could

  1. use type command to find where is this command is
  2. use cat or vim to see what's the content of this command

You can see that java-algs4 added -cp parameter to original java command just as Olaf Kock said.

Upvotes: 7

Eric Zhang
Eric Zhang

Reputation: 35

You imported class StdOut in your java code (import edu.princeton.cs.algs4.StdOut;), you have to tell java how stdOut implement

Accroding to the link you provided (http://algs4.cs.princeton.edu/code/). You have to follow the "Installing the textbook libraries." section to install this lib first.

Upvotes: 1

aliasm2k
aliasm2k

Reputation: 901

You'll have to probably use -cp flag to set the class path to include the package.

While using the -cp flag, don't forget to include the current working directory using .

So, something like javac -cp thejar.jar:. should work in linux or javac -cp thejar.jar;. should work for windows

Upvotes: 5

Olaf Kock
Olaf Kock

Reputation: 48057

If you're referring to a jar file that should be on the classpath, you must name it explicitly. E.g.

java -cp "G:/java/helloworld;G:/whereever/algs4j.jar" HelloWorld

Do they really provide another name for System.out? In this case you can also safely ignore that jar by using System.out instead of StdOut

Upvotes: 7

Related Questions