0

I have a class named as Constants.java. I get a java.lang.NoClassDefFoundError for this class. I know this is in my classpath as I can access other files from same package. I found that In Java Lib there are many classes with Same name Constants.java. Is this name overlap causing some issue ? If Yes How do I resolve it ?

Aditya
  • 1,033
  • 3
  • 11
  • 24
  • You could have multiple classes with the same name as long as they are in different namespaces – matcheek Jun 23 '15 at 06:54
  • If they have different package names, they shouldn't collide. But this happens at runtime, not at compile time, correct? Is it possible that the .class file was deleted? – Luke Jun 23 '15 at 06:54
  • Yes they are all in different packages. But for this class I am getting error. And class is simple public class Constants { } with some static final constants, nothing else. – Aditya Jun 23 '15 at 06:56
  • You should use interface for constants. This is not relevant to the question but as a standard way of declaring constants. – Nakul91 Jun 23 '15 at 06:57
  • @Luke No the .class file is present. I Use Tomcat. I can see the .class file in webapps folder under same package as I defined. – Aditya Jun 23 '15 at 06:58
  • Classes are known to ClassLoader by their canonical name (package+classname). Even if you have two classes with the same canonical name class loader will load one of them so there will be some problem with calling methods of the other one. So as long as your class canonical name is unique you are good to go. – A.v Jun 23 '15 at 06:58
  • Possible duplicate of this: http://stackoverflow.com/questions/15804872/noclassdeffounderror-in-web-application-running-on-tomcat-7 – Luke Jun 23 '15 at 06:59
  • If your Constants.java is in your custom defined package (for example com.yourname.Constants.java) there isn't any way to collide with JRE libraries. Is your Constants.java in a library which you added to your project or not? – STaefi Jun 23 '15 at 07:00
  • @Nakul91 no, actually, that's a recognized anti-pattern. – JB Nizet Jun 23 '15 at 07:01
  • Post the complete stack trace of the exception. Tell us where the class is located (which jar file, where the jar file itself is) – JB Nizet Jun 23 '15 at 07:02

0 Answers0