File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Need Some Help with Packages in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need Some Help with Packages in Java" Watch "Need Some Help with Packages in Java" New topic
Author

Need Some Help with Packages in Java

Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 78
Directory :- C/Desktop/Java Exercises/abc/A.java


Directory :- C/Desktop/Java Exercises/abc/B.java


Directory :- C/Desktop/Java Exercises/abc/Test.java



I have put all three Java files in one folder called abc. Should the folder name and package name be equal ?
Anyway I have named my packages as abc too.
But when Test.java file is compiled it still shows some errors and I am confused.
------------------------------------------------------
Errors shown by the cmd

Test.java:1: error: package abc does not exist
import abc.*;
^
Test.java:4: error: cannot access A
A a1=new A();
^
bad source file: .\A.java
file does not contain class A
Please remove or make sure it appears in the correct subdirectory of the sou
rcepath.
2 errors

C:\Users\User\Desktop\Java Exercises\abc>javac Test.java
Test.java:3: error: cannot access A
A a1=new A();
^
bad source file: .\A.java
file does not contain class A
Please remove or make sure it appears in the correct subdirectory of the sou
rcepath.
1 error
-----------------------------------------------

Please help me to compile the above programs successfully.

Actually I am still struggling to understand this package concept clearly. I went through articles posted by Oracle. But I guess it did not help me to clarify all my doubts in packages. Please help me with these sample programs.
Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Move to the Exercises folder, then try javac abc/A.java (etc)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

Test.java is missing a package abc; statement.

Or, if you don't want Test to be in the package abc, you have to move it up one directory, into the directory C:\Desktop\Java Exercises

As Campbell already suggested, compile and run from the C:\Desktop\Java Exercises directory; not from the C:\Desktop\Java Exercises\abc directory.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 78
Jesper de Jong wrote:Test.java is missing a package abc; statement.

Or, if you don't want Test to be in the package abc, you have to move it up one directory, into the directory C:\Desktop\Java Exercises

As Campbell already suggested, compile and run from the C:\Desktop\Java Exercises directory; not from the C:\Desktop\Java Exercises\abc directory.

As you guys already suggested I moved Test.java file up one directory.
Then I compiled Test.java file
The errors cmd shows now are

----------------
Testt.java:3: error: cannot find symbol
A a1=new A();
^
symbol: class A
location: class Testt
Testt.java:3: error: cannot find symbol
A a1=new A();
^
symbol: class A
location: class Testt
Testt.java:5: error: cannot find symbol
B b1=new B();
^
symbol: class B
location: class Testt
Testt.java:5: error: cannot find symbol
B b1=new B();
^
symbol: class B
location: class Testt
4 errors
------------------------------------------
Why are they shown now ?
Can not main class and other Java files be in the same directory?
I am grateful if anyone could provide me a precise solution.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

In class Test, (or is it now Testt?) you need to import classes A and B from package abc.

import abc.A;
import abc.B;
Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 78
Jesper de Jong wrote:In class Test, (or is it now Testt?) you need to import classes A and B from package abc.

import abc.A;
import abc.B;

Yes Boss !!! It works :-) . Thank you very much. (Yep it is Testt. Because there were some Test.java files in that directory. So I had to make it Testt.)
Now can anybody tell me "Is not is possible to keep main class Java files and other Java files in one folder/directory?"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Gihan Madushanka wrote: . . . "Is not is possible to keep main class Java files and other Java files in one folder/directory?"
Yes, of course it is. But your package declarations and the directory structure must correspond to one another.

I have got rid of the red text, because many people find coloured text difficult to read.
Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 78
Campbell Ritchie wrote:
Gihan Madushanka wrote: . . . "Is not is possible to keep main class Java files and other Java files in one folder/directory?"
Yes, of course it is. But your package declarations and the directory structure must correspond to one another.

I have got rid of the red text, because many people find coloured text difficult to read.

Sorry for making them red.
Can you further explain it please ?

You can use below example

directory - C/desktop/JavaExercises/x





directory - C/desktop/JavaExercises/x


What are the changes that I should make for the above code to compile successfully. They are all in one package
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

You would not need to change anything in the code. Just compile it from the right place, the directory C:\desktop\JavaExercises, which is the base directory of the package named x.


C:\desktop\JavaExercises> javac x\Test.java
C:\desktop\JavaExercises> java x.Test
Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 78
Jesper de Jong wrote:You would not need to change anything in the code. Just compile it from the right place, the directory C:\desktop\JavaExercises, which is the base directory of the package named x.


C:\desktop\JavaExercises> javac x\Test.java
C:\desktop\JavaExercises> java x.Test

Yes it is correct. Thanks for making me understand about packages. Thanks a lot.
Many thanks for everyone who did not hesitate to comment on this topic.
Cheers!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
You’re welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need Some Help with Packages in Java