eclipse - How to add JAR libraries to WAR project without facing java.lang.ClassNotFoundException? Classpath vs Build Path vs /WEB-INF/lib -
how should add jar libraries war project in eclipse without facing java.lang.classnotfoundexception or java.lang.noclassdeffounderror?
the classpath environment variable not seem work. in cases add jar files build path property of eclipse project make code compile. need put jar files inside /web-inf/lib folder of java ee web application make code run on classes inside jar.
i not understand why classpath not work , in cases should add jars build path , when jars should placed in /web-inf/lib.
the classpath environment variable used java.exe command , when used without of -cp, -classpath, -jar arguments. ignored ides eclipse, netbeans , idea. see java.lang.classnotfoundexception in spite of using classpath environment variable.
the build path libraries required project's code compile. manually placing jar in /web-inf/lib, or setting deployment assembly, or letting external build system maven place <dependency> jar in /web-inf/lib of produced war during build, libraries required code deploy , run on target environment too. note you're not supposed create subfolders in /web-inf/lib. jars have placed in root.
some libraries provided target java ee server or servletcontainer, such jsp, servlet, el, etc. not need put jars of libraries in /web-inf/lib. moreover, cause classloading trouble. it's sufficient (indirectly) specify them in build path only. in eclipse, setting targeted runtime accordingly. automatically end in build path. see how import javax.servlet api in eclipse project?
other libraries, 3rd party ones apache commons, jdbc drivers , standard libraries not provided target servletcontainer (e.g. tomcat doesn't support jsf, cdi, jpa, ejb, etc out box), need end in /web-inf/lib. not need specify in build path. perhaps when have user library, should use deployment assembly setting this. see classnotfoundexception when using user libraries in eclipse build path.
in case you're using maven, need make absolutely sure mark libraries <scope>provided</scope> if provided target runtime, such java ee, servlet, el, etc in case deploy wildfly, tomee, etc. way won't end in /web-inf/lib of produced war (and potentially cause conflicts server-bundled libraries), end in eclipse's build path (and project's code compile).
Comments
Post a Comment