This week's book giveaway is in the Functional programming forum.
We're giving away four copies of Functional Design and Architecture and have Alexander Granin on-line!
See this thread for details.
Win a copy of Functional Design and Architecture this week in the Functional programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Need Help to compile the code

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic