Results 1 to 19 of 19
Thread: Creating and Using Packages
- 02-19-2009, 03:22 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 21
- Rep Power
- 0
- 02-19-2009, 03:23 AM #2
Could you show us the code please (in CODE tags).
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-19-2009, 03:31 AM #3
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
what does your directory structure look like?
- 02-19-2009, 07:41 AM #4
Member
- Join Date
- Feb 2009
- Posts
- 21
- Rep Power
- 0
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
- 02-19-2009, 07:43 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 21
- Rep Power
- 0
Also I did set the environment variable in Windows: CLASSPATH C:\java\polymorphism\gui;
- 02-19-2009, 01:11 PM #6
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.
- 02-19-2009, 03:16 PM #7
Member
- Join Date
- Feb 2009
- Posts
- 21
- Rep Power
- 0
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
- 02-20-2009, 12:46 AM #8
.. is the class you are importing public?
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-21-2009, 03:25 AM #9
Member
- Join Date
- Feb 2009
- Posts
- 21
- Rep Power
- 0
Yes MK12 the class I'm trying to import is public...
- 02-21-2009, 03:36 AM #10
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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
- 02-21-2009, 03:45 AM #11
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)
- 02-21-2009, 04:22 AM #12
Member
- Join Date
- Feb 2009
- Posts
- 21
- Rep Power
- 0
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...
- 02-21-2009, 04:23 AM #13
Member
- Join Date
- Feb 2009
- Posts
- 21
- Rep Power
- 0
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
- 02-21-2009, 04:26 AM #14
Member
- Join Date
- Feb 2009
- Posts
- 21
- Rep Power
- 0
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
- 02-21-2009, 04:26 AM #15
Member
- Join Date
- Feb 2009
- Posts
- 21
- Rep Power
- 0
that's basically where I reach but i can't seem to get my .class file back from the package...
- 02-21-2009, 04:45 AM #16
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)
- 02-21-2009, 09:36 PM #17
Member
- Join Date
- Feb 2009
- Posts
- 21
- Rep Power
- 0
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?
- 02-21-2009, 10:49 PM #18
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.
- 02-23-2009, 12:58 AM #19
Member
- Join Date
- Feb 2009
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
Importing packages from the packages within same application.
By sta2003 in forum New To JavaReplies: 3Last Post: 02-12-2008, 11:03 AM -
Using packages
By prfalco in forum New To JavaReplies: 5Last Post: 01-31-2008, 10:38 PM -
Closing packages
By uncopywritable in forum New To JavaReplies: 0Last Post: 08-13-2007, 11:47 PM -
packages
By ai_2007 in forum Advanced JavaReplies: 1Last Post: 07-31-2007, 12:10 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks