String.format()error (works in a class file , not in jsp)
Hello all,
I am using Java 5 and am trying to use the String.format() method in a jsp but am getting the following error:
HTTP Status 500 -
--
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 5 in the jsp file: /ajax.jsp
Generated servlet error:
C:\Documents and Settings\gforte\.netbeans\5.0beta2\jakarta-tomcat-5.5.9_base\work\Catalina\localhost\agi\org\apache\jsp\ajax_jsp.java:50: cannot find symbol
symbol : method format(java.lang.String,java.util.Date,java.util.Date)
location: class java.lang.String
String dateString = String.format("%tb%td",date,date);
^
1 error
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
org.apache.jasper.compiler.AntCompiler.generateClass(AntCompiler.java:246)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.
--
Apache Tomcat/5.5.9
The code I am using is working fine in a class file but wont work in my jsp.
<%@page import="java.util.*,java.lang.String"%>
Date date =new Date();
String dateString = String.format("%tb%td",date,date);
System.out.println(dateString);
Any ideas?
[2321 byte] By [
gfortya] at [2007-10-2 10:57:35]

I get excactly the same problem.
If compile the the JSP in Netbeans it works fine, however when Tomcat attempts to compile it into a servlet at runtime, I get the same error as described.
This is the line in JSP that fails:
<code>Some more text <%= String.format("%s","Bizarre") %> </code>
However this one works OK:
<code>Some more text <%= "Bizarre".substring(3) %> </code>
Its almost as if Tomcat is compiling under JDK 1.4.2 or something but I don't have any other JDK or JRE installed; only 1.5.
I suspect it may be a Tomcat setting that's causing a 1.4 code compliance.
In Netbeans, if I go to the project properties -> Sources, at the bottom is the "Source Level" drop down list. If I change this to 1.4 and clean and build the whole project I get the same error when the jsp (or any class with this construct) is compiled by NetBeans.
Note: I am using the Netbeans 5.0 and Tomcat and 5.5.9 co-bundle which I downloded from the Sun website.
Can anyone help?
Heres the splurge from the browser output:
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 55 in the jsp file: /login.jsp
Generated servlet error:
C:\Documents and Settings\Peter\.netbeans\5.0\jakarta-tomcat-5.5.9_base\work\Catalina\localhost\SuperStruts\org\apache\jsp\login_jsp.java:105: cannot find symbol
symbol : method format(java.lang.String,java.lang.String)
location: class java.lang.String
out.print( String.format("%s","Bizarre") );
^
1 error
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
org.apache.jasper.compiler.AntCompiler.generateClass(AntCompiler.java:246)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
Note also that when I run this JSP page:
<%@page import="java.util.*"%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%
Properties p = System.getProperties();
Enumeration keys = p.keys();
Object key;
while (keys.hasMoreElements()) {
key = keys.nextElement();
out.println("key: " + key + " - " + p.getProperty((String)key) + "<br/>");
}
%>
... I get this browser output:
key: java.runtime.name - Java(TM) 2 Runtime Environment, Standard Edition
key: sun.boot.library.path - C:\Program Files\Java\jdk1.5.0_06\jre\bin
key: java.vm.version - 1.5.0_06-b05
key: shared.loader - ${catalina.base}/shared/classes,${catalina.base}/shared/lib/*.jar
key: java.vm.vendor - Sun Microsystems Inc.
key: java.vendor.url - http://java.sun.com/
key: path.separator - ;
key: tomcat.util.buf.StringCache.byte.enabled - true
key: java.vm.name - Java HotSpot(TM) Client VM
key: file.encoding.pkg - sun.io
key: user.country - GB
key: sun.os.patch.level - Service Pack 2
key: java.vm.specification.name - Java Virtual Machine Specification
key: user.dir - C:\Program Files\netbeans-5.0\enterprise2\jakarta-tomcat-5.5.9\bin
key: java.runtime.version - 1.5.0_06-b05
key: java.awt.graphicsenv - sun.awt.Win32GraphicsEnvironment
key: java.endorsed.dirs -
key: os.arch - x86
key: java.io.tmpdir - C:\Documents and Settings\Peter\.netbeans\5.0\jakarta-tomcat-5.5.9_base\temp
key: line.separator -
key: java.vm.specification.vendor - Sun Microsystems Inc.
key: java.naming.factory.url.pkgs - org.apache.naming
key: java.util.logging.manager - org.apache.juli.ClassLoaderLogManager
key: user.variant -
key: os.name - Windows XP
key: sun.jnu.encoding - Cp1252
key: java.library.path - C:\Program Files\Java\jdk1.5.0_06\bin;.;C:\WINDOWS\system32;C:\WINDOWS;C:\oracle\product\10.2.0\db_2\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\ATI Technologies\ATI.ACE\;C:\Program Files\MySQL\MySQL Server 5.0\bin;C:\Program Files\Common Files\GTK\2.0\bin;C:\Program Files\CVSNT\
key: java.specification.name - Java Platform API Specification
key: java.class.version - 49.0
key: sun.management.compiler - HotSpot Client Compiler
key: os.version - 5.1
key: user.home - C:\Documents and Settings\Peter
key: catalina.useNaming - true
key: user.timezone - Europe/London
key: java.awt.printerjob - sun.awt.windows.WPrinterJob
key: file.encoding - Cp1252
key: java.specification.version - 1.5
key: catalina.home - C:\Program Files\netbeans-5.0\enterprise2\jakarta-tomcat-5.5.9
key: java.class.path - C:\Program Files\Java\jdk1.5.0_06\lib\tools.jar;C:\Program Files\netbeans-5.0\enterprise2\jakarta-tomcat-5.5.9\bin\bootstrap.jar
key: user.name - Peter
key: java.naming.factory.initial - org.apache.naming.java.javaURLContextFactory
key: package.definition - sun.,java.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.
key: java.vm.specification.version - 1.0
key: java.home - C:\Program Files\Java\jdk1.5.0_06\jre
key: sun.arch.data.model - 32
key: user.language - en
key: java.specification.vendor - Sun Microsystems Inc.
key: awt.toolkit - sun.awt.windows.WToolkit
key: java.vm.info - mixed mode
key: java.version - 1.5.0_06
key: java.ext.dirs - C:\Program Files\Java\jdk1.5.0_06\jre\lib\ext
key: sun.boot.class.path - C:\Program Files\Java\jdk1.5.0_06\jre\lib\rt.jar;C:\Program Files\Java\jdk1.5.0_06\jre\lib\i18n.jar;C:\Program Files\Java\jdk1.5.0_06\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.5.0_06\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.5.0_06\jre\lib\jce.jar;C:\Program Files\Java\jdk1.5.0_06\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.5.0_06\jre\classes
key: server.loader - ${catalina.home}/server/classes,${catalina.home}/server/lib/*.jar
key: java.vendor - Sun Microsystems Inc.
key: catalina.base - C:\Documents and Settings\Peter\.netbeans\5.0\jakarta-tomcat-5.5.9_base
key: file.separator - \
key: java.vendor.url.bug - http://java.sun.com/cgi-bin/bugreport.cgi
key: common.loader - ${catalina.home}/common/classes,${catalina.home}/common/i18n/*.jar,${catalina.home}/common/lib/*.jar
key: sun.io.unicode.encoding - UnicodeLittle
key: sun.cpu.endian - little
key: package.access - sun.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,org.apache.jasper.,sun.beans.
key: sun.desktop - windows
key: sun.cpu.isalist -
demonstrating (I think) that the correct JDK and JDK are in use by Tomcat.