you should get this error while compiling! did you set classpath ? how did you compile? once you get compilation successful then you can expect an exception [see sachin post]
First of all thank you for helping me out
I have following directly structure
c:\ranch\model with file Test2.java
and
c:\ranch\web with file Test1.java
and running like this...
c:\ranch\model>javac Test2.java and its showing this error
C:\ranch\model\Test2.java:2: package ranch.web does not exist
import ranch.web.*;
^
C:\ranch\model\Test2.java:12: cannot find symbol
symbol : class Test1
location: class ranch.model.Test2
Test1 test=new Test1();
^
C:\ranch\model\Test2.java:12: cannot find symbol
symbol : class Test1
location: class ranch.model.Test2
Test1 test=new Test1();
^
3 errors
Tool completed with exit code 1
The SOURCE CODE FOR THE FILE Test2 is
package ranch.model;
import ranch.web.*;
public class Test2
{
//Test1 test=new Test1();
//test.run();
I can compile everything by running the following from the command prompt (example for Test2.java):
c:>javac ranch\model\Test2
To be able to RUN the file, I need to make sure Java knows where to find the classes I'm importing, in this case, the ones in the web package. You can think of packages like folders or directories.
You have the following structure:
ranch->web
ranch->model
If you are IN the ranch.web package, it has no way of figuring out where the ranch.model package is, as it's not visible at the moment. The simplest solution is to run the command in such a way that it's easy to find the folder, for example:
C:>java ranch.model.Test2
This will generate the output correctly as it makes it easy for Java to find the classes you are important relative to the current folder. An alternative would of course be to update the CLASSPATH to include your own classes.
Might I suggest you Google Java Classpath and learn how that works? Packages can be a bit confusing in the beginning, but you'll soon get the hang of it.
deepak carter wrote:
i.e C:\javac ranch\mode\Test2.java
javac uses \ to seperate the packages and *java uses . to seperate the packages*. so
when you run the class, please use . as in java ranch.model.Test2 . hope your Test2.class is in ranch.model package!
The source code for BeerSelect is
package com.example.web;
import com.example.model.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class BeerSelect extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
String c=req.getParameter("color");
BeerExpert be=new BeerExpert();
List result=be.getBrands(c);
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("Beer Selection Advice<br>");
Iterator it=result.iterator();
while(it.hasNext())
{
out.println("<br>try: "+it.next());
}
}
}
and source code for BeerExpert is
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("Jail Pale Ale");
brands.add("Gout Stout");
}
return(brands);
}
}
Beer Expert is compiling fine but whenevr i am running BeerSelect its giving me an error
C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\Beer-v1\WEB-INF\classes\com\example\web\BeerSelect.java:2: package com.example.model does not exist
import com.example.model.*;
^
C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\Beer-v1\WEB-INF\classes\com\example\web\BeerSelect.java:14: cannot find symbol
symbol : class BeerExpert
location: class com.example.web.BeerSelect
BeerExpert be=new BeerExpert();
^
C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\Beer-v1\WEB-INF\classes\com\example\web\BeerSelect.java:14: cannot find symbol
symbol : class BeerExpert
location: class com.example.web.BeerSelect
BeerExpert be=new BeerExpert();
^
3 errors
Hmm... Sorry, I guess I don't understand the local customs. On many forums it's considered spam to start new threads on what appears to be the same issue. If there are no responses I'll start a new thread.
By the way, I am working on the same walkthrough as Deepak from Head First Servlets and JSPs. I can get the sample to run if I compile locally and then manually deploy the resulting class file, but it doesn't appear that should be necessary.
Post by:autobot
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
a bit of art, as a gift, the permaculture playing cards