Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: How to use another package's classes?

  1. #1
    Junior Member
    Join Date
    Jun 2020
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to use another package's classes?

    So I am working on a lemmatizer, currently, so I have one package where I will store classes corresponding to parts of speech, e.g. Noun, Verb and what not. And then in another package I will have my class that will contain main.

    I would like for my main class to be able to access any of the classes within the parts of speech package. W3 schools is a dirty liar, because they said using the keyword "class" by default allows said class to be used by other classes in the same package. But no, I have "class Noun", so default class, but main, in another package, can't make a Noun object.

    And I use Eclipse, and Eclipse has this feature "Organize Imports" where it will apparently import packages for you. But when I click on it, or use the shortcut ctrl + shift + o, literally nothing happens. No dialogue window comes up, nothing. So I clearly need to do it the manual way.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,186
    Thanks
    53
    Thanked 2,465 Times in 2,416 Posts

    Default Re: How to use another package's classes?

    There are a couple of ways to write code that use classes from another package:
    one is to have import statements that give the compiler the path to where the class file is located:
    import java.util.ArrayList;

    another is to code the full package name with the class name when referring to the class:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));

    For the compiler to find the class, its package must be on the classpath. The classpath is set for the javac command by using the -cp option.


    Or is this a question on how to configure your IDE?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2020
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to use another package's classes?

    How do I figure out the path where my class file is located? That is, in such a way that it can be imported? Yeah, I know where the physical .class file is, but what am I supposed to do, say import C:/users/eclipse/src/bin etc?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,186
    Thanks
    53
    Thanked 2,465 Times in 2,416 Posts

    Default Re: How to use another package's classes?

    If the full classname with package is mypackage.MyClass
    and the file MyClass.class is in the mypackage folder
    then the classpath for javac must point to the folder that contains the mypackage folder.
    For example if the path to the class file was: C:/users/eclipse/src/mypackage/MyClass.class
    then the classpath for the javac program would be: -cp C:/users/eclipse/src

    Your code would have the statement: import mypackage.MyClass;
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2020
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to use another package's classes?

    I tried both import parts_of_speech and parts_of_speech.Noun (which I don't want to do, I want to import every single class within the whole package) and nothing worked. Within my eclipse folder, I have a folder that is the name of my Java Project, within that there is a src file, then within that are my two packages.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,186
    Thanks
    53
    Thanked 2,465 Times in 2,416 Posts

    Default Re: How to use another package's classes?

    Can you post the full path to your classes
    and the full name of the class with the package name?
    Also post the value of the classpath passed to the javac command (on the commandline following the -cp)
    also the current directory where the javac command was executed?

    The classpath passed to the javac command needs to connect to the package folder.
    For example if the path to the class file was: C:/users/eclipse/src/mypackage/MyClass.class
    and the current directory where the javac command was executed was: C:/users/eclipse/
    then the -cp needs to be -cp src because the mypackage folder is in the src folder.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2020
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to use another package's classes?

    Path to main class: C:\Users\(my name)\eclipse-workspace\Lemmatizer Prototype\src\main\main.java
    Path to Noun class: C:\Users\(my name)\eclipse-workspace\Lemmatizer Prototype\src\oarts_of_speech\Noun.java

    I have no clue what any of that javac/cp stuff even is (I use eclipse, not the command line, so I never have a reason to use javac), so I am afraid I cannot provide that.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,186
    Thanks
    53
    Thanked 2,465 Times in 2,416 Posts

    Default Re: How to use another package's classes?

    Sorry, I do not know how to configure Eclipse so it will set the correct classpath when it compiles a java program.

    Note: The paths you posted were to the source files - have an extension of .java
    The class files I was asking about have an extension of .class.

    Also you didn't show what the full names of your classes are. The full name includes the package.
    For example for the Arrays class, the full name is java.util.Arrays. I referenced that in post#2
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2020
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to use another package's classes?

    Perhaps I have too many gaps in my information to be worrying about this. I will end the thread here.

Similar Threads

  1. how to import/ access classes created in one package in another package
    By ydandurkar in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 11th, 2014, 11:12 AM
  2. Replies: 0
    Last Post: February 1st, 2013, 12:25 PM
  3. Replies: 4
    Last Post: November 8th, 2012, 07:55 AM
  4. Replies: 1
    Last Post: August 5th, 2012, 05:01 AM
  5. Simple package problem, package does not exist error
    By Farmer in forum Object Oriented Programming
    Replies: 3
    Last Post: August 23rd, 2011, 11:18 AM