简体   繁体   中英

Error while compiling java files and java-servlet files

I am currently new to java and servlets. I am using Apache Tomcat 9.0 as my server and creating the servlets. The error I am getting or the place where I am getting stuck is that when I set CLASSPATH in the system variables as the location of "servlet-api.jar" file the servlet programs compile and run or function properly but not the simple java programs. And the reverse is also true, that is when I remove the CLASSPATH from system variables the servlet programs throw error and the normal java programs compile and run fine. What to do please help.

You can add your Servlet and other third party jar files in classpath while compiling with " javac " command using the " -cp " option. Below command demonstrate adding two jar files in classpath servlet-api.jar and another.jar while compiling MyFirstServlet.java controller.

javac -cp "/path/to/your/servlet-api.jar:/path/to/your/another.jar;" MyFirstServlet.java

With this approach you do not need to set CLASSPATH in the system variables for your third party jar files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM