Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why can't use javac to compile servlet?

 
Lowera Chris
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Is anyone who can help me with javac -classpath to compile servlet?
I typed the following into the windows cmd box:
my project directory>javac -classpath \my path\tomcat\common\lib\servlet-api.jar -d classes src\web\MyServlet.java
I got error:
src\web\MyServlet.java:1: 'class' or 'interface' expected
import javax.servlet.*;

I am confused and do not know why. If you know, could you please tell me?

Thanks a lot in advance.

Sunny
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post the code for MyServlet? (Or at least the first few lines.)
 
Lowera Chris
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Marc,
Thank you very much for your message. The following is the code:
package web;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class MyServlet extends HttpServlet {
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws IOException {
PrintWriter out = response.getWriter();
..........
}
}
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error are you getting? I tried and got
campbell@linux-pgix:~/java> javac -d . MyServlet.java
MyServlet.java:2: package javax.servlet does not exist
import javax.servlet.*;
^
MyServlet.java:3: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
MyServlet.java:5: cannot find symbol
symbol: class HttpServlet
public class MyServlet extends HttpServlet {
^
MyServlet.java:6: cannot find symbol
symbol : class HttpServletRequest
location: class web.MyServlet
public void doGet (HttpServletRequest request,
^
MyServlet.java:7: cannot find symbol
symbol : class HttpServletResponse
location: class web.MyServlet
HttpServletResponse response)
^
5 errors
campbell@linux-pgix:~/java>
The javax.servlet.http package is not a J2SE package; try installing J2EE.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15279
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at Compiling servlets in the JavaRanch FAQ. You need to add the servlet API classes to your classpath; they are not part of Java SE.

But the error that you are getting is strange. If you get "'class' or 'interface' expected", then you've probably made a typo or put the 'import' statement in a strange place in your source code.
 
Lowera Chris
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
Thanks a lot for your reply. Actually, I got error:
'class' or 'interface' expected
import javax.servlet.*;

'class' or 'interface' expected
import javax.servlet.http.*;

........

Could you please tell me why?

Thank you in advance.

Sunny
 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This error occurs when there is code outside of a class declaration. The usual cause of this is when you're declaring a few objects at the start of the class, and mistakenly place them before the class has started.


http://www.kirun.co.uk/java/answers/43.php
 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lowera Chris:
Actually, I got error:
'class' or 'interface' expected
import javax.servlet.*;

'class' or 'interface' expected
import javax.servlet.http.*;

........
Could you please tell me why?

When compiler tells you that a 'class' or 'interface' expected then it means that it cannot find that class or interface in the system.. So you need to install J2EE and then try the command which works fine in your case... Are you clear?....

 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that's it. If Lowera didn't have J2EE installed or pointed to that incorrectly the error would be different, indicating the import could not be found.

My guess is that the import statements follow something other than the package declaration, other imports, whitespace or comment.

Lowera, can you post your code until these import statements?
[ July 15, 2008: Message edited by: Rob Prime ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, I think we have several different versions of the app running with different error messages; I tried it with copy-and-paste and got the 5 errors I posted earlier, not including a "class enum or interface expected." So obviously the {} balancing problem was corrected earlier.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like I said, I don't think it's a classpath problem. That seems to be ok.

I don't know what's causing this error, but when I remove the "i" from "import" I get the same message as Lowera. In fact, when I ran this through the Java 1.4.2 compiler*, I got the following errors:
<blockquote>code:
<pre name="code" class="core">MyServlet.java:3: 'class' or 'interface' expected
mport javax.servlet.*;
^
MyServlet.java:4: 'class' or 'interface' expected
import javax.servlet.http.*;
^
MyServlet.java:5: 'class' or 'interface' expected
import java.io.*;
^
3 errors
</pre>
</blockquote>
See how similar those errors are?

So my guess is still that something is wrong between the package declaration and the first import statement.


* The compilers of Java 5.0 and up will also mention "enum" being expected, so that's how I know that Lowera is using Java 1.4
 
Lowera Chris
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rob,
You are right. I am using Java 1.4.2_17
Should I change it to Java 5?

Thank you and all the other friends.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, upgrading isn't necessary for this. It's not what's causing the problem.

Can you check if there are any strange characters between the package statement and the first import statement? Because the error just appears to say that there is something there that doesn't belong there.

The following code can be used to recreate your error message (with a different import statement though):
<blockquote>code:
<pre name="code" class="core">package bla;

class X {}

import java.io.*;
</pre>
</blockquote>
As such, I think that there is something valid (not whitespace) between the package and the import. If it wasn't something valid then that line would give an error as well.
 
Lowera Chris
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rob,
Thanks a lot for your help. Everything is ok now.

Thank all the friends who helped me.

Best wishes,
Sunny
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what was the problem?
 
Lowera Chris
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rob,
I am still not quite sure about the problem. The only thing I did was to add a "/" which I missed in the last </url-pattern> in the web.xml. I think when I compiled a servlet, it would check the web.xml file. If there was something wrong, the errors would occur. However, I don't know why I got those 3 errors. The errors did not tell me that the web.xml was incorrect.

Thank you very much for asking me the problem and your help.

Best wishes,
Sunny
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest, the class shouldn't even have to know about the web.xml file, so this solution is quite strange.

Ah well, the problem is gone, and that's what counts.
 
Lowera Chris
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rob,
I am very honest to say it is a mystery to me. I know the compilation of a servlet is nothing to do with a web.xml file now, but it is a strange problem.

Thank you so much again.

Best wishes,
Sunny
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic