• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

can't import own model

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be that you have named your Java-files .html and not .java?
 
Patrick Smith
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic