*
The moose likes Beginning Java and the fly likes Why can't use javac to compile servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why can Watch "Why can New topic
Author

Why can't use javac to compile servlet?

Lowera Chris
Greenhorn

Joined: Jul 10, 2008
Posts: 10
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

Joined: Aug 31, 2004
Posts: 11343

Could you post the code for MyServlet? (Or at least the first few lines.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Lowera Chris
Greenhorn

Joined: Jul 10, 2008
Posts: 10
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

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Aug 16, 2005
Posts: 13869
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Lowera Chris
Greenhorn

Joined: Jul 10, 2008
Posts: 10
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

Joined: Dec 12, 2006
Posts: 751
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


Jigar Naik


Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

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?....



Never try to be a hard-worker. Be a smart-worker.
My Blog
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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

Joined: Jul 10, 2008
Posts: 10
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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

Joined: Jul 10, 2008
Posts: 10
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

So what was the problem?
Lowera Chris
Greenhorn

Joined: Jul 10, 2008
Posts: 10
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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

Joined: Jul 10, 2008
Posts: 10
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can't use javac to compile servlet?
 
Similar Threads
chapter 3 "mini MVC"
problem in servlet complation
Error: Requested resourse() not available
how to set class path
Compile error