Problems compiling servlets with JDK 5/Netbeans 4.0

Hello,

I've been using Sun 1 Studio CE, and more recently, Netbeans 3.6, with J2SE 1.4.2. I have some basic swing applets and servlets that I wrote for my business' web site.

I recently downloaded and installed the new software bundle containing Netbeans 4.0 and J2SE 1.5. When I try to compile my servlets, I get a compiler error that packagesjavax.servlet.* andjavax.servlet.http.* do not exist.

I reinstalled J2SE 1.4.2 and Netbeans 3.6 and the servlets compile fine.

Why is JDK 5 not able to find these packages? My understanding is that they're part of the Java Servlet API, which I never explicitly downloaded previously. Is that the piece of the puzzle that I'm missing?

Also, I'm used to the way that Netbeans 3.6 allows you to mount filesystems. Currently, I am using a single default project. I have a mounted file system calledc:\netbeans\src[/b, and I had two folders,applets andservlets, which I compile separately. (I've never used ANT, at least not directly.) I've read the Netbeans 3.6 to 4.0 migration instructions, but I'm still confused. Does anyone have some tips on how to migrate my applets/servlets?

Please help!

Thanks

[1240 byte] By [cskuehna] at [2007-10-1 1:28:08]
# 1

I can't assist with the migration, but you might find the following useful.

There is an NB 4.0 tutorial that walks you through the creation of an applet and then the embedding of that applet in a web application. I did it, it's quick and straightforward. You end up with a working web app and a project file structure that works. You can examine where the various files are placed, which should help you with the migration.

Here's the tutorial page: http://www.netbeans.org/kb/articles/tutorial-applets-40.html

ChuckBinga at 2007-7-8 1:49:23 > top of Java-index,Administration Tools,Sun Connection...
# 2

Thanks for the tutorial page...

Do you have any ideas about why the version 1.5 compiler can't find the java.servlet and java.servlet.http packages? Version 1.4.2 has no problem with my import statements. Right now that is my more immediate concern.

Thanks again

cskuehna at 2007-7-8 1:49:23 > top of Java-index,Administration Tools,Sun Connection...
# 3

I suspect the difference is due to the change to NB 4.0, not the change to Java 5.

The method that NB 4.0 uses to establish the classpath is totally different from the mount method in 3.6. That's why I suggested you could use the tutorial to figure out the differences.

Refer to the NB 4.0 package help - look up classpath.

ChuckBinga at 2007-7-8 1:49:23 > top of Java-index,Administration Tools,Sun Connection...
# 4

Thanks for your response...

I've just done a search for servlet*.jar on the Netbeans 3.6/JDK 1.4 and Netbeans 4.0/JDK 1.5 environments. The same 11 JAR files exist, in the same locations(almost the same, anyway; the Tomcat 5.0.28 and Netbeans 4 directory structures are a little different).

Both software bundles contain the same files, yet 1.4.2 compiles without any problems or additional configuration. In other words, I reinstalled the old 1.4.2/ Netbeans 3.6 bundle, took my existing source code, and started compiling.

I understand that there are changes to the way Netbeans 4.0 deals with class paths, but I thought that was for the classes I've written. Shouldn't the compiler be able to find the servlet API, since it was bundled/installed with everything else?

Any thoughts on this would be appreciated

Thanks

cskuehna at 2007-7-8 1:49:23 > top of Java-index,Administration Tools,Sun Connection...
# 5

Well, I created a project as a Web Application in order to have the Servlet API v2.4 in the class path by default, then copied my two servlets into that project. There was a Java Class written by someone else that I needed as well, so I manually added that to the class path (under Project properties). At least now it compiles...

Here's the deal. Under NB 3.6/JDK 1.4, I would compile these two servlet classes, then FTP the two individual class files directly to the WEB-INF/classes folder on my web site, where they performed correctly.

Under NB 4.0/JDK 1.5, I've FTP'd the compiled class files from the build directory to WEB-INF/classes but I get HTTP response code 500 when I try to access the servlet.

servletURL = new URL("http://www.sundialvideo.com/servlet/SubmitFormServlet");

Out of desperation, I took the WAR file generated in the dist directory and FTP'd that to WEB-INF/classes and got the same message.

I assume the way you invoke a servlet has not changed, so I must still be doing something wrong in the IDE. I really don't need all the XML, etc. - these servlets just don't require that level of complexity.

Is there a way to set up the NB 4.0 IDE so that I can get these 2 servlet classes running as they used to (e.g., copy *.class to WEB-INF/classes)? Or is there another way entirely?

Any assistance would be appreciated.

cskuehna at 2007-7-8 1:49:23 > top of Java-index,Administration Tools,Sun Connection...
# 6

Have you even considered the possibility that your application server simply can't handle version 49.x classfiles?

