• Post Reply Bookmark Topic Watch Topic
  • New Topic

I cannot run a java program that is using an external library from command line  RSS feed

 
Nick Petas
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys.
I am reading the excellent book Algorithms.
The author of this book is using his own libraries.
I have downloaded the libraries (it is a file called stdlib.jar) and I've store it in a directory called ~/Downloads(I am using a macbook pro).
Then I have created a project with IntelliJ Idea 14.1 using the default package (as it is the only way for a program to 'see' these libraries).
The program is running inside the ide, but because of the nature of this book, all the included code must be run from the command line.
The problem is that I cannot run for example the Average class from the command line because the system asks for the external library.
Specifically the error is :

I enter : java -cp . Average

and the Error I get is :

Exception in thread "main" java.lang.NoClassDefFoundError: StdIn
at Average.main(Average.java:11)
Caused by: java.lang.ClassNotFoundException: StdIn
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more

The Average class is the following :



 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think the command you need is

java -cp ~/Downloads Average
 
Nick Petas
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just found the solution.
The problem is that at the 90% of the web pages they forget to add .: before the path of the jar..
Here is the correct solution :

java -cp .:/Users/Administrator/Downloads/stdlib.jar Average
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!