Results 1 to 15 of 15
  1. #1
    ktcreator's Avatar
    ktcreator is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Question Importing Custom Classes

    So I'm a relatively new Java programmer, but I'm experienced in some other languages.

    I recently wrote a custom class for finding the slope between two points. I'd like to use the class, and I'm having trouble importing it. Whenever I move the folder and try to redo the import statement, it always says "The import [entered text] cannot be resolved." Does anyone know how to import it correctly and what directories everything should be in? Thanks.


    NOTE: This should be obvious, but I'm using Eclipse.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Did you package the class you want to import? Also, if it's a non private class in the same package you can use it without an import statement.

  3. #3
    ktcreator's Avatar
    ktcreator is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    Well the point is I want to be able to use this class over and over, so I want it to be separate.

    Yeah, there is a package with the class.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You will want to package the class with the package statement. If you store your source in the folder hierarchy of util, you would just make the first line of the source file be
    Java Code:
    package util;
    then another class can import it with
    Java Code:
    import util.ClassName;
    
    Or
    
    import util.*;
    the package statement must reflect the folder hierarchy from the class path to the location of the source file(separated by . And not /)

    Here is more helpful information at this link
    Creating and Using Packages (The Java™ Tutorials > Learning the Java Language > Packages)

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    If you want to use it over and over again in different projects then you could compile it and stick it in a jar file (Eclipse will do that for you).
    That would generally involve this class being in its own project, say a commons type project (see Apache for the same sort of thing).
    Then you include the jar file in any other projects that require that class.

    Of course, normally you'd have a suite of classes, but you can always add to the commons project later.

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    I'm not sure if this is the "right" way to do it -- in fact, I suspect it isn't -- but I have the utility classes I've written in their own Eclipse project, in a package called krum.util. If I refer to one of those classes from another project, Eclipse offers to "fix" my project setup, and sets up a reference from the project I'm working on to my library project. I'm sure there's an option somewhere in the project menus to reference another project like that, but I don't know where it is.
    Get in the habit of using standard Java naming conventions!

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    I'd build your utils project into a jar file and simply dump a copy of that into each project.
    That may be what Eclipse is doing under the hood when you reference other projects, but I tend to prefer doing these things manually.

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

    Default

    Quote Originally Posted by Tolls View Post
    I'd build your utils project into a jar file and simply dump a copy of that into each project.
    That may be what Eclipse is doing under the hood when you reference other projects, but I tend to prefer doing these things manually.
    There is no need to duplicate your .jar file over and over again (maintenance nightmares are waiting ...); simply make Eclipse refer to your .jar (that is stored somewhere once). Also, as kjkrum wrote, make a project refer to another project; all Eclipse does it fiddle diddle a bit with the classpath. I like the second method because Eclipse will be able to debug through all the source code (it can't do that with just a .jar file with the utility code).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    There is no need to duplicate your .jar file over and over again (maintenance nightmares are waiting ...); simply make Eclipse refer to your .jar (that is stored somewhere once). Also, as kjkrum wrote, make a project refer to another project; all Eclipse does it fiddle diddle a bit with the classpath. I like the second method because Eclipse will be able to debug through all the source code (it can't do that with just a .jar file with the utility code).

    kind regards,

    Jos
    So you recommend upgrading all projects to a new version of something straight away, with no regression testing?

    ;)

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

    Default

    Quote Originally Posted by Tolls View Post
    So you recommend upgrading all projects to a new version of something straight away, with no regression testing?

    ;)
    Yep, I like living close to the edge ;-) and I don't like legacy stuff, i.e. if something needs to be updated I want to do it immediately, besides that, old .jar files smell funny.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    I like pickled stuff...

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

    Default

    Quote Originally Posted by Tolls View Post
    I like pickled stuff...
    Wild pickles (with fangs)? Or those domesticated lazy, fat belly, toothless, pickles?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    ktcreator's Avatar
    ktcreator is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    Okay, so how do I convert it into a .jar file? I tried something but it didn't seem to let me, or do anything with it.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    Wild pickles (with fangs)? Or those domesticated lazy, fat belly, toothless, pickles?

    kind regards,

    Jos
    Mother-in-law does the pickling of our homegrown shallots...god knows what she puts in, but I expect booze is involved.

    Quote Originally Posted by ktcreator View Post
    Okay, so how do I convert it into a .jar file? I tried something but it didn't seem to let me, or do anything with it.
    Eclipse has some jarring thing. Is it part of the project output? I can't remember, but a quick read through of the docs should tell you where to look.

    Oh, and "I tried something but it didn't seem to let me" tells us nothing you know.

  15. #15
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    In Eclipse, go to File -> Export and select Jar or Runnable Jar from the Java folder.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. importing classes
    By javajames in forum New To Java
    Replies: 6
    Last Post: 05-24-2011, 09:21 AM
  2. Importing custom class
    By BigRed in forum Eclipse
    Replies: 2
    Last Post: 02-14-2009, 02:12 PM
  3. Need help with importing classes
    By Deathmonger in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 10:03 AM
  4. Importing classes
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-06-2007, 02:27 PM
  5. Importing a Custom Class
    By jfredrickson in forum New To Java
    Replies: 3
    Last Post: 07-11-2007, 11:23 AM

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
  •