Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Help to compile the code

 
Sneha Kulkarni
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am trying to run the example given in the third chapter of the HFSJ..
1)I created the HTML and DD...and run the program .
Executed perfectly.
2)Created the First Version of Servlet BeerSelect.java and executed perfectly.
3)Created the BeerExpert.java,compiled and copied a copy of .class file of BeerExpert in com/example/model packege.
4)Modiefied the first verion of servlet to second version as below

//BeerSelect.java

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 IOException,ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Beer Selection Advice <br>");
String c = request.getParameter("color");
//out.println("<br>Got beer color " +c);

BeerExpert be = new BeerExpert();
List reult = be.getBrands(c);
Iterator it = result.iterator();
while(it.hasNext())
{
out.print("<br>try: "+it.next());
}


}
}

And BeerExpert.java is as below

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("Red Moose");
}
else
{

brands.add(" Jial Pale Ale");
brands.add(" Gout Stout");
}
return(brands);
}
}

5)But when i tried to compile the BeerSelect.java, i am getting an error
As-
BeerSelect.java:3: package com.example.model does not exist
import com.example.model.*;
^
So what is the mistake i have done here..please anybody suggest me ,what i have to do to execute it correctly.

Thanks in advance
Sneha
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you remember what you learned at SCJP about compiling classes in packages ? Try to think about how to make sure that the compiler will find "com.example.model". If you still struggle, you can try the ranch's search as this question is being asked quite often.
 
Ravi Singh
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is this dude!

You must have created your application inside webapps folder of tomcat .

What you have to do is inside the WEB-INF folder of your project first create a classes folder anf then place create folder stuctuer for the BearSelect.java (i mean create com/example/model) and place .class file here .

This will work
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will work

No. This is a compilation error.
 
Sneha Kulkarni
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I checked the past reply,i tried everything they have mentioned.
I changed my import statement as-import com.example.model.BeerExpert;
I created the src directory and tried to compile from src.
Then also its giving the same error.

I tried to compile from the below mentioned path

D:\dir\apache-tomcat-5.5.26\webapps\ram\WEB-INF\classes\com\example\web>javac BeerSelect.java

Thanks in Advance
Sneha
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to put the "com" package in your classpath. The fastest way to do it would be :
javac -classpath D:\dir\apache-tomcat-5.5.26\webapps\ram\WEB-INF\classes;. BeerSelect.java

I would not put java files in the classes directory, but it's a matter of taste
 
Amol Fuke
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sneha ,

Which editor you ar using ?

If you are using eclipse , then left click on project->properties->default output location-> put '<proj_name>/application/WEB-INF/classes'

Next time when you save any java file , complied code will go to classes with package structure.

Thanks,
Amol
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic