Results 1 to 6 of 6

Thread: import class

  1. #1
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Question import class

    Hi,

    I have written, some small classes in java.
    Can i "import" these classes into another project ?
    so i can use them without copying them.

    thx

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    -- Classes in a package cannot make use of classes in the default (aka unnamed) package
    -- To import and use a class, it must be available on the compile-time and run-time classpath

    -- 'project' is a term related to your IDE and is not otherwise specified in Java (the JLS makes no mention of 'projects')

    db

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Dipke View Post
    I have written, some small classes in java.
    Can i "import" these classes into another project ?
    so i can use them without copying them.
    You're talking about 'projects' so I guess you're using an IDE (Eclipse, Netbeans etc). Most (all?) of them allow projects to refer to other projects so the classes in the other project are available to the first project. You still have to write an 'import' statement for those classes in the class(es) that need them.

    kind regards,

    Jos

  4. #4
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Default

    Hello,

    Project is a general name that i use for programm and is not linked to a GUI.
    I use BlueJ for writting and testing the classes.
    There i can import other classes.
    but my question is if i can do that in code.

    Like : import javax.swing.*

    thx

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Dipke View Post
    Project is a general name that i use for programm and is not linked to a GUI.
    I use BlueJ for writting and testing the classes.
    There i can import other classes.
    but my question is if i can do that in code.

    Like : import javax.swing.*
    Nobody spoke about a GUI anywhere; but if the directory where your .class files are stored in the CLASSPATH variable you can simply import them by using the 'import yourpackage.*' statement.

    kind regards,

    Jos

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by JosAH View Post
    but if the directory where your .class files are stored in the CLASSPATH variable you can simply import them by using the 'import yourpackage.*' statement.

    right. so i would create a separate path for your classes and put all your classes in there and add this path once to the CLASSPATH. all further classes added/copied later to the mentioned path will be found using 'import yourpackage.whatever' statement.

    if you want to work with your classes without copying them, then all your classes must have the same project-root-path.

Similar Threads

  1. Import
    By gothrog in forum Java Applets
    Replies: 2
    Last Post: 05-13-2010, 11:16 PM
  2. how to Import my Java Class in Jsp Page
    By pos in forum JavaServer Pages (JSP) and JSTL
    Replies: 20
    Last Post: 04-06-2010, 02:51 PM
  3. Can not import jxl.*
    By AnnieDing in forum Java Servlet
    Replies: 3
    Last Post: 01-22-2010, 03:52 PM
  4. can't import my own class
    By Splat in forum New To Java
    Replies: 5
    Last Post: 10-13-2009, 04:53 AM
  5. Replies: 4
    Last Post: 12-29-2008, 02:20 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •