aspose file tools*
The moose likes Beginning Java and the fly likes can't import own model Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can Watch "can New topic
Author

can't import own model

Patrick Smith
Ranch Hand

Joined: Oct 08, 2007
Posts: 35
Hi,

My WebApp in the development phase looks like the following:
- a model file: webappname/src/com/example/model/BeerExpert.html. It has a package declaration that is "package com.example.model;"
- a view file: webappname/src/com/example/web/BeerSelect.html. It has a package declaration that is "package com.example.web;". That file imports the model file with the following command: "import com.example.model.*;"
When i compile the model file from webappname, all ok. But then,. when i compile the view file from webappname, it says:
"
src/com/example/web/BeerSelect.java:3: package com.example.model does not exist
import com.example.model.*;
^
"

Any advice? Thanks for your help, Patrick
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

It might be that you have named your Java-files .html and not .java?
Patrick Smith
Ranch Hand

Joined: Oct 08, 2007
Posts: 35
the extensions are ok, but i think there's a problem with the classpath, as it can't find the package. If i have the following situation:
file a is in: webapps/app_name/src/test/model/a.java
file b is in: webapps/app_name/src/test/web/b.java
I compile file a:
webapps/app_name>javac -d classes src/test/model/a.java -> all ok, a.class gets created in webapps/app_name/classes/test/model/a.class
I compile file b, that imports file a (import test.model.a.* ->
javac says:
"src/com/example/web/BeerSelect.java:3: package test.model does not exist"
The question is wether the classpath needs to be set specifically for those files below the webapps directory or does it find it automatically?
Matteo Di Furia
Ranch Hand

Joined: Jun 20, 2008
Posts: 102
"test.model.a.*" should be replaced by "test.model.*"

Also you should specify the option "-classpath /src/" to let the compiler find the class file for a
[ December 09, 2008: Message edited by: Matteo Di Furia ]
Patrick Smith
Ranch Hand

Joined: Oct 08, 2007
Posts: 35
I've tried it all out, no change. So i give you all transparently::
in a: the code of file BeerExpert.java, located in C:\DevEnv\beerV1\classes\com\example\web,
in b: the code of file BeerSelect.java, located in C:\DevEnv\beerV1\classes\com\example\model,
in c: the javac output when compiling file "BeerSelect.java" from C:\DevEnv\beerV1>
in d: the set of the classpath (in Windows XPSP2)

a)code of file BeerExpert.java (the compilation here works fine):
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;
}
}

b)code of file BeerSelect.java (the compilation here fails, see ouput below):
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");
BeerExpert a =new BeerExpert();
List result =a.getBrands(c);
Iterator it = result.iterator();
while(it.hasNext()) {
out.print("<br>try:"+it.next());
}
}
}

c) compiler output:
C:\DevEnv\beerV1>javac -d classes src/com/example/web/BeerSelect.java
src/com/example/web/BeerSelect.java:3: package com.example.model does not exist
import com.example.model.*;
^
src/com/example/web/BeerSelect.java:15: cannot find symbol
symbol : class BeerExpert
location: class com.example.web.BeerSelect
BeerExpert a = new BeerExpert();
^
src/com/example/web/BeerSelect.java:15: cannot find symbol
symbol : class BeerExpert
location: class com.example.web.BeerSelect
BeerExpert a = new BeerExpert();
^
3 errors

C:\DevEnv\beerV1>javac -d classes src/com/example/web/BeerSelect.java
src/com/example/web/BeerSelect.java:3: package com.example.model does not exist
import com.example.model.*;
^
src/com/example/web/BeerSelect.java:15: cannot find symbol
symbol : class BeerExpert
location: class com.example.web.BeerSelect
BeerExpert a = new BeerExpert();
^
src/com/example/web/BeerSelect.java:15: cannot find symbol
symbol : class BeerExpert
location: class com.example.web.BeerSelect
BeerExpert a = new BeerExpert();
^
3 errors

d) classpath set in the system variables screen: "C:\Program Files\Tomcat\common\lib\servlet-api.jar; C:\DevEnv\beerV1\src; C:\DevEnv\beerV1\classes;"
Patrick Smith
Ranch Hand

Joined: Oct 08, 2007
Posts: 35
It works now. I've tried lots of Classpaths, the lastz one worked and looks like this:
"set CLASSPATH=.;C:\;C:\Program Files\Tomcat\common\lib\servlet-api.jar;C:\DevEnv;C:\DevEnv\beerV1\classes\com\example\model;classes"
A soon as i leave thge last one ("classes") away, it can't find that package anymore. What is the meaning of that classpath extension? My problem is solved, but no idea why. And thats no good feeling....
Regrads, thanks for feedback on how to solve this in a controlled way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can't import own model
 
Similar Threads
Ch3 Excercise in HFJS
servlet compiling errors
compiling servlet
Command line arguments
Head First Servlet : Beer Version 2 Problem