Results 1 to 19 of 19
  1. #1
    dawiz001 is offline Member
    Join Date
    Feb 2009
    Posts
    21
    Rep Power
    0

    Angry Creating and Using Packages

    I'm trying to create and use a package. I create the package but when I import it in a differnt file and compile I get an error saying package not found. What could be the problem here?

  2. #2
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Could you show us the code please (in CODE tags).
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    what does your directory structure look like?

  4. #4
    dawiz001 is offline Member
    Join Date
    Feb 2009
    Posts
    21
    Rep Power
    0

    Default

    directory structure; C:\java\<filename>
    I used inside the progam; package polymorphism.gui;
    the class file was created inside gui, however when I try to import in a new file; import polymorphism.gui.<publicClassName>; I got a message saying package not found

  5. #5
    dawiz001 is offline Member
    Join Date
    Feb 2009
    Posts
    21
    Rep Power
    0

    Default

    Also I did set the environment variable in Windows: CLASSPATH C:\java\polymorphism\gui;

  6. #6
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    1. Was the package declaration the first thing in your program? (besides comments)
    also, 2. is the class you import also declared properly in the package? and 3. Why are you importing from polymorphism.gui when the class is in the polymorphism.gui package? That's unnecessary (unless I misunderstood)
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  7. #7
    dawiz001 is offline Member
    Join Date
    Feb 2009
    Posts
    21
    Rep Power
    0

    Default

    1. Yes the package declaration was the first thing in my program; 2. The class was declared properly in the package...I know this because when I merge the actual class in the next file I'm working with, it worked ok; 3. The class itself is not in polymorphism.gui package...I was referring to the .class file

  8. #8
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    .. is the class you are importing public?
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  9. #9
    dawiz001 is offline Member
    Join Date
    Feb 2009
    Posts
    21
    Rep Power
    0

    Default

    Yes MK12 the class I'm trying to import is public...

  10. #10
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    exactly what are the paths of your files? i'm still confused. your actual (relevant) code and actual file locations are more useful than trying to describe the trouble you're having

  11. #11
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    and also, why don't you do a simple test to see if your package class path is working correctly.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  12. #12
    dawiz001 is offline Member
    Join Date
    Feb 2009
    Posts
    21
    Rep Power
    0

    Default

    well that's exactly the problem I'm having with the package path...I know my code is working well and I also know that the .class file is located where it is suppose to be...I just can't import it back and get it to work...

  13. #13
    dawiz001 is offline Member
    Join Date
    Feb 2009
    Posts
    21
    Rep Power
    0

    Default

    package polymorphism.music;
    import javax.swing.*;

    public class SwingConsole{
    public static void
    run(final JFrame f, final int width, final int height){
    SwingUtilities.invokeLater(new Runnable(){
    public void run(){
    f.setTitle(f.getClass().getSimpleName());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(width, height);
    f.setVisible(true);
    }//end run()
    });//end SwingUtilities
    }//close overloaded run()
    }//end public class

  14. #14
    dawiz001 is offline Member
    Join Date
    Feb 2009
    Posts
    21
    Rep Power
    0

    Default

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import polymorphism.music;

    public class Button1 extends JFrame{
    private JButton
    b1 = new JButton("Button 1"),
    b2 = new JButton("Button 2"),
    b3 = new JButton("Button 3");
    private JTextField txt = new JTextField(10);
    private ActionListener bl = new ActionListener(){
    public void actionPerformed(ActionEvent e){
    String name = ((JButton)e.getSource()).getText();
    txt.setText(name);
    }
    };//end ActionListener
    public Button1(){
    b1.addActionListener(bl);
    b2.addActionListener(bl);
    b3.addActionListener(bl);
    setLayout(new FlowLayout());
    add(b1);
    add(b2);
    add(b3);
    add(txt);
    }//end constructor
    public static void main(String[] args){
    run(new Button1(), 300, 100);
    }//end main()
    }//end public class

  15. #15
    dawiz001 is offline Member
    Join Date
    Feb 2009
    Posts
    21
    Rep Power
    0

    Default

    that's basically where I reach but i can't seem to get my .class file back from the package...

  16. #16
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    the above you said polymorphism.gui, but now you are using polymorphism.music?

    well... try this:
    1) set your classpath to classpath=.;C:\java
    2) make sure you have c:\java\polymorphism\music
    3) and put the .class file in the music folder.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  17. #17
    dawiz001 is offline Member
    Join Date
    Feb 2009
    Posts
    21
    Rep Power
    0

    Default

    I think that's where I'm going wrong...the .class file appearing either in the music or gui folder was not the issue...I strongly believe it's with the classpath...I'm using Windows Vista and I set my variable to CLASSPATH and the value to C:\java Is that correct?

  18. #18
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    If your package is polymorphism.music, and your code is physically in c:\java\polymorphism\music, and you have your CLASSPATH set to include c:\java, everything will work fine, as long as you compile from the c:\java directory. It won't work right if you are in some other directory at the time you compile.

  19. #19
    dawiz001 is offline Member
    Join Date
    Feb 2009
    Posts
    21
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 02-12-2008, 11:03 AM
  2. Using packages
    By prfalco in forum New To Java
    Replies: 5
    Last Post: 01-31-2008, 10:38 PM
  3. Closing packages
    By uncopywritable in forum New To Java
    Replies: 0
    Last Post: 08-13-2007, 11:47 PM
  4. packages
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 12:10 PM

Posting Permissions

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