1

I run the following command:

C:\Projects\java -cp ./dependency.jar -jar ./dist/main.jar
Exception in thread "main" java.lang.NoClassDefFoundError: MyException
Caused by: java.lang.ClassNotFoundException: MyException
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: Main. Program will exit.

MyException is inside dependency.jar. I also tried to replace -cp with -classpath. What do I do wrong?

Stepan Yakovenko
  • 8,670
  • 28
  • 113
  • 206

2 Answers2

5

The -cp options is ignored if you use -jar. If you need extra jar-files on the class-path, you should specify this in the Manifest of main.jar like this:

Class-Path: dependency.jar

(Then you can run your program using java -jar main.jar.)

Related questions:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • A good alternative is to make a single jar that bundles all dependencies. search for "jar-with-dependencies". – Thilo Jun 20 '14 at 06:45
0

Have you tried specifying the class path in the windows environment variable CLASSPATH ?

specify your environment variable CLASSPATH and put the value of your JAVA_HOME\lib and (path to all your classes) and also include current directory by including (dot or period sign).

pratz
  • 358
  • 1
  • 3
  • 10