wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Package does not exist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Package does not exist" Watch "Package does not exist" New topic
Author

Package does not exist

sameer khazi
Greenhorn

Joined: Sep 05, 2008
Posts: 28
Hi All...as i am new member i don't know where to post this particular post. Kindly accept my apologies if posted in a wrong forum.

I am working on Tomcat 5.5 and Java SE 5.0. I am working on servlets.

I do have a servlet file at the following directory. this is not a tomcat directory, but a simple directory as shown :

NO 1: C:\myproject\beerV1\src\com\example\web ..
Over here at Web i have servlet file by name BeerSelect.java. The package name give in this servlet file is "package com.example.web" (excluding the double quotes).

Now, i have another servlet program , a src file and a .class file at the following path..

NO 2: C:\myproject\beerV1\src\com\example\model

The problem i am facing is when i go to compile the Servlet file from the path as shown in the first path (No 1) my servlet is not being compiled and i get the following error..

BeerSelect.java:3: package com.example.model does not exist
import com.example.model.*;
^
BeerSelect.java:19: cannot find symbol
symbol : class BeerExpert
location: class com.example.web.BeerSelect
BeerExpert be=new BeerExpert();
^
BeerSelect.java:19: cannot find symbol
symbol : class BeerExpert
location: class com.example.web.BeerSelect
BeerExpert be=new BeerExpert();
^
3 errors

As far as my knowledge goes the package path is correct as described in the NO 1 and N0 2 above.

Please help me out of this..

thank you.


This is a sample code from Head First Servlet and JSP, chapter 3 page 84.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You need to make sure that the directory C:\myproject\beerV1\src\ is in your classpath. The easiest way to do that is to navigate to C:\myproject\beerV1\src\ and compile/run your classes from there:


Steve
bhargav reddy vempalli
Greenhorn

Joined: Oct 09, 2011
Posts: 7
hi i have the same problem. i have tried what ever you have said but it did not work. so please tell me the following solution.
my program is

package folder1;
public class CalledProgram
{
public void calledProgramFunction()
{
System.out.println("we are in CalledProgramFunction");
}
}

package folder2;
import folder1.CalledProgram;
public class CallingProgram
{
public void callingProgramFunction()
{
System.out.println("we are in callingProgramFunction");
CalledProgram cp1=new CalledProgram();
cp1.calledProgramFunction();
}
public static void main(String args[])
{
CallingProgram cp=new CallingProgram();
cp.callingProgramFunction();
}
}

C:\scjp_practice\chapter1\accessmodifiers>javac folder2\CallingProgram.java
folder2\CallingProgram.java:2: error: package folder1 does not exist
import folder1.CalledProgram;
^
folder2\CallingProgram.java:8: error: cannot find symbol
CalledProgram cp1=new CalledProgram();
^
symbol: class CalledProgram
location: class CallingProgram
folder2\CallingProgram.java:8: error: cannot find symbol
CalledProgram cp1=new CalledProgram();
^
symbol: class CalledProgram
location: class CallingProgram
3 errors

bhargav reddy vempalli
Greenhorn

Joined: Oct 09, 2011
Posts: 7
when i tried to use -cp . it worked to compile but it is not executing. please tell me how to fix this issue
C:\scjp_practice\chapter1\accessmodifiers>javac -cp . folder2\CallingProgram.jav
a

C:\scjp_practice\chapter1\accessmodifiers>javac -cp . folder2\CallingProgram
javac: invalid flag: folder2\CallingProgram
Usage: javac <options> <source files>
use -help for a list of possible options

C:\scjp_practice\chapter1\accessmodifiers>java -cp . folder2\CallingProgram
Exception in thread "main" java.lang.NoClassDefFoundError: folder2\CallingProgra
m (wrong name: folder2/CallingProgram)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:472)


C:\scjp_practice\chapter1\accessmodifiers>cd folder3
The system cannot find the path specified.

C:\scjp_practice\chapter1\accessmodifiers>cd folder2

C:\scjp_practice\chapter1\accessmodifiers\folder2>java -cp . CallingProgram
Exception in thread "main" java.lang.NoClassDefFoundError: CallingProgram (wrong
name: folder2/CallingProgram)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:472)


C:\scjp_practice\chapter1\accessmodifiers\folder2>java CallingProgram
Error: Could not find or load main class CallingProgram
bhargav reddy vempalli
Greenhorn

Joined: Oct 09, 2011
Posts: 7
it got resolved by using -cp .

C:\scjp_practice\chapter1\accessmodifiers>java -cp . folder2.CallingProgram
we are in callingProgramFunction
we are in CalledProgramFunction
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
That Beer Expert application is supposed to be a beginner’s exercise. Delete the package names and import declarations and put everything into one folder which you use as the unnamed package.
bhargav reddy vempalli
Greenhorn

Joined: Oct 09, 2011
Posts: 7
yes you are true but i want to learn packages and access modifiers that is the reason i am using packages.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Start here.
Arvind Das
Greenhorn

Joined: May 28, 2011
Posts: 3

Yes I made that working. You have to add your ../model in your classpath and compile with "-cp" option instead of "-d". In my case it successfully worked.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Welcome to the Ranch Arvind Das
Arvind Das
Greenhorn

Joined: May 28, 2011
Posts: 3

Campbell Ritchie wrote:Welcome to the Ranch Arvind Das

Thank You Sir Ritchie Its a place where I can find all of my solutions.
Proud to be a part of it.
I have one question in mind. If I want to upload my this sample web-app on Github. Will that work? I tried it. It shows only index.html content and nothing else If i perform any action on my index.html page. Or I will have to build it. Any solutions there?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Aman Adhikari,
Your post was moved to a new topic.
Igor Dziuba
Greenhorn

Joined: May 11, 2012
Posts: 1
sammer khazi, try this:
javac -classpath c:\tomcat\lib\servlet-api.jar;classes -d classes src/com/example/web/BeerSelect.java
Somnath Guha
Greenhorn

Joined: Aug 06, 2012
Posts: 2
Igor Dziuba wrote:sammer khazi, try this:
javac -classpath c:\tomcat\lib\servlet-api.jar;classes -d classes src/com/example/web/BeerSelect.java


This has worked for me. Thanks Igor.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Welcome to JavaRanch, Somnath Guha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Package does not exist