aspose file tools*
The moose likes Beginning Java and the fly likes Unable to import a my package. 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 "Unable to import a my package." Watch "Unable to import a my package." New topic
Author

Unable to import a my package.

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

I want to import class MyB into class MyC. I get a compile time error when i attempt that. Can you help me with this ?
Here , Folder A and class MyA are inside the same folder. Packages B,C are inside Folder A as shown.The directory structure i have used :

PS : Please tell me if/how i can make this diagram better or more reader-friendly ?



SCJP 6. Learning more now.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
The diagram is pretty good, for "character graphics".

You don't show us your import statement(s). To import MyB into MyC, then MyC would need the statement:

import packageB.myB;

or

import packageB.*;

I have removed the space from your example to make an example package name; package name elements cannot have spaces in them.

rc
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Ralph Cook wrote:The diagram is pretty good, for "character graphics".

You don't show us your import statement(s). To import MyB into MyC, then MyC would need the statement:

import packageB.myB;

or

import packageB.*;

I have removed the space from your example to make an example package name; package name elements cannot have spaces in them.

rc

To remind you, folder A contains class MyA, packageB and packageC.
MyA :


MyB :


MyC:


Now if i dont import MyB into MyC, the compilation is fine and there are no problems.
Here is the error i get when i go to folder of class MyC and compile :

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

And is the folder that contains the packageB package / folder in your class path?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Rob Spoor wrote:And is the folder that contains the packageB package / folder in your class path?


What is the class path ? I only know that it was something i set in the environment variables of my computer so that i do not have to set it manually in command prompt.

Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 383

make sure folder A is in your classpath


http://plainoldjavaobject.blogspot.in
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3162
    
  10
Rahul Sudip Bose wrote:
Rob Spoor wrote:And is the folder that contains the packageB package / folder in your class path?


What is the class path ? I only know that it was something i set in the environment variables of my computer so that i do not have to set it manually in command prompt.



Once you start working on more than one project at a time, you shouldn't use the CLASSPATH environment variable because it is probably going to need a different value for each project. Use the -cp option to specify it on the command line.


Joanne
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Rahul Sudip Bose wrote:Here is the error i get when i go to folder of class MyC and compile :



I think this is your problem. You do not change directories to compile MyC -- you compile it from the folderA, and specify its path:

c:\folderA> javac packageC\MyC.java

Back when I was doing this on the command line, this was not necessary unless MyC was referenced by name (using Class.forName(), an unusual circumstance, if you haven't heard of it, don't worry about it now). I would compile the class that contained main (MyA, in your case) and the compiler would find and compile everything that got referred to automatically. I assume the compiler still works that way.

Try this: delete all the .class files that are from your classes (MyA, MyB, MyC) from all directories. Set your default directory to FolderA. Enter the command "javac MyA.java". If I'm not mistaken, it will compile all three classes, and put their class files in the same directories that hold their source files. There are compiler switches, etc., to alter that, but this would illustrate how to compile the whole project without having to execute javac on each source.

rc
Hitesh Aroraa
Greenhorn

Joined: Feb 26, 2011
Posts: 1

Kindly explain it clearly anyone on Setting folder A in Classpath.

I tried deleting the class files and compiling it again, but it's not working.

Thanks,
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Please don't ever just say "It's not working". What is it that's not working? does it give an error message? Is it the same error message? Does it crash the compiler, or the computer? what happens when you try, and, by the way, what have you tried?

rc
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
I submit the following:

C:\folderA>dir/s
Volume in drive C is Cdrive
Volume Serial Number is 84F8-438B

Directory of C:\folderA

02/26/2011 02:16 PM <DIR> .
02/26/2011 02:16 PM <DIR> ..
02/26/2011 02:15 PM 207 MyA.java
02/26/2011 02:16 PM <DIR> packageB
02/26/2011 02:16 PM <DIR> packageC
1 File(s) 207 bytes

Directory of C:\folderA\packageB

02/26/2011 02:16 PM <DIR> .
02/26/2011 02:16 PM <DIR> ..
02/26/2011 02:06 PM 129 MyB.java
1 File(s) 129 bytes

Directory of C:\folderA\packageC

02/26/2011 02:16 PM <DIR> .
02/26/2011 02:16 PM <DIR> ..
02/26/2011 02:08 PM 152 MyC.java
1 File(s) 152 bytes

Total Files Listed:
3 File(s) 488 bytes
8 Dir(s) 32,608,169,984 bytes free

C:\folderA>echo %classpath%
%classpath%

C:\folderA>rem so there's nothing in the classpath

C:\folderA>javac MyA.java

C:\folderA>dir/s *.class
Volume in drive C is Cdrive
Volume Serial Number is 84F8-438B

Directory of C:\folderA

02/26/2011 02:17 PM 481 MyA.class
1 File(s) 481 bytes

Directory of C:\folderA\packageB

02/26/2011 02:17 PM 407 MyB.class
1 File(s) 407 bytes

Directory of C:\folderA\packageC

02/26/2011 02:17 PM 408 MyC.class
1 File(s) 408 bytes

Total Files Listed:
3 File(s) 1,296 bytes
0 Dir(s) 32,608,235,520 bytes free

C:\folderA>


with the following code:





As you will notice, with my default directory set to "folderA", one javac command produced class files for all three objects.

If I had wanted or needed to compile one of the objects separately, say MyB, I could have entered:

c:\folderA> javac packageB\MyB.java

rc
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to import a my package.
 
Similar Threads
Accessspecifiers.........imp..............
Package Visibility
narrowing of type and interfaces
Accessspecifiers....................imp.....................
Extracting certain parameters from objects