Results 1 to 11 of 11
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By DarrylBurke

Thread: Package not found

  1. #1
    garet jax is offline Member
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Question Package not found

    I have been trying to make packages for common utility classes etc for later use. However with all the packages I try to create, no matter how I do the directory, my import statements always cause an import error saying the package doesn't exist. I've looked at several tutorials, looked at the API and I still don't get it. In case its important, I use TextPad to write, compile and run my current java programs.

    I created an example directory and used HelloWorld as the sample program just for simplicity sake.

    C:/world/HelloWorld.java is the directory

    I included package world; in my HelloWorld program, and it compiled fine.

    I opened command prompt and entered the following: set CLASSPATH .;C:\; and it went just fine.

    I then successfully compiled my program from command prompt using javac world.HelloWorld

    Neither import world.HelloWorld; or import world.*; worked in my other program, which compiles fine before I add that import statement, with the only generated error being that the package doesn't exist. My other import statements for java.swing and javax.awt.event compile fine still.

    I then went to my environment variables and added C:\world to both system path (both at the end of the path variable containing the java path and creating a new path for it) and new user path variables.

    My import statement still does not compile. The programs compile fine by themselves, but when I try to import any custom made package, I always get "the package does not exist" error.
    I've been working through tutorials and forums for days now and I'm hoping someone here can help me out.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    garet jax is offline Member
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default Re: Package not found

    This is the program containing the package code. It compiles fine. It is saved in C:/world
    C:/world has been added to system and user variables and set as CLASSPATH using the command prompt.

    Java Code:
    package world;
    
    public class HelloWorld
    {
    	public static void main(String[] args)
    	{
    		System.out.println("Hello World");
    	}
    }
    This is one of my random programs that I try to import it into, and it will not compile with the import world.* statement. Once that import statement has been removed it works fine.

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import world.*;
    
    public class Namer extends JFrame
    {
    	public static void main(String [] args)
    	{
    		new Namer();
    	}
    	private JButton buttonOK;
    	private JTextField textName;
    
    	public Namer()
    	{
    		this.setSize(325,100);
    		this.setTitle("Character Creation");
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		ButtonListener b1 = new ButtonListener();
    
    		JPanel panel1 = new JPanel();
    
    		panel1.add(new JLabel("Enter Your Character's Name: "));
    
    		textName = new JTextField (18);
    		panel1.add(textName);
    
    		buttonOK = new JButton("OK");
    		buttonOK.addActionListener(b1);
    		panel1.add(buttonOK);
    
    		this.add(panel1);
    		this.setVisible(true);
    	}
    
    	private class ButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			if (e.getSource() == buttonOK)
    			{
    				String name = textName.getText();
    				if (name.length() == 0)
    				{
    					JOptionPane.showMessageDialog(
    						Namer.this,
    					"You need to enter a name for your character.",
    					"You need to enter a name for your character.",
    					JOptionPane.INFORMATION_MESSAGE);
    				}
    				else
    				{
    					JOptionPane.showMessageDialog(
    						Namer.this,
    					"Welcome" + name,
    					"Welcome" + name,
    					JOptionPane.INFORMATION_MESSAGE);
    				}
    				textName.requestFocus();
    			}
    		}
    	}
    }
    I receive the following error when attempting to compile.

    O:\Java\Namer.java:3: error: package world does not exist
    import world.*;
    ^
    1 error

    Tool completed with exit code 1

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    garet jax is offline Member
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default Re: Package not found

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    C:\Users\Owner>javac O:\Java\Namer.java
    O:\Java\Namer.java:3: error: package world does not exist
    import world.*;
    ^
    1 error

    C:\Users\Owner>

    That is exactly what shows up in the command prompt. I'm not sure if theres a specific BB code for command line prompts or if you wanted a screenshot, but thats the exact error.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Package not found

    You don't seem to have the world package in the class path. What if you did something on the lines of

    Java Code:
    C:\Users\Owner>javac -cp .;C:\ O\Java\Namer.java

  7. #7
    garet jax is offline Member
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default Re: Package not found

    Would that just add the package to the classpath and then compile the program, or will that be adding my namer class to the classpath as well?

    Also, it worked when i input that exact code into the command line, however, it didn't actually seem to compile.

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    C:\Users\Owner>java O:\Java\Namer
    Error: Could not find or load main class O:\Java\Namer

    C:\Users\Owner>

    It also still fails to compile in the TextPad compiler. Same error, says package world does not exist.
    Last edited by garet jax; 12-05-2012 at 06:01 AM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Package not found

    Quote Originally Posted by garet jax View Post
    Would that just add the package to the classpath and then compile the program, or will that be adding my namer class to the classpath as well?
    Try it and see.

    Namer doesn't seem to have a package and thus appears to be in the "default" package. I don't advise that you do this since all code should be in a package.
    garet jax likes this.

  9. #9
    garet jax is offline Member
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default Re: Package not found

    Once I added Namer to the package, it worked even though namer wasn't in the package directory. Why is that? (Just want to know for curiousity sake)

    One last question: Will classes from separate packages be unable to call on each other using import, or is that just a downside of the default package?

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Package not found

    Classes in a named package cannot access classes in the default package.

    The reverse isn't true.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    garet jax is offline Member
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default Re: Package not found

    Thanks for all the help guys. Glad I joined this community already.

Similar Threads

  1. Replies: 5
    Last Post: 10-31-2012, 12:51 AM
  2. Replies: 3
    Last Post: 03-27-2012, 09:27 AM
  3. Package not found
    By doncigalo in forum New To Java
    Replies: 3
    Last Post: 10-25-2010, 06:06 PM
  4. run package inside anthor package
    By AhmedAdel in forum AWT / Swing
    Replies: 4
    Last Post: 04-20-2010, 11:52 AM
  5. Replies: 7
    Last Post: 11-09-2009, 07:51 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
  •