File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Some Help with Packages in Java

 
Gihan Madushanka
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Move to the Exercises folder, then try javac abc/A.java (etc)
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic