Problem creating ArrayList<String>

Hi,

I'm new to JSP, so I'm trying to do some basic things, mainly following a JSP tutorial I found online. The tutorial outputted a few numbers to a table, so I decided to try to have it output the values of an ArrayList<String>.

In my file, index.jsp, my code looks like this:

<%@ page import="java.util.*"%>

<%

ArrayList<String> al =new ArrayList<String>();

al.add("Row 1");

al.add("Row 2");

al.add("Row 3");

al.add("Row 4");

%>

<TABLE BORDER=2>

<%

for (int i = 0; i < al.length; i++ ){

%>

<TR>

<TD>Number</TD>

<TD><%= al[i] %></TD>

</TR>

<%

}

%>

</TABLE>

When I deploy my page onto my webserver (Tomcat 4.0), I get the following error:

org.apache.jasper.JasperException: Unable to compileclassfor JSPNote: sun.tools.javac.Main has been deprecated.

An error occurred between lines: 18 and 24 in the jsp file: /index.jsp

Generated servlet error:

/var/apache/tomcat/work/Standalone/localhost/_/index$jsp.java:85:'(' expected.

ArrayList<String> al =new ArrayList<String>();

^

Forgive me if I missed something obvious, but I'm not sure what the problem is. Where am I missing a parenthesis? What is it talking about? I know the code for creating the ArrayList is correct in Java, so I'm confused as to what the problem is.

If anyone can provide some advice, I'd be appreciative.

Thanks,

Dan

[2029 byte] By [Djaunla] at [2007-11-26 17:59:01]
# 1

Try this, it should work:

<%@ page import="java.util.ArrayList" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head><title></title></head>

<body>

<%

ArrayList<String> al = new ArrayList<String>();

al.add("Row 1");

al.add("Row 2");

al.add("Row 3");

al.add("Row 4");

%>

<TABLE BORDER=2>

<%

for (int i = 0; i < al.size(); i++) {

%>

<TR>

<TD>Number</TD>

<TD><%=al.get(i)%></TD>

</TR>

<%

}

%>

</TABLE>

</body>

</html>

appy77a at 2007-7-9 5:24:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Also what version of JDK are you using?

In your code you are using generics for example:

ArrayList<String>

Generics are only supported in JDK 1.4 or lower, if you want to use generics in your code download a JDK version greater than 1.4.x or simply download the most recent stable version.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I'd like to mention a few things to help you understand the concepts better: An ArrayList is itself an object and it holds other objects, but an array of primitive types is a little different.

With an array of primitive types one can access individual elements with arrayName[index] , but this is not possible with ArrayList.

Here's the details on ArrayList :

http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Once you get familiar with JSP I recommend learning JSTL, because right now the JSP contains scriptlets - which lead to messy and unmanageable code once your application grows.

But since you are new to JSPs it's fine to use scriptlets to get familiar with the concepts.

Message was edited by:

appy77

appy77a at 2007-7-9 5:24:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Hi,

Thanks for the reply; I'm sorry it took me awhile to respond.

I modified my code in my index.jsp file to look like the code provided. However, it still is not working, and I'm getting the same error on the code where my ArrayList resides.

I am running JDK 1.5.0_06-b05.

Also, thanks for the explanation on generics. I am aware of the difference between an Array and an ArrayList. I simply used ArrayList here because it is used predominantly in an application I hope to deploy on my server in the future.

I'll definitely look into JSTL once I get a better understanding of JSP.

Anyway, I don't know what could be causing the problem. My JDK is up-to-date for the code I'm using, and the actual code looks fine. My code is exactly this:

<!doctype html public "-//w3c//dtd html 4.0 transitional//en" "http://www.w3.org/TR/REC-html40/strict.dtd">

<%@ page import="java.util.ArrayList" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head><title></title></head>

<body>

<%

ArrayList<String> al = new ArrayList<String>();

al.add("Row 1");

al.add("Row 2");

al.add("Row 3");

al.add("Row 4");

%>

<TABLE BORDER=2>

<%

for (int i = 0; i < al.size(); i++) {

%>

<TR>

<TD>Number</TD>

<TD><%=al.get(i)%></TD>

</TR>

<%

}

%>

</TABLE>

</body>

</html>

Maybe Tomcat 4.0 does not support JDK1.5 for some reason? I looked on the website and it said it supported anything above JDK1.2, but maybe I read it wrong.

Is there anything else I could do that would be of help?

Thanks,

Dan

Djaunla at 2007-7-9 5:24:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

The new code you posted worked on my machine with Tomcat 5.5 and JDK 1.5

I'm not sure if Tomcat 4.x supports JDK 1.5, but if you have questions and doubts on any version of Tomcat subscribe to the Tomcat User Mailing list , and post a new thread there http://tomcat.apache.org/lists.html

