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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Compiling a Servlet" Watch "Compiling a Servlet" New topic
Author

Compiling a Servlet

Suvojyoty Saha
Greenhorn

Joined: Apr 19, 2011
Posts: 25

Hi I'm new to Servlets. I'm having a lot of difficulty in compiling and creating the class file of the servlet.

As per listed in Head First i've created the directory structure
C:\Documents and Settings\dodo\My Documents\MyProject\beerV1\src\com\example\web\BeerServlet.java
C:\Documents and Settings\dodo\My Documents\MyProject\beerV1\classes
C:\Documents and Settings\dodo\My Documents\MyProject\beerV1\etc\web.xml
C:\Documents and Settings\dodo\My Documents\MyProject\beerV1\lib\servlet-api.jar
C:\Documents and Settings\dodo\My Documents\MyProject\beerV1\web


my path is set as C:\Program Files\Java\jdk1.6.0_23\bin
classpath as D:\tomcat\common\lib

after going to the directory i'm entering the following command

C:\Documents and Settings\dodo\My Documents\MyProject\beerV1>
javac -classpath D:\tomcat\common\lib\servlet-api.jar;classes:. -d classes src/com/example/web/BeerSelect.java


but getting this error

javac: file not found: src\com\example\web\BeerSelect.java
Usage: javac <options> <source files>
use -help for a list of possible options


could someone please help me resolve this issue.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

You are using a wrong set of file seperators- "/" or "\". Also note that you would have to use the servlets in a web container- example Tomcat so that it will manage the servlets lifecylce.


Mohamed Sanaulla | My Blog
Suvojyoty Saha
Greenhorn

Joined: Apr 19, 2011
Posts: 25

i tried using both the file seperators "/" and "\" but still getting the same msg. File not found.

I also tried to compiling in the following manner :

C:\Documents and Settings\dodo\My Documents\MyProject\beerV1\src\com\example\web>
javac -classpath D:\tomcat\common\lib\servlet-api.jar BeerSelect.java



but still getting this error

javac: file not found: BeerSelect.java
Usage: javac <options> <source files>
use -help for a list of possible options

Ankit Tripathi
Ranch Hand

Joined: Oct 17, 2009
Posts: 186
Hi Suvojyoti,welcome to javaranch.........

most possible reason can be extension problem.if you are sure that file java file exists in directory from where you are compiling then use "dir" command to list the contents of that directory,check whether it is "BeerSelect.java" or "BeerSelect.java.txt".
also check for spaces and other punctuation characters in name of java file.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

Try wrapping the path to jar inside quotes.

javac -classpath "D:\tomcat\common\lib\servlet-api.jar" BeerSelect.java


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Suvojyoty Saha
Greenhorn

Joined: Apr 19, 2011
Posts: 25

Thanks Ankit for the warm welcome

here's screen shot


i've checked for the file extension it seems correct. Is there any other options that i can try?
Suvojyoty Saha
Greenhorn

Joined: Apr 19, 2011
Posts: 25

Vijitha the same issue still persists.

for servlet compilation we only need the servlet-api.jar i guess

For the following command "javac -classpath "D:\tomcat\common\lib\servlet-api.jar" BeerSelect.java"

-classpath "D:\tomcat\common\lib\servlet-api.jar" ---- denoting the path where the servlet-api resides
and
BeerSelect.java in the current path "C:\Documents and Settings\dodo\My Documents\MyProject\beerV1\src\com\example\web"

please do correct me if i'm wrong.
Deepakkumar Devarajan
Ranch Hand

Joined: Apr 19, 2011
Posts: 52

Check the class path, you have colon instead of semi colon. Also enclose the path in double quotes.


Regards,
Deepakkumar Devarajan
Deepakkumar Devarajan
Ranch Hand

Joined: Apr 19, 2011
Posts: 52

OMG , none of us noticed it. You created BeerServlet.java and tryibg to execute BeerSelect.java as it is in HeadFirst book. lol , ,
Suvojyoty Saha
Greenhorn

Joined: Apr 19, 2011
Posts: 25

Thanks a Lot Deepak... you are a life saver..

@Ankit: My bad i still wasn't able to figure out problem. Thanks a lot for your help.

@Other: Thanks a lot for your precious inputs. All's working perfectly. I'm able to get the output as per mentioned in Head First. Thanks a lot once again.
Deepakkumar Devarajan
Ranch Hand

Joined: Apr 19, 2011
Posts: 52

You are Welcome.. Life Saver??? hahaha , I understand (Your lead follow up with you for the output).
Suvojyoty Saha
Greenhorn

Joined: Apr 19, 2011
Posts: 25

hehe... no just practicing things on my own. going through the Head First book and trying to improve my skills in Servlets and JSP's

i'm presently facing a new issue....

This is the BeerServlet code

package com.example.web;

import com.example.model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class BeerServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException
{


BeerExpert be = new BeerExpert();
String str = request.getParameter("color");
List result = be.getResult(str);

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("Beer Selection Advice <br>");

//out.println("<br>Got Beer Color: " + str);

out.print("Values Retrieved: ");
Iterator it = result.iterator();
while(it.hasNext())
{
String s1 = (String) it.next();
out.print(" "+s1);
}
}
}



and below is the BeerExpert code

package com.example.model;

import java.util.*;

public class BeerExpert
{
public List getResult(String value)
{
List al = new ArrayList();
if(value.equals("amber"))
{
al.add("amber");
al.add("found");
}
else
{
al.add("amber");
al.add("not found");
}

return al;
}
}


when i'm executing the folowing command
C:\Documents and Settings\dodo\My Documents\MyProject\beerV1>javac -classpath D:\tomcat\common\lib\servlet-api.jar;classes:. -d classes src/com/example/web/BeerServlet.java

i'm getting the following error

src\com\example\web\BeerServlet.java:3: package com.example.model does not exist

import com.example.model.*;
^
src\com\example\web\BeerServlet.java:15: cannot find symbol
symbol : class BeerExpert
location: class com.example.web.BeerServlet
BeerExpert be = new BeerExpert();
^
src\com\example\web\BeerServlet.java:15: cannot find symbol
symbol : class BeerExpert
location: class com.example.web.BeerServlet
BeerExpert be = new BeerExpert();
^
3 errors


the packages have been named but some how its not able to identify the package...
Deepakkumar Devarajan
Ranch Hand

Joined: Apr 19, 2011
Posts: 52

Do com folder is a sub folder of beerV1? if not you should tell the compiler where it's placed(Just add it to classpath) . Please note that you have a colon after classes in class path, replace it with ; .
Suvojyoty Saha
Greenhorn

Joined: Apr 19, 2011
Posts: 25

thanks once again Deepak. On replacing the :. with ;. i got the output..
Deepakkumar Devarajan
Ranch Hand

Joined: Apr 19, 2011
Posts: 52

You are Welcome.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

Suvojyoty,

Next time make sure you use code tags when posting code. You may wrap your code inside [code][/code] tags or use the "Code" button in editing area to do this.
Suvojyoty Saha
Greenhorn

Joined: Apr 19, 2011
Posts: 25

Vijitha,

Thanks for the info. would surely make a note of using the code tags when posting code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compiling a Servlet
 
Similar Threads
compiling using -d flag
Head First Servlets & JSP: Can't compile second version of BeerSelect.java
Head First Servlet : Beer Version 2 Problem
need basic help to compile first servelet
Package doesn't exists - error!!- How to resolve?