0

Today I tried to compile my sources through the command prompt:

PS ...\JavaDev\Prog> javac -classpath <libs> -d . -sourcepath src src/com/negi/prog/Prog.java

They compiled successfully.

But when I try to run it, it produces an error:

PS ...\JavaDev\Prog> java -classpath  com.negi.prog.Prog

Exception in thread "main" java.lang.NoClassDefFoundError: com/negi/prog/Prog
Caused by: java.lang.ClassNotFoundException: com.negi.prog.Prog

How can I fix that?

arshajii
  • 127,459
  • 24
  • 238
  • 287
Vlad Markushin
  • 443
  • 1
  • 6
  • 24

3 Answers3

2

The classes in your -classpath have to be separated by :

PS ...\JavaDev\Prog> java -classpath "<libs>:com.negi.prog.Prog"

To complete the answer, the different operating systems have different classpath separators. You can check the separator by retrieving the value of the java.class.path property.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • 1
    Actually different operating systems have different classpath seporators thus the `java.class.path` property in [System](http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html). – selig Jun 30 '13 at 20:52
  • Since he's on Windows (judging by use of the back slash instead of forward slash) he needs to use `;`. [Relevant](http://stackoverflow.com/questions/4528438/classpath-does-not-work-under-linux). – Brian Jun 30 '13 at 20:53
  • Correct. But my guess was this is the reason the class is not included in the classpath. – Konstantin Yovkov Jun 30 '13 at 20:53
  • @kocko Absolutely, your answer is correct, just making sure we get complete information. – Brian Jun 30 '13 at 20:54
2

By default . (current path) is included in class path, but if you specify -classpath or -cp, then that is overridden. Include . in your classpath:

java -classpath <libs>:. com.negi.prog.Prog
Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53
1

You need to ensure the current directory is on the classpath when running i.e.

PS ...\JavaDev\Prog> java -classpath <libs>:. com.negi.prog.Prog
selig
  • 4,834
  • 1
  • 20
  • 37