aspose file tools*
The moose likes Servlets and the fly likes Problems compiling servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problems compiling servlet" Watch "Problems compiling servlet" New topic
Author

Problems compiling servlet

jagan raja
Ranch Hand

Joined: Jan 29, 2010
Posts: 54
When I tried compilng my servlet file I get the following error.

C:\Documents and Settings\MyServlet>javac -d WEB-INF\classes src
\mypackage\*.java
src\mypackage\LoginServlet.java:21: cannot access javax.servlet.http.HttpServlet

bad class file: C:\Documents and Settings\My Documents\Downloads\servl
et-api.jar(javax/servlet/http/HttpServlet.class)
class file has wrong version 49.0, should be 48.0
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
public class LoginServlet extends HttpServlet {

Can any one please help me out.

Thanks in advance
adil qureshi
Ranch Hand

Joined: Jul 11, 2008
Posts: 48
the first problem is that the servlet-api.jar is not in the class path just set it in class path and then tell whats message you are getting...may be that resolves the problem


SCJP 1.5,SCWCD 1.5
jagan raja
Ranch Hand

Joined: Jan 29, 2010
Posts: 54
I am getting the same error again.

Even JBoss is not working. I am getting the exception as

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/jboss/Mai
n (Unsupported major.minor version 49.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:537)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You have compiled the class with a Java version later than the Java version you're trying to run it with.
jagan raja
Ranch Hand

Joined: Jan 29, 2010
Posts: 54
I couldn't understand you clearly.

I have set CLASSPATH to C:\Documents and Settings\My Documents\Downloads\servlet-api.jar

My JAVA_HOME is set to C:\Program Files\j2sdk1.4.2_02

My Path is set to C:\Program Files\j2sdk1.4.2_02\bin;C:\Program Files\Java\jre1.6.0_04\bin;

If I have gone wrong somewhere do say and Can you advise me what to do for this?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You're running the class with an earlier version than you're compiling with.

In other words, you're compiling with 1.6 and trying to run with 1.4 (or 1.5). Either run the class with a later version of Java, or compile with an earlier version.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
My JAVA_HOME is set to C:\Program Files\j2sdk1.4.2_02

My Path is set to C:\Program Files\j2sdk1.4.2_02\bin;C:\Program Files\Java\jre1.6.0_04\bin;

Both these indicate that you're using Java 1.4, which you should run, not walk, to delete from your system - that's how obsolete it is.

To make matters worse, you have the binaries of both Java 1.4 and Java 6 in the PATH - that's a problem waiting to happen.


Ping & DNS - my free Android networking tools app
adil qureshi
Ranch Hand

Joined: Jul 11, 2008
Posts: 48
jagan raja wrote:I am getting the same error again.

Even JBoss is not working. I am getting the exception as

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/jboss/Mai
n (Unsupported major.minor version 49.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:537)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)


hey just ensure that you are using the same version of java everywhere .
jagan raja
Ranch Hand

Joined: Jan 29, 2010
Posts: 54
Now I my CLASSPATH is set to
C:\Documents and Settings\My Documents\Downloads\servlet-api.jar


PATH
C:\Program Files\Java\jre1.6.0_04\bin


JAVA_HOME
C:\Program Files\Java\jre1.6.0_04


After doing this my JBOSS runs fine.

But when i try compile my servlet file I get the same error as before.

C:\Documents and Settings\jagan\MyServlet>javac -d WEB-INF\classes src
\mypackage\*.java
src\mypackage\LoginServlet.java:12: cannot access javax.servlet.http.HttpServlet

bad class file: C:\Documents and Settings\604848923\My Documents\Downloads\servl
et-api.jar(javax/servlet/http/HttpServlet.class)
class file has wrong version 49.0, should be 48.0
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
public class LoginServlet extends HttpServlet {
^
1 error


Thanks in advance
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
The number "48.0" indicates that you're still using Java 1.4. My guess is that you won't be making much progress until you've completely erased that from your system.

See byte offsets 6 and 7 in http://en.wikipedia.org/wiki/Class_%28file_format%29#General_layout for what that number means.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 645
If you're sure you're getting the 1.6 version of javac, then what is the version of the servlet-api.jar file in your downloads directory?
jagan raja
Ranch Hand

Joined: Jan 29, 2010
Posts: 54
Yes when I give java -version I get java version "1.6.0_04"
Java(TM) SE Runtime Environment (build 1.6.0_04-b12)
Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode)

And I have taken the servlet-api.jar from JBOSS folder.
How can I resolve this?
vijayakumar durai
Ranch Hand

Joined: Aug 18, 2008
Posts: 153
remove the servlet-api.jar file and download the new servlet-api.jar and set the class path
jagan raja
Ranch Hand

Joined: Jan 29, 2010
Posts: 54
Thank you for all your assistance.

Now its working fine.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problems compiling servlet