Monday, 3 August 2009

JSP compile error in Eclipse

One of the most common problem when using Eclipse to develop web pages (JSPs) is the default JRE settings. By default Eclipse sets the default system JRE as its JRE. This sounds all good, but JSPs require a JDK instead of a JRE - this sounds weird. This article shows you how to solve this very small issue by changing the Eclipse Installed JREs settings.

To avoid confusion in this article, I'm going to use italics to refer to the JRE Eclipse name (nothing to do with Java unless otherwise told), that is, the name Eclipse uses and has nothing to do with the Java JRE. Also, JDK means Java Development Kit and JRE (not in italics) means Java Runtime Environment.

Set Eclipse Installed JREs

Check your JDK

Before you proceed, make sure that you have a JDK installed. You can do so by executing the following command on your command prompt.


C:\>javac -version
javac 1.6.0_02

In my case I have a copy of Java 1.6 update 2 installed. Your version may be different from the one displayed above, depending on the version you have installed. As long as the above command returns something (similar to the above) it's OK. If the above returns an error similar to following, it means that either you don't have a JDK installed or the JDK bin directory is not in your system path.


C:\>javac -version
'javac' is not recognized as an internal or external command, operable program or batch file.

In this case, browse to the folder where you think Java is installed (on Windows this is usually: C:\Program Files\Java or C:\Program Files (86)\Java in case of 64 bit Windows version) and check it yourself. Alternatively, visit Sun's website (http://java.sun.com) and download and install the JDK from there. Make sure to download the JDK and not the JRE.

Set the Installed JREs

Open Eclipse if it is not already running. Open the Preferences from the Windows menu as shown below.

Expand the Java node and select the Installed JREs sub-node.

JSPs require that Eclipse default Installed JRE is actually pointing to a JDK and not a JRE. Thus if your default Installed JRE is pointing to a JRE instead, you need to replace it with your JDK. This may sound weird since the dialog is asking you for a JRE.

The process is very simple:

  1. First add a new JRE by clicking the Add button. You cannot just remove your only JRE without first adding another JRE.
  2. Select Standard VM option from the list and click Next.
  3. Click the Directory button or manually enter the path to your (latest or which one you require) JDK directory on your local machine. Note that the JDK directory is the folder where JDK was installed. See the next screen shot.
  4. Wait for Eclipse to verify your entry and list the libraries.
  5. Optionally, you can change the JRE name to a name of your liking.
  6. Click Finish to complete the process.
  7. Set the new JRE (that is, the JDK just added) as your default JRE by checking the checkbox near the JRE friendly name (usually the first column).
  8. Optionally, you can remove the other JRE by first selecting it (not setting it as your default) and pressing the Remove button.
  9. Click OK to close the preferences dialog.

With the default JRE set to a JDK, test your JSPs and confirm that the errors are now gone and the JSPs are now compiling.

32 comments:

  1. Worked perfectly, once I figured out that the install for jdk 1.6_16 was referencing the C:\Program Files\Java\jdk1.6.0_16 and not the install directory I had told the sun jdk installer to work in, which was C:\java\jdk_1_6_16. That was just the jre.

    Hope this helped.

    ReplyDelete
  2. Thnx for the post Albert.
    You have helped me a lot....

    ReplyDelete
  3. Thanks for this! I was stuck because my jsps wouldn't compile but your post helped me resolve the error.

    ReplyDelete
  4. Thanks a lot, you saved my day (or actually my night).

    Was stuck trying out a jsp tutorial and was getting very frustrated.

    You have my heartfelt thanks :)

    ReplyDelete
  5. Very helpful information. Thank you.

    ReplyDelete
  6. very helpful thanks a lot....

    ReplyDelete
  7. Thank you!!Thank you!!Thank you!!

    ReplyDelete
  8. Very helpful. You saved me a ton of time.

    ReplyDelete
  9. thanks dude. java is screwed up sometimes. they know this..why dont they handle it?

    ReplyDelete
  10. Hi Albert Attard !

    Thanks for your information, i have resolved my issue in eclispe.

    Keep on growing ....


    ---------

    Thangavel L Nathan...

    ReplyDelete
  11. Thanks for the detailed post. This was a tailor-made solution.

    ReplyDelete
  12. Thank you big time

    ReplyDelete
  13. Many thanks for the solution and clear explanation!

    ReplyDelete
  14. Absolutely heartfelt thanks! You saved my teams hours of work and refactoring!

    ReplyDelete
  15. very helpful, however in my case I had to do something else:
    preferences->java->installed jres->execution environments
    there I had to select the jdk. then it worked perfectly!

    ReplyDelete
  16. Very helpful. Thanks!

    ReplyDelete
  17. Thank you!!!!!!! I could not do it without this page!!! :)

    ReplyDelete
  18. I thot I knew what to do, but could do it only after reading your post.

    ReplyDelete
  19. plz give the exceat link for jdk version i cant identified.

    ReplyDelete
  20. Do you have a JDK installed?

    You can download the latest version from: http://www.oracle.com/technetwork/java/javase/downloads/java-se-jdk-7-download-432154.html.

    ReplyDelete
  21. Netbeans vet new to Eclipse. Worked like a charm! Thanks.

    ReplyDelete
  22. great post, thanks

    ReplyDelete
  23. thanks very much , i've been messed up this error since a whole day !

    ReplyDelete
  24. It works only for me, if i do a full rebuild, what should i do?

    ReplyDelete
  25. Now when using JDK1.7 I get the error as below. Could not even run it locally as prior to this update I could do that with no problem.
    WARNING: EXCEPTION
    java.lang.ClassNotFoundException: guestbook.GuestbookServlet

    ReplyDelete