File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem using package and import Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem using package and import" Watch "Problem using package and import" New topic
Author

Problem using package and import

Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

Hello java ranch members please help on the following java code
the question is from Sun's pre-assesment test for scjp5.0

PkgAccess.java file source code





PkgAccess2.java file source code



when PkgAccess2.java is compiled
I got following error message from compiler

F:\PkgAccess2.java:3: cannot access PkgAccess
bad class file: .\PkgAccess.java
file does not contain class PkgAccess
Please remove or make sure it appears in the correct subdirectory of the classpath.
int x1 = PkgAccess.tiger;
^
1 error


why this error occurs
Regards
Ninad

[ September 18, 2008: Message edited by: Ninad Kulkarni ]

[ September 18, 2008: Message edited by: Ninad Kulkarni ]
[ September 18, 2008: Message edited by: Ninad Kulkarni ]

SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
Hi,

The static import stmt will import all the static memebers of the class PkgAccess not the class itself. Here in this case, it imports the static variable tiger only.
So the class PkgAccess2 cannot find a class with name PkgAccess.
To be able to compile, you can add import com.sun.PkgAccess;

The stmt int x4 = sun.PkgAccess.tiger; also doesnt compile as there is no class with the full name sun.PkgAccess.

Hope this is clear!


Thanks,<br />Srilatha M
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

Hello Srilatha M

Thanks

Regards
Ninad
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

Hello java ranch members

I modified original java code of Sun's pre-assesment test
and try to compile it

the modified PkgAccess2.java file source code is as follows

[CODE[B]]//import com.sun.PkgAccess;[/B] [B]//when this line is uncommented code compiles just fine[/B]
[B]import com.sun.*;[/B]
//import static com.sun.PkgAccess.*;
public class PkgAccess2 {
//int x1 = PkgAccess.tiger;
//int x2 = tiger;
int x3 = com.sun.PkgAccess.tiger;
[B]PkgAccess pa = new PkgAccess();[/B]
//int x4 = sun.PkgAccess.tiger;
}[/CODE]



now compiler gives following error message
F:\PkgAccess2.java:8: cannot access PkgAccess
bad class file: .\PkgAccess.java
file does not contain class PkgAccess
Please remove or make sure it appears in the correct subdirectory of the classpath.
PkgAccess pa = new PkgAccess();
^


Why this happens?
Regards
Ninad

[ September 18, 2008: Message edited by: Ninad Kulkarni ]

[ September 18, 2008: Message edited by: Ninad Kulkarni ]

[ September 18, 2008: Message edited by: Ninad Kulkarni ]
[ September 18, 2008: Message edited by: Ninad Kulkarni ]
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
Check whether the class file for PkgAccess is located in com/sun directory!
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

Originally posted by M SRILATHA:
Check whether the class file for PkgAccess is located in com/sun directory!


Yes PkgAccess is located in com/sun
and I compiled PkgAccess.java using following option
javac -d [current directory] [source file path]

But this situation is not arised when using package java.io.* in some other code but it happens when we create package structure and then importing in other .java source file



Regards
Ninad
[ September 18, 2008: Message edited by: Ninad Kulkarni ]
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
Its compiling properly for me.
Let me explain once how i did.

you have a source code file PkgAccess.java in com\sun directory.
compile that class in the com\sun directory itself. now PkgAccess.class file will be created in com\sun directory.

Then you have another source file PkgAccess2.java which uses PkgAccess class with the use of import stmt import com.sun.PkgAccess;
I have put the file PkgAccess2.java in the directory in which com directory is located.
And compiled using javac PkgAccess2.java


It worked. Hope this will help!
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

Originally posted by M SRILATHA:
Its compiling properly for me.
Let me explain once how i did.

you have a source code file PkgAccess.java in com\sun directory.
compile that class in the com\sun directory itself. now PkgAccess.class file will be created in com\sun directory.

Then you have another source file PkgAccess2.java which uses PkgAccess class with the use of import stmt import com.sun.PkgAccess;
I have put the file PkgAccess2.java in the directory in which com directory is located.
And compiled using javac PkgAccess2.java


It worked. Hope this will help!



As you said it is ok if I remove the comment of first line
com\sun has PkgAccess.class if you put source file in that directory
but the binary name of that class is only com.sun.PkgAccess.class it is not PkgAccess.class
because when you run PkgAccess class from com/sun direcory
jvm throws error java.lang.NoClassDefFoundError and said that
wrong name:com/sun/PkgAccess


you have to run it from current_directory with name of com.sun.PkgAccess

But my question is using javac -d option
I compiled it using that option
and another thing if you put PkgAccess.java in com/sun and use javac -d option then directory structure created is as follows


current_directory\com\sun\com\sun\PkgAccess.class

so when you run program from current_directory and use com.sun.PkgAccess name for that class it cannot run because jvm simply looking for com directory for com.sun.PkgAccess class and it is not found in com/sun directory and throws error
like java.lang.NoClassDefFoundError

because com/sun has again com directory
and com has again sun directory
and sun has PkgAccess.class

like following directory structure com\sun\com\sun\PkgAccess.class


my doubt is
by using statement import com.sun.PkgAccess; works fine it compiles but
by using statement import com.sun.*; in PkgAccess2.java
It is not compiled why this import on demand statement not works as like java.io.* it workes fine in java source file.


Regards
Ninad

[ September 18, 2008: Message edited by: Ninad Kulkarni ]

[ September 18, 2008: Message edited by: Ninad Kulkarni ]
[ September 18, 2008: Message edited by: Ninad Kulkarni ]
sannuth kashikar
Greenhorn

Joined: Sep 16, 2008
Posts: 14
Remove PkgAccess from 3rd line only tiger as 4th statement and add com. as in 5th statement will compile.
static import is used to import static members of a class.

import static com.sun.PkgAccess.*; // import static members of class PkgAccess
public class PkgAccess2 {
int x1 = PkgAccess.tiger; //is a different package which
int x2 = tiger; //ok since your have imported in 1st statement
int x3 = com.sun.PkgAccess.tiger; //ok , full package name
int x4 = sun.PkgAccess.tiger; // invalid ,not a complete package name
}


scjp5 90%
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Hmmm...Seems like Ninad likes to edit his posts


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

Originally posted by sannuth kashikar:
Remove PkgAccess from 3rd line only tiger as 4th statement and add com. as in 5th statement will compile.
static import is used to import static members of a class.

import static com.sun.PkgAccess.*; // import static members of class PkgAccess
public class PkgAccess2 {
int x1 = PkgAccess.tiger; //is a different package which
int x2 = tiger; //ok since your have imported in 1st statement
int x3 = com.sun.PkgAccess.tiger; //ok , full package name
int x4 = sun.PkgAccess.tiger; // invalid ,not a complete package name
}



Thanks to all for reply and explaination
but my doubt is working of import on demand for com.sun.*; only
all other things are ok single import works fine



Regards
Ninad
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

Originally posted by Ninad Kulkarni:



but my doubt is working of import on demand for com.sun.*; only
all other things are ok single import works fine



Regards
Ninad


Please Help me on this problem

Regards
Ninad
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Can you please archive your directory of files related to this question and upload it on rapidshare and provide a link here.

Your problem seems to be unique. Single import is working but * import is not working. Seeing the whole code and directory structure will help solve the problem....
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

yes Ankit I will upload it that may solve the problem

Regards
Ninad
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

Originally posted by Ninad Kulkarni:
yes Ankit I will upload it that may solve the problem

Regards
Ninad


The link is as follows

http://rapidshare.com/files/146525440/current_directory.rar.html

current_directory is working directory from where we use javac -d option so that following structure created

current_directory\com\sun\PkgAccess.class
and source file is in following directory

current_directory\PkgAccess.java
current_directory\PkgAccess2.java

compile from current_directory using javac -d . PkgAccess.java option
from current_directory

comment first line of statement then compile
and use second line import on demand that is
com.sun.*;

Regards
Ninad
[ September 19, 2008: Message edited by: Ninad Kulkarni ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

your directory structure is wrong...

correct structure



To get this structure, create the appropriate directories and put the .java files in place. Then move to current_directory and use the command javac com/sun/PkgAccess.java, then use the command javac PkgAccess2.java

an alternate way is the one you are doing.



You are using the command javac -d . PkgAccess.java then you are using the command javac PkgAccess2.java.

But the error is occurring because when you compile PkgAccess2.java, then the compiler finds PkgAccess.java in the current directory and says that it is in the wrong directory.

I don't know why it gives error when the directory structure is like your directory structure. I can't say why the compiler is behaving this way. If you remove PkgAccess.java from the current directory after compiling it and then compile PkgAccess2.java, it works fine....
[ September 19, 2008: Message edited by: Ankit Garg ]
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
Hi Ninad,

Now i am also getting the same compiler error

can anyone explain why the compilation fails?
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

Originally posted by Ankit Garg:
your directory structure is wrong...

correct structure



To get this structure, create the appropriate directories and put the .java files in place. Then move to current_directory and use the command javac com/sun/PkgAccess.java, then use the command javac PkgAccess2.java

an alternate way is the one you are doing.



You are using the command javac -d . PkgAccess.java then you are using the command javac PkgAccess2.java.

But the error is occurring because when you compile PkgAccess2.java, then the compiler finds PkgAccess.java in the current directory and says that it is in the wrong directory.

I don't know why it gives error when the directory structure is like your directory structure. I can't say why the compiler is behaving this way. If you remove PkgAccess.java from the current directory after compiling it and then compile PkgAccess2.java, it works fine....

[ September 19, 2008: Message edited by: Ankit Garg ]


Hello Ankit
Thanks for reply


Regards
Ninad
[ September 19, 2008: Message edited by: Ninad Kulkarni ]
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

Yes Ankit you are also correct after compilation removing PkgAccess.java
PkgAccess2.java compiles fine

Yes it works but why this happens? I really don't know
can anybody focus on this?

Regards
Ninad
[ September 19, 2008: Message edited by: Ninad Kulkarni ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Ninad I think this is the first time you are using any forum..

Please don't quote these long posts. If you want to reply to a person, then you can use @ followed by the name of the person to whom you want to give the message like @ninad....On this thread you one quoted your own post...

also write what you have to write in one post. You write more than one posts yourself concurrently, that is irritating....

And please if you edit a message more than once, remove the extra editing information from the bottom...I don't know if it is fair or not, but it makes your post look better....
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

Thanks to Ankit

Yes I am new to forum I don't know details of forum rules
I will definitely consider your suggestions

Regards
Ninad
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Ninad,
I too faced the same problem sometimes back and came to know that if you import something like import com.sun.*. And if you make use of any of the classes in that package then java compiler will look for a match of the class name(here PkgAccess) with both .java and .class files whichever it finds early it carry along with that. Here is the same case, its finding PkgAccess.java file before PkgAccess.class. Thus giving you the error file does not contain class PkgAccess. I too was mad at this and googled a lot .
You can refer to this link i had bookmarked.
Link

Hope this will help
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

Thanks a lot Pranav Bhatt

Now I very much clear about package and import

Hello M SRILATHA and Ankit Garg
if you are still not clear about this problem

You can refer to this link as given below

http://forums.sun.com/thread.jspa?messageID=9914393

Thanks to All members of javaranch

Best Regards
Ninad
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem using package and import