Link to home
Create AccountLog in
Avatar of jdean4
jdean4

asked on

Can't get JSP 5.5.x to use my Java 5.0 JDK (it uses an older JDK, even though I think I deleted the older JDK)

I'm using Tomcat 5.5.12 with Windows XP Professional v. 5.1.
I am using JDK 1.5.0_06 and that's been working fine for all my Java applications for 6 months or so.
Whenever I use JDK 1.5 code in a JSP page, I get a compilation error (e.g., for printf or Scanner). My JSP pages work fine if I use JDK 1.4 code (if I use BufferedReader instead of Scanner for reading from a file, it works fine).

I removed my old JDK 1.4 directories from my hard drive and I've verified that they're removed by looking at the directory structure in Windows Explorer.

I've looked on the Internet for compatibility issues and haven't found any. Here's a site that explains how to use Tomcat 5.x with JDK 5.0:

http://www.murach.com/dloads/jsps/jsps_tomcat-5.5.pdf

My system variables point to the jdk1.5 directory, and the system variables do not point to a jdk1.4 directory.

How can I get my JSP pages to work with jdk1.5.x code?

Aside: I'm new to experts exchange:
1. How do I receive answers?
2. How many points am I allowed overall for my one-month subscription?

Thanks,
John
Avatar of fargo
fargo

Hi,

you need to point the JAVA_HOME variable to the jdk 1.5 path like the following:
JAVA_HOME=C:\java\path

Tomcat uses the JAVA_HOME to compile the jsps etc.
Just set the JAVA_HOME and add the bin directory to path variable. And you are set to go!

fargo
Avatar of jdean4

ASKER

My system variables were already adjusted as mentioned by fargo. In other words, my JAVA_HOME variable already pointed to my jdk1.5 directory and my path variables (one for my personal login and one for my system variable) both already pointed to my jdk1.5/bin directory.

Does anyone have any other suggestions?
What is the output of "java -version" on a command line?

________
radarsh
Avatar of jdean4

ASKER

Radarsh,
Here's what happens when I enter java -version:

C:\Documents and Settings\John Dean>java -version
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)
How have you compiled your application? Do you get this problem with only JSPs or
with even Java Classes?

Do you have code which uses Generics or Varargs features of JDK 1.5 in your JSP page?
I think you have a source version problem.

If you have to make use of those features of JDK 1.5 (such as the varargs in printf) you
have to compile your Java Source files with a source version of 1.5

On a command line, this should be done using "-source 1.5" switch.


________
radarsh
I still want to know for sure if it is a source version error or otherwise.

So, do this simple test in your JSP and let me know what you get.

<%
    int [] array = {1, 2, 3, 4, 5};
    out.println("Array: "+java.util.Arrays.toString(array));
%>

Let me know if you get an output of [1, 2, 3, 4, 5] or a compilation error.

** Please do this befor you try the steps in my previous post.


________
radarsh
Avatar of jdean4

ASKER

Radarsh et al,

Your suggested Arrays.toString code does work fine.
Actually, I've retested my original code with Scanner and it now works fine as well, so I apologize for not thoroughly testing prior to posting my question. My guess is that when I changed my system variables a month ago to accommodate JDK 1.5, I simply rebooted rather than shutdown completely. That's the only thing I can think of that I've done differently this time around. But I'm far from sure about that hypothesis.

But I still can't use JDK 1.5's printf for formatted output. I tried JSP's out object, but apparently printf is not part of out yet. Can I use printf at all with JSP? I realize that I can use other techniques like DecimalFormat, but I really just want to know definitively about printf with JSP. If someone can definitively say "No, you can't use it, but it will be part of a new JSP release," that would be great.

Thanks,
John
Alright. In that case, you have to enable special features for JSP compilation.
Go through the article I posted above and follow the steps.

JDK 1.5 comes with lots of new features like printf, generics, varargs, for in loop, etc.
All these will not even compile if you just change your SDK to 1.5. All you have to do is
change your source version. This source version, by default will be 1.4 even in JDK 1.5.

So, while compiling you have to use the -source 1.5 switch (if done in command line)
For Tomcat, however, you have to follow the steps in that link and proceed.

________
radarsh
Avatar of jdean4

ASKER

As I said above, JDK 1.5 code is now working in my JSP pages.
The problem is now not with JDK 1.5. I just wanted to know if I can mimic printf within JSP.
I can run System.out.printf, but I don't know where the output goes. I was hoping to use JSP's implicit out object and call out.printf, but apparently out does not contain a printf method.

This question is basically a different question than my original question.
My original question resolved itself.
If someone can answer this new question, great, but no big deal if you can't.
ASKER CERTIFIED SOLUTION
Avatar of radarsh
radarsh

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of jdean4

ASKER

My workaround is to use String.format. Here's my new code:
  <%= String.format("$%4.2f", (double) totalCost) %>

Thanks,
John