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

problen in headfirst mvc tutorial

 
muktesh tripathi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting package com.example.model.* package does not exist error when i try to compile the servlet version 2 in mvc tutorial. where as I have followed the developement deployment approach as in the book but it does not let me compile the servlet where as the BeerExpert class compiles ok.


PLEASE HELP....

I think my earlier approach of not following developement approach and compiling classes together was nice....

comments please
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"mukteshtripathi "please check your private messages for an important administrative matter. You can see them by clicking the My Private Messages link above.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will you please let us know what your development and deployment environment is? By this I mean the directory structure. Also I guess you can interpret that " package does not exist error " it means that the package does not exist at all or it cannot be located in the classpath.

And welcome to Javaranch
 
muktesh tripathi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My development structure is BeerV1.src.com.example.web.BeerSelect.java
and
BeerV1.src.com.example.model.BeerExpert.java

package com.example.model;
import java.util.*;

public class BeerExpert {
public List getBrands(String color){

List brands = new ArrayList();
if(color.equals("amber")){
brands.add("Jack Amber");
brands.add("Reb Moose");
}
else{
brands.add("Jail Pale Ale");
brands.add("Gout Scout");
}
return (brands);
}
}


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

public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Beer Selecion Advice<br>");
String c = request.getParameter("color");
BeerExpert be = new BeerExpert();
List result = be.getBrands(c);
Iterator it = result.iterator();
while(it.hasNext()){
out.print("<br> Try: "+it.next());
}
}
}

to compile BeerExpert.java cd BeerV1
javac -d classes src/com/example/model/BeerExpert.java

it lands .class file to right folder correctly.

but on compiling BeerSelect cd BeerV1
javac -classpath c:\servlet-api.jar -d classes src/com/example/web/BeerSelect.java
it shows package does not exist error....

please help
 
Satish Kandagadla
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Muktesh Tripathi:
My development structure is BeerV1.src.com.example.web.BeerSelect.java
and
BeerV1.src.com.example.model.BeerExpert.java

package com.example.model;
import java.util.*;

public class BeerExpert {
public List getBrands(String color){

List brands = new ArrayList();
if(color.equals("amber")){
brands.add("Jack Amber");
brands.add("Reb Moose");
}
else{
brands.add("Jail Pale Ale");
brands.add("Gout Scout");
}
return (brands);
}
}


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

public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Beer Selecion Advice<br>");
String c = request.getParameter("color");
BeerExpert be = new BeerExpert();
List result = be.getBrands(c);
Iterator it = result.iterator();
while(it.hasNext()){
out.print("<br> Try: "+it.next());
}
}
}

to compile BeerExpert.java cd BeerV1
javac -d classes src/com/example/model/BeerExpert.java

it lands .class file to right folder correctly.

but on compiling BeerSelect cd BeerV1
javac -classpath c:\servlet-api.jar -d classes src/com/example/web/BeerSelect.java
it shows package does not exist error....

please help


src is a source folder. In your class the package name starts with com.example.model. Change your directory to src and then try compiling. The error seems to be valid as src is a source folder.
 
muktesh tripathi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem is with my classpath setting or with my cmd for compiling due to some classpath issue the jvm is not able to find the package in the same directory(probably)..

I am not able to figure out the mistake I am making

please help
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is simple, while compiling just as you provide the path for servlet-api.jar, you need to provide the classpath for BeerSelect.java.

Hope this helps
 
muktesh tripathi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the help

finally I made it(with help from an very old thread on this site only)

I had to provide path to COMPILED BeerExpert.class file which was in classes directory after compiling BeerExpert.java

working cmd:

javac -classpath c:\servlet-api.jar;classes; -d classes src/com/example/web/BeerSelect.java


Thanks again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic