Results 1 to 11 of 11
Thread: Package not found
- 12-05-2012, 05:09 AM #1
Member
- Join Date
- Dec 2012
- Posts
- 6
- Rep Power
- 0
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.
-
Re: Package not found
Please show exactly how you try to compile your code and the exact error message.
- 12-05-2012, 05:33 AM #3
Member
- Join Date
- Dec 2012
- Posts
- 6
- Rep Power
- 0
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.
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:package world; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } }
I receive the following error when attempting to compile.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(); } } } }
O:\Java\Namer.java:3: error: package world does not exist
import world.*;
^
1 error
Tool completed with exit code 1
-
Re: Package not found
Please show the whole command line where you try to compile your code.
- 12-05-2012, 05:47 AM #5
Member
- Join Date
- Dec 2012
- Posts
- 6
- Rep Power
- 0
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.
-
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
- 12-05-2012, 05:55 AM #7
Member
- Join Date
- Dec 2012
- Posts
- 6
- Rep Power
- 0
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.
-
Re: Package not found
- 12-05-2012, 07:12 AM #9
Member
- Join Date
- Dec 2012
- Posts
- 6
- Rep Power
- 0
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?
- 12-05-2012, 07:30 AM #10
Re: Package not found
Classes in a named package cannot access classes in the default package.
The reverse isn't true.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-05-2012, 07:59 AM #11
Member
- Join Date
- Dec 2012
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Glassfish3/jdk7 - where is javax.servlet package; not found when I compile
By BernieC in forum New To JavaReplies: 5Last Post: 10-31-2012, 12:51 AM -
Eclipse package classes are not visible - deleting package completely - checkstyle
By compauer in forum EclipseReplies: 3Last Post: 03-27-2012, 09:27 AM -
Package not found
By doncigalo in forum New To JavaReplies: 3Last Post: 10-25-2010, 06:06 PM -
run package inside anthor package
By AhmedAdel in forum AWT / SwingReplies: 4Last Post: 04-20-2010, 11:52 AM -
The declared package does not match the expected package
By oneforall in forum EclipseReplies: 7Last Post: 11-09-2009, 07:51 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks