• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

How to import classes in a subpackage from another sub-package?

 
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a problem regarding importing subpackages. Since I was not doing any serious project, the names are all funny.

My folder structure is like this : a folder called body. This has 3 sub-folders,left,right and centre. Left has folders hand and leg.

There are other sub-folders in the other 2 folders as well. Each folder has 1 class, TestXXXX, where XXXX is name of folder (eg. hand folder has class TestHand) and the class has only 1 method, showXXXX (eg. showHand() method in TestHand class).

The code for 1 class is like this :


The codes in other classes are similar. What I am trying to do is, access method showLeg() of TestLeg class from the hand folder using a class Test, which looks like this :


When I try to compile class Test going inside the folder hand, I get the following error : package body.left.leg does not exist

However, instead of using the Test class in the hand folder, if I use it in the parent folder that contains the body folder, I am able to access all classes anywhere in the directory structure. In other words, I cannot access a sub-package from another sub-package, but I can access any sub-package from the main package.

Can you please help me with this?

P.S. - I already posted this on an existing thread, but got no reply. So re-posting it here.
 
Greenhorn
Posts: 17
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try using -classpath switch on javac command and in that set classpath as absolute path to your body folder
 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry...I didnt get that completely. Can you please explain in detail?
also, is there any other way to do this? Without touching the classpath? My classpath is set to the bin of the jdk
 
Abhijeet Sutar
Greenhorn
Posts: 17
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compiling the file which is in package that requires class in the another package , you need to specify the classpath that is root of package directory
In this case, if you want compile class Test which is in directory,say. body/left/leg ,and this Test class requires another class i.e. TestHand, So you must specify classpath to TestHand for compiling the Test class + in Test class. you must specify package declaration .

Here is the program and command line invokation





Hope this helps
 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used those command line arguments. Compiling never had any issues. I have the class files. But it is crashing at runtime. When I try to run, it fails and gives the error.
 
Abhijeet Sutar
Greenhorn
Posts: 17
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Souvvik Basu wrote:
When I try to compile class Test going inside the folder hand, I get the following error : package body.left.leg does not exist


look for above post

Souvvik Basu wrote:
But it is crashing at runtime. When I try to run, it fails and gives the error.



TellTheDetails ...try to give more details about the runtime error and your command line invocation of java command
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Abhijitsutar. You uncover my question  about the referation in java infrustructure.
 
Marshal
Posts: 76447
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Old posts can still be useful.
 
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic