Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How to use package?

 
Ranch Hand
Posts: 44
Firefox Browser VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a package structure like this
com(package)
first.java
com/in(package)
second.java
Third.java


I want to use a method from second.java in both third.java and also first.java.

Hoe to do this?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://download.oracle.com/javase/tutorial/java/package/usepkgs.html
 
manikandan jayakumar
Ranch Hand
Posts: 44
Firefox Browser VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi that link didn't provided me what i want.
that tutorial provided only the basics how to import package etc..

com(package)
first.java
com/in(package)
second.java
Third.java

I want to access a method from second.java in third.java

usually we will import the package
but in my case how to import?

'm not able to import directly like

import com.in.*;
or
import com.*;

and also in first.java from second.java
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

manikandan jayakumar wrote:
usually we will import the package


we import classes not packages

manikandan jayakumar wrote:
but in my case how to import?


1.first the method should be public(assume the is no IS-A relationship)
2.import the class which contains the method
3.create an instance of that class and call the method(if the method static then call it using Class name, object creation is not required)
 
manikandan jayakumar
Ranch Hand
Posts: 44
Firefox Browser VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These are my files
exc.java (pack/)


hai.java

able to execute exc.java but while executing hai.java Error:cannot find the symbol class exc
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how are you executing ? can you post your command with path location? probably, might be classpath issue!
 
manikandan jayakumar
Ranch Hand
Posts: 44
Firefox Browser VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should I set CLASSPATH?
If so where should it point to?
 
manikandan jayakumar
Ranch Hand
Posts: 44
Firefox Browser VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as usual
C:\Java\pack>javac exc.java
C:\Java\pack>javac hai.java
files are in c:>Java>pack
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
remove import pack.exc from hai.java; because both classes are in pack . it was searching one more pack folder inside the pack, hence the error!
 
manikandan jayakumar
Ranch Hand
Posts: 44
Firefox Browser VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same Error without import statement also
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm, but remeber I always use *javac package\classname.java* and java package.classname . so, I suggest you to use as in
C:\Java>javac pack\hai.java insteadof C:\Java\pack>javac hai.java
 
manikandan jayakumar
Ranch Hand
Posts: 44
Firefox Browser VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah it works.
Thank you for your valuable time.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
set classpath=%classpath%;.;<path of your package>
you need to specify the path where your package
example
if package exist in d:\javaprg\pack\Sample.class
where pack is package which has sample.class

set classpath=%classpath%;.;d:\javaprg;
you need to specify the path where your package

or you can also specify the package path in the environment variable classpath

right click mycomputer- properties-advance-environment variable

under the heading system variable create environment variable classpath
name - classpath
value- %classpath%;.;d:\javaprg;

Hope it works


 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome
 
manikandan jayakumar
Ranch Hand
Posts: 44
Firefox Browser VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more doubt can you rectify it?
If pack is a subPackage of com and while using pack's class in com should use any import statement?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

manikandan jayakumar wrote:If pack is a subPackage of com and while using pack's class in com should use any import statement?


Yes, if both classes in a same package then no need to mention package[you can also mention-optional] else you need to mention package[import]
 
manikandan jayakumar
Ranch Hand
Posts: 44
Firefox Browser VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hy dude thanks a lot for this help.
I came know many things with this site.
Thanks a lot lot lot lot



All the Happy Emoticons!!!
 
Marshal
Posts: 74390
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are several different ways to get access to a class:
  • 1: Use a class in the same package.
  • 2: Use a class in the java.lang package.
  • 3: Import the class by name.
  • 4: Import any classes in the package, used as required, with the import foo.bar.*; syntax
  • 5: Use the fully-qualified name of the class.

  • Try to avoid using no 4; no 3 is a better way to import classes.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic