hi folks,basically i am facing a problem importing a package,

dir structure is like

src
|---com
      |---example
                |---model
                        |---BeerExpert.java




package com.example.model;
import java.util.*;

public class BeerExpert
{
    public List getBrands(String color)
    {
        //code goes here
    }

}





src
|---com
      |---example
                |---web
                      |---BeerSelect.java

now in BeerSelect.java i am importing the BeerSelect class from the model directory

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
    {
        //code goes here
    }
}

now at the time of compiling the BeerSelect.java,from cmd,by follwing command

e:\JAVA\Beer-v1\javac -classpath D:/tomcat/common/lib/servlet-api.jar -d classes src/com/example/web/BeerSelect.java

i am getting error

 src\com\example\web\BeerSelect.java:3: package com.example.model does not exist
    import com.example.model.*;
    ^

plz help

Dani AI

Generated

Short answer: the compiler can't find com.example.model because the command-line classpath you gave only included the servlet JAR, and a -classpath on the javac command overrides any CLASSPATH environment setting. As said, the compiler needs the package root (the directory that contains com) on the classpath. was on the right track with the trailing dot — it means “current directory” — but that only helps if your current directory is the package root.

What the dot means and why it confused you: a classpath is a list of directories and JARs. On Windows entries are separated by ;. A single . entry tells javac “look in the current working directory for packages.” If com is actually under a src folder, then . will not find it unless you cd into src. If you prefer to stay in the project root, explicitly add the src folder (the package root) to the classpath instead.

Practical fixes (pick one):

  • Change directory into the folder that directly contains com (the package root) and run javac with the servlet JAR plus . on the classpath so the compiler can find other source files.
  • Or keep your current working directory and put the full path to the src folder on the -classpath along with the servlet JAR.
  • Alternatively, compile the model class first to produce class files, then compile the servlet while including the classes output directory on the classpath.

A safe template to use (replace placeholders) is:

javac -classpath [path-to-servlet-jar];[path-to-src-root] -d [output-dir] [path-to-source-file]

Notes: use ; on Windows and : on Unix, wrap paths with spaces in quotes, and remember -classpath overrides your environment CLASSPATH. If problems persist, verify what javac sees for its classpath (either print %CLASSPATH% or run a tiny Java program that prints java.class.path) and make sure the package-root location you supply actually contains the com directory.

Recommended Answers

All 14 Replies

Your src directory must be in the classpath for the compiler to find com and its subdirectories

i set the classpath through environment variables,and set the following value

E:\JAVA\JSP\beerV1\src;

but it still gives the error.

src\com\example\web\BeerSelect.java:3: package com.example.model does not exist
    import com.example.model.*;
    ^

plz help...

What is the command line you are using? Do you put the path to the src folder in the -classpath option used in the command line?
I don't know if the javac command will use both the environment variable AND the -classpath option's value.

To see what classpath is used by the java program make a small program with this:
System.out.println(System.getProperties().getProperty("java.class.path"));

and execute it with a -classpath option and without to see the difference.

my cmd line is:

e:\JAVA\Beer-v1>javac -classpath D:/tomcat/common/lib/servlet-api.jar -d classes src/com/example/web/BeerSelect.java

and it gives the error:

src\com\example\web\BeerSelect.java:3: package com.example.model does not exist
    import com.example.model.*;
    ^

and i also set E:\JAVA\JSP\beerV1\src; this path to th CLASSPATH value through environment variables

I don't see the path to the src folder in javac's -classpath option. Try putting it there.

Did you write the small test program and test it the two ways I suggested?
What happened?

i already set the path through environment variables

like,

my computer->properties->advanced->environment varibles->classpath->(and here i set the path)

Re-read my past posts.

Did you try putting the path to the src folder on the -classpath option?

@Norm R1

you are saying that i have set the path of the src directory in the -classpath option of the cmd line.

so now my new cmd line is going to be....

E:\JAVA\beerV1>javac E:/JAVA/JSP/beerV1/src D:/tomcat/common/lib/servlet-api.jar -d classes src/com/example/web/BeerSelect.java

but it gives th same error

if i'm wrong please make me correct

to set path or classpath:
use following command in prompt

Set path = " path which u wnat to set "

set classpath=" path u want to set "

The posted command line doesn't have the -classpath option. You should get a different error with that command line. For example: javac: invalid flag: E:/JAVA/JSP/beerV1/src
Please copy the full contents of the command window so we can see what has been entered and what the javac program's response was.

To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

@Norm R1,thnx for ur response

my cmd line is:

E:\JAVA\JSP\beerV1>javac -classpath D:/tomcat/common/lib/servlet-api.jar -d clas
ses 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:14: cannot find symbol
symbol  : class BeerExpert
location: class com.example.web.BeerSelect
                BeerExpert be=new BeerExpert();
                ^
src\com\example\web\BeerSelect.java:14: cannot find symbol
symbol  : class BeerExpert
location: class com.example.web.BeerSelect
                BeerExpert be=new BeerExpert();
                                  ^
3 errors

E:\JAVA\JSP\beerV1>

Where is the path to the src folder pointed to by the -classpath option?
You keep leaving that off. You need to have it in the classpath.

What folder is the src folder in? If it is in the beerV1 folder then you only need to add a . to the -classpath you are using:

-classpath D:/tomcat/common/lib/servlet-api.jar;.

@Norm R1

can you please write the full cmd line.....

now the value which is set on the classpath is E:\JAVA\JSP\beerV1\src\;

and yes,src folder is in the beerV1 folder

BeerV1
|---src
    |---com
        |---example
            |---model
                |---BeerExpert.java

and BeerSelect.java resides in

BeerV1
|---src
    |---com
        |---example
            |---web
                |---BeerSelect.java

can you please explain the line

"If it is in the beerV1 folder then you only need to add a . to the -classpath you are using:

-classpath D:/tomcat/common/lib/servlet-api.jar;.

"
it will be helpful for me....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.