I'm guessing that Tomcat 4.x does support it, on your command prompt type java -version , and check if there's another conflicting JDK sometimes, some systems have more than 1 JDK versions installed and that can cause conflict.

Also your System Environment variable should be set to:

PATH

%JAVA_HOME%\bin;

CATALINA_HOME

C:\dev\apache-tomcat-5.5.12 - (or in your case 1.4 , and your tomcat's folder)

CLASSPATH

.;..;(one dot semicolon two dots semicolon)

JAVA_HOME

C:\dev\jdk (whereever you installed your JDK)

appy77a at 2007-7-9 5:24:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Hi, thanks for the reply.

My PATH, CATALINA_HOME, and JAVA_HOME variables are all set properly. My JAVA_HOME was not set properly before and I fixed it, but I'm still having the problem.

I don't understand what you're doing with the CLASSPATH variable. I've searched the web a bit, and I've found that my Tomcat classpath needs to be set. What is ".;..;"?

Unfortunately, I'm developing my applications on Windows, but Tomcat is installed on UNIX. Obviously ".;..;" is not the same on UNIX, so if you can tell me what that variable is supposed to show, I can emulate it properly on UNIX.

If my java -version displays:

Java(TM) Runtime Environment, Standard Edition (build 1.5.0_06-b05)

Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

is that normal?

Furthermore, simple code I had before worked fine in my index.jsp file. I only started getting the errors once I added the ArrayList.

Would I need to restart Tomcat? If so, I can't find the bin directory. That sounds like it may be a problem. Maybe this topic would best be covered on the Tomcat-Users list, as it seems as if it's getting into Tomcat.

Djaunla at 2007-7-9 5:24:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Tomcat looks for JAVA_HOME to locate the JDK , so setting it is required after you set JAVA_HOME you need to re-start Tomcat so that it can now start refering to the JDK mentioned in JAVA_HOME

In Windows . means current directorya and .. the previous directory, I think classpath is set that way so that Java files can be run from anywhere regardless of the directory's location. I'm not sure if that is required, but it's on my system CLASSPATH thats' why I mentioned it to you.

Ah, on Unix what version of JDK is being used by the Tomcat. if you check the equivalent of java -version on Unix where your Tomcat is installed , look for a different version of JDK.

I think it would be better if you could get a local copy of Tomcat on your Windows system, rather than using a remote one insalled on Unix.

The Java version you have is normal, I'm assuming that's the one you have on Windows.

You can also test your code on a new JSP page with other new features from JDK 1.5 , for example the short-form for loop , or static imports etc, see if those work. If those work then the problem might be something else too.

You can always check with the Tomcat User list - if you want you can keep this thread going simultaneously no problem with that.

On the Tomcat user list most people know Tomcat very well, so they'll be able to help you better.

appy77a at 2007-7-9 5:24:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

I posted a question on the Tomcat-Users mailing list for additional advice.

I believe the fact that I cannot find my tomcat /bin directory (therefore I cannot restart Tomcat) is causing the main problem. My JAVA_HOME env variable is not set correctly until I set it to the JDK, and if I cannot restart Tomcat, it obviously cannot recognize the proper path to the JDK.

The version of Java I posted previously is the version I am running on UNIX.

Unfortunately, I cannot get a version of Tomcat installed on Windows, although it would be quite helpful.

I'll test some other 1.5 code and see what happens.

Thanks a lot for your continued advice.

Djaunla at 2007-7-9 5:24:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

Hi,

This thread is rather old, but I've found a solution to the problem, so I decided I would share it in case someone in the future is stuck like I am. It took awhile to find a solution (I finally solved it yesterday), so I will summarize.

Tomcat 4.0.5, the version I was using, had an outdated compiler; it could only compile up to JDK1.4. Also, according to some replies on the Tomcat-Users list, some of the errors I was getting were very unusual, so I was recommended to upgrade.

I upgraded to an existing build of Tomcat 5.5.9 we had, but that too had an outdated compiler, and could only compile up to JDK1.4.

I then upgraded to Tomcat 6.0.9, the newest release, and it worked fine. The compiler is compliant with JDK1.5.

Basically, if you are getting compiler errors, either:

1) Upgrade Tomcat to the newest version (RECOMMENDED)

2) Google for "compilerSourceVM". This will help you upgrade your compiler to support JDK1.5. This thread here on the Java forums is also pertinent: http://forum.java.sun.com/thread.jspa?threadID=579806&messageID=2928513

I tried option two and it did not work for me, which is why I recommend option 1.

I hope this helps someone eventually.

Djaunla at 2007-7-9 5:24:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
Thanks for posting the solution.
appy77a at 2007-7-9 5:24:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...