Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to import a my package.

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?

 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And is the folder that contains the packageB package / folder in your class path?
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
make sure folder A is in your classpath
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
Eclipse IDE Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic