关于java中的包

时间:2011-08-04 05:31:43

标签: java

我有以下目录结构。

C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\Beer-v1\WEB-INF\classes\com\example\model\BeerExpert.java

C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\Beer-v1\WEB-INF\classes\com\example\web\BeerSelect.java

BeerSelect的源代码是

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());
}
}
}

和BeerExpert的源代码是

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);
}
}

啤酒专家编译正常但是我正在运行BeerSelect它给我一个错误

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个错误

工具已完成,退出代码为1

2 个答案:

答案 0 :(得分:0)

您是否已将CLASSPATH设置为包含BeerExpert?将* C:\ Program Files \ Apache Software Foundation \ Tomcat 6.0 \ webapps \ Beer-v1 \ WEB-INF \ classes * 目录包含到CLASSPATH中

答案 1 :(得分:0)

看起来您使用的是错误的构建位置。您需要在WEB-INF\classes中获取相对路径。 IE,首先运行:

cd "C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\Beer-v1\WEB-INF\classes"

您可能还需要在命令行参数中添加-classpath .;%CLASSPATH%

按要求提供完整命令:

C:\> cd "C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\Beer-v1\WEB-INF\classes"
C:\program files\apache\tomact 6\webapps\Beer-v1\WEB-INF\classes> javac -classpath .;%CLASSPATH% com\example\model\BeerExpert.java