This discussion is archived
6 Replies Latest reply: Mar 19, 2013 4:03 PM by rp0428 RSS

Package Importing Help

997174 Newbie
Currently Being Moderated
Hi friends i need a lil help.

How can i import a package from a back folder.

Example:

I have a folder named " project "
in " project " folder i have two package folders name named " a " and " b "

now i want to import package of " b " folder into the package of " a " folder ..is there any way?
  • 1. Re: Package Importing Help
    gimbal2 Guru
    Currently Being Moderated
    Yes, that's exactly what the classpath and package system of Java is for. Assuming the 'project' folder is on the classpath, you can simply do import a; or import b; in any class.

    You most likely need to research the classpath more, if you knew how it worked you wouldn't have to ask this.
  • 2. Re: Package Importing Help
    997174 Newbie
    Currently Being Moderated
    Dear yes we can import package with import a.*;

    import a.*; <Thats mean we are importing all class containing in a folder

    and we need a java file outer of the " a " folder to import package " a "

    but my question is that if we have to different package named " a " and " b "

    that's mean " a " and " b " is two different folder

    if we have a java file " test.java " outside of " a " and " b " folder then we can import packages by import a.*; and import b.*;

    but i want to call package " a " from package "b" so can

    So can i write import a.*; in the java file " b.java " contain inside " b " folder package?
  • 3. Re: Package Importing Help
    gimbal2 Guru
    Currently Being Moderated
    Don't just repeat the same question with more noise added to it, I gave you the answer. Research the classpath and all will become clear. Until then you're only asking "is this possible?" without going to look first yourself.
  • 4. Re: Package Importing Help
    997174 Newbie
    Currently Being Moderated
    Whats the oracle forum for? am not i trying to search via this forum? why you people don't answered directly?

    I searching for this from more then 3 hours...in google docs...oracle docs etc...but no such method for importing package from another package .... So i post here the problem.....
    And now you again saying me to search again??

    If you don't know the method then just stop replying.....

    Some one said me that i can do it by import ("../packagename/calssname");

    but its also error.....
  • 5. Re: Package Importing Help
    PhHein Guru Moderator
    Currently Being Moderated
    OK, here is the answer straight forward: adjust your classpath!
  • 6. Re: Package Importing Help
    rp0428 Guru
    Currently Being Moderated
    >
    How can i import a package from a back folder.
    >
    I've never heard of a 'back folder'.
    >
    Example:

    I have a folder named " project "
    in " project " folder i have two package folders name named " a " and " b "

    now i want to import package of " b " folder into the package of " a " folder ..is there any way?
    >
    For examples of how to import and how to work with packages see 'Using Package Members' in The Java Tutorials
    http://docs.oracle.com/javase/tutorial/java/package/usepkgs.html
    >
    Importing a Package Member
    To import a specific member into the current file, put an import statement at the beginning of the file before any type definitions but after the package statement, if there is one. Here's how you would import the Rectangle class from the graphics package created in the previous section.

    import graphics.Rectangle;
    Now you can refer to the Rectangle class by its simple name.

    Rectangle myRectangle = new Rectangle();
    This approach works well if you use just a few members from the graphics package. But if you use many types from a package, you should import the entire package.

    Importing an Entire Package
    To import all the types contained in a particular package, use the import statement with the asterisk (*) wildcard character.

    import graphics.*;
    Now you can refer to any class or interface in the graphics package by its simple name.

    Circle myCircle = new Circle();
    Rectangle myRectangle = new Rectangle();
    The asterisk in the import statement can be used only to specify all the classes within a package, as shown here. It cannot be used to match a subset of the classes in a package. For example, the following does not match all the classes in the graphics package that begin with A.

    // does not work
    import graphics.A*;
    Instead, it generates a compiler error. With the import statement, you generally import only a single package member or an entire package.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points