How 开发者_高级运维come when I have a jar in my project's libraries folder, the compiler keeps screaming NoClassDefinitionError, but when I put it into tomcat's libraries folder, everything works fine. I don't want it to be there, however, because none of my ant tasks work that way.
Add the JAR to the classpath element when you invoke the compiler. Adding the folder doesn't work because the Java compiler doesn't search JAR files for you (it will only search .class files).
If the ant task can not find your jar's you will want to define your classpath to point to them. Here is an example [docs]
<path id="myclasspath">
<fileset dir="${myAppDir}/lib">
<include name="*.jar" />
</fileset>
</path>
And then when you can reference your classpath at any point (using javac as an example)
<javac ... whatever you have>
<classpath refid="myclasspath" />
... anything else you do to compile
</javac>
I would think that if your ant tasks works with the jar's in the Tomcat folder but not your app folder, then you have set the classpath already pointing to Tomcat's lib (probably looking for servlet.jar) folder but failed to include your own. Within the path deceleration above you can have multiple fileset's so you can include various areas in your classpath.
Use the following methods
If you are compiling java files by command line.
javac -classpath \lib\example.jar Test.java
http://download.oracle.com/javase/1.3/docs/tooldocs/win32/javac.html
If you are using one of the IDEs, include the jar as a library. In case of Eclipse:
Right click on Project->Properties->Java Build Path->Libraries-> Add External Jar
It is something similar in Netbeans as well, I don't have the IDE installed to write down the exact steps.
精彩评论