Tuesday, September 4, 2012

Set Java PATH and CLASSPATH when using XAMPP.


This should work for compiling your servlets with XAMPP's Tomcat from a command prompt in Windows 7 (I'm using XAMPP version 1.8.0).

1) Right click "Computer" and select "Properties".
2) On the left panel click "Advanced system settings".
3) Click on "Environment Variables".
4) In "System variables" click on "New".
5) In "Variable name" type "CLASSPATH" and...

In the next step you need to know the path in your computer to the "\xampp\tomcat\lib\servlet-api.jar" file, also for the \Java\jdk1.7.0_07\lib folder and remember to add a PERIOD as the first variable, all variables are separated by a semicolon.

I'm giving an example of where the paths are in my computer:

6) In "Variable value" enter this path: .;C:\xampp\tomcat\lib\servlet-api.jar;C:\Program Files\Java\jdk1.7.0_07\lib

7) In "System variables" search for "Path" and click on "Edit" if the variable doesn't exists just create a new "PATH" one.

In the next step you also need to know the path to the \Java\jdk1.7.0_07\bin folder in your computer, I'm adding an example of where it is located in my computer, the location may vary depending on where you installed the JDK.

8) In "Variable value" type a semicolon to the end of the existing variables and enter this path: C:\Program Files\Java\jdk1.7.0_07\bin

To test the java compiler simply open a command prompt and type "javac". To compile your .java source files just type "javac YourFileName.java" for example.

You may alternatively add the paths to your "User variables" instead.

Remember that to run your classes in Tomcat they should be inside of a WEB-INF/classes folder and to configure the WEB-INF/web.xml file in a manner similar to this (assuming your compiled file is named "YourFileName.class"):

<servlet>
    <servlet-name>
YourFileName</servlet-name>
    <servlet-class>
YourFileName</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>
YourFileName</servlet-name>
    <url-pattern>/
YourFileName</url-pattern>
</servlet-mapping>

1 comment:

Anonymous said...

life saver... only one on the internet that had a solution after 30min of messing with it. THANKS!