Reputation: 461
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
Reputation: 71
For Mac, using M1, cd
in your folder:
javac -classpath ".:./algs4.jar" HelloWorld.java
java -classpath ".:./algs4.jar" HelloWorld
Upvotes: 4
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
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
type
command to find where is this command iscat
or vim
to see what's the content of this commandYou can see that java-algs4
added -cp
parameter to original java
command just as Olaf Kock said.
Upvotes: 7
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
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
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