What JVM do you have running on the remote machine? If it's a 1.4.x it can use classfiles of versions 48.x and lower, not version 49.x (which is the version created by the 1.5.x compiler).

jwentinga at 2007-7-8 1:49:23 > top of Java-index,Administration Tools,Sun Connection...
# 7

Thanks for responding...

Before sending my last message on this thread, I emailed my web hosting service about that very question. They claim to support J2SE 1.4.2, Servlet 2.3.

Based on this info, you believe my applets and servlets shouldn't be able to run?

This is curious because I've already recompiled my applets with NB 4.0/JDK 1.5. I've FTP'd the JAR file and tested it on my web site, and it seems to work fine.

I created a new class library project, added my java servlet code, and added the Servlet API 2.3 to the classpath. After compiling the individual files, I FTP'd the *.class files only to the WEB-INF/classes directory. I still get the HTTP 500 error.

Of course, If I recompile the individual servlet classes with NB 3.6/JDK 1.4.2, the servlets work fine.

I'm certainly no expert Java programmer. With that in mind, here are my next questions:

1) Why are my applets able to work compiled under JDK 1.5 if the app server is running 1.4.2?

2) If I've compiled the servlets using the Servlet API 2.3 library, why are they not working? Is that because the compiler is still v1.5?

Thanks in advance for your assistance.

cskuehna at 2007-7-8 1:49:23 > top of Java-index,Administration Tools,Sun Connection...
# 8
To followup on jwenting's post, try compiling (in NB 4.0 and using Java 5) with the javac command option -source 1.4. That creates bytecode compatible with 1.4. (untested, not sure that it will resolve the problem)
ChuckBinga at 2007-7-8 1:49:23 > top of Java-index,Administration Tools,Sun Connection...
# 9

> 1) Why are my applets able to work compiled under

> JDK 1.5 if the app server is running 1.4.2?

because the applet code is never run on the server. It's downloaded to your computer and executed on your (1.5) runtime.

> 2) If I've compiled the servlets using the Servlet

> API 2.3 library, why are they not working? Is

> that because the compiler is still v1.5?

>

yes, that's the reason. -target 1.4 -source 1.4 as compiler flags should give you 1.4 bytecode.

Just -source 1.4 is not enough!

jwentinga at 2007-7-8 1:49:23 > top of Java-index,Administration Tools,Sun Connection...
# 10

> Just -source 1.4 is not enough!

Yes it is. The following from the documentation confirms the correctness of my statement.

-target version

Generate class files that will work on VMs with the specified version. The default is to generate class files to be compatible with the JDK 5 VM.

When the -source 1.4 or lower option is used, the default target is 1.4.

ChuckBinga at 2007-7-8 1:49:23 > top of Java-index,Administration Tools,Sun Connection...
# 11

Actually, the other day I tried the -source switch only, the -target switch only, then both together. I couldnt' get any of them to work properly; Of course, I only had the 1.5 jdk on my system!

Yesterday, I downloaded 1.4.2_06 and installed it on my machine, then used the Java Platform Manager to register it in Netbeans 4.0. After that it's just a question of changing a drop-down list under project properties. It compiled and runs fine.

As you know the issue wasn't really whether I could get it to compile and run... The issue was whether I could get it to compile and run under JDK 5! With JDK 5, the applets seem fine, except for some minor code changes (e.g. enum, etc.).

The servlets, on the other hand, would not run if compiled with JDK 5, even if I used Servlet API 2.3 in the classpath. I wasn't sure if I had migrated my code properly, to NB 4, which was causing runtime problems. However, when compiling with 1.4.2, everything works smoothly.

My conclusiion is that I migrated the code correctly. The problem is that my web hosting service's app server is using JDK 1.4.2, so I have to compile with it as well. At least until they upgrade.

Bottom line - when they do upgrade to JDK 5 and Servlet API 2.4, I should be ready. Thanks for everyone's help!

P.S. One last question: According to "tech support", the app server is running Servlet API 2.3. Just for kicks, I compiled the servlets using JDK 1.4.2, but selected Servlet API 2.4 in the classpath. It still ran fine. How could I run servlets with the 2.4 specification if the app server on which it resides is runninig v2.3?

cskuehna at 2007-7-8 1:49:23 > top of Java-index,Administration Tools,Sun Connection...
# 12
> How could I run servlets with the 2.4 specification if the app server on which it resides is runninig v2.3? Depends on if your servlet uses code that's different in the two versions. Obviously it doesn't, and there's no version checking in effect.
ChuckBinga at 2007-7-8 1:49:23 > top of Java-index,Administration Tools,Sun Connection...
# 13
The version of a class library you link to isn't included in the classfile.As long as you don't use any features which are new to the later version you can compile against it and run against an older version.
jwentinga at 2007-7-8 1:49:23 > top of Java-index,Administration Tools,Sun Connection...