| Author |
importing packages in a class
|
nirjari patel
Ranch Hand
Joined: Apr 23, 2009
Posts: 241
|
|
I have created package1, package2 and package3 under packages folder on my desktop.
I want to import these packages in a new class I am creating. Can I just import these packages in the class with import statement in the beginning or do I need to put these packages in some library or path in order to import it in the class ?
Please let me know, what do I need to do in order to import these packages in class. If it needs to be put in library, how to put it in library or path ?
Thanks
|
 |
Kevin Workman
Ranch Hand
Joined: Sep 28, 2010
Posts: 151
|
|
nirjari patel wrote:Can I just import these packages in the class with import statement in the beginning or do I need to put these packages in some library or path in order to import it in the class ?
What happened when you tried that?
The packages have to be on your classpath.
Recommended reading: Setting the class path
|
 |
nirjari patel
Ranch Hand
Joined: Apr 23, 2009
Posts: 241
|
|
This is the path for packages.
"C:\Users\nirjari\Desktop\packages\package1"
"C:\Users\nirjari\Desktop\packages\package2"
"C:\Users\nirjari\Desktop\packages\package3"
I get following errors when I run code on the cmd line.
C:\Users\nirjari\Desktop\packages\package1> java SubclassInSamePackage
Exception in thread "main" java.lang.NoClassDefFoundError: SubclassInSame
(wrong name: package1/SubclassInSamePackage)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: SubclassInSamePackage. Program will exit.
Please suggest, whats wrong in here. When I am running class file from the path its located in, why do I need to set classpath ?
Thanks
|
 |
Kevin Workman
Ranch Hand
Joined: Sep 28, 2010
Posts: 151
|
|
|
Read the link I posted. It explains what the classpath is, why you need to set it, and how you set it.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
You ought to be working from the directory one level above the packages. You need to call the classes by their fully-qualified names, eg package1.Foo
And package1, package2, etc are very poor names for packages.
|
 |
Audrey Williams
Greenhorn
Joined: Jan 08, 2011
Posts: 4
|
|
|
I'm having the same problem importing a package. I'm actually trying to work through the Beverage/Tea example on page 54 of the SCJP Java 6 Study Guide. I set my classpath directory to the bin folder where I'm saving my code, and I also tried setting the classpath to the directory above the bin folder. I still keep getting the error that the package that I'm trying to import doesn't exist. Can someone PLEASE help me.
|
 |
Audrey Williams
Greenhorn
Joined: Jan 08, 2011
Posts: 4
|
|
I have just fixed my problem. I'm still new to Java, but it looks like my issues were all environmental. I don't know if this will help anyone else, but below is my solution:
1) at the command prompt I typed: Set JAVA_HOME = C:\Program Files\Java\jdk1.6.0_23
2) also at the command prompt: Set Path = %JAVA_HOME%\bin
3) also at the command prompt: Set CLASSPATH = ".;C:\Java"
I'm not 100% sure why this worked....but it did! I ended up creating a C:\Java\src directory to store my .java files instead of keeping them in the bin directory.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
Welcome to the Ranch Audrey Williams
Find the "common problems" page in the Java™ tutorials, where it says to look at §4 of the installation instructions about setting your PATH. Put the Java™ installation you want to use before System32 or similar in the PATH string. You may find out why it worked when you read those resources; if not, ask again.
It is usually a bad idea to set a CLASSPATH permanently; if you keep all your files in your "current directory" and navigate there with the cd command, you can probably dispense with a classpath altogether. Leave your classpath blank and see whether it still works; if you have problems, ask again.
Good idea to create a C:\Java directory. I keep telling newbies to do that, so I am please you worked it out for yourself.
|
 |
Christian Joseph
Ranch Hand
Joined: Jan 07, 2011
Posts: 43
|
|
i believe its on HEAD FIRST JAVA book
you need to run it this way
run > cmd
@cmd
type "javac xxx.java"
then type " java application1.xxxx
heres the link http://www.jarticles.com/package/package_eng.html
|
 |
 |
|
|
subject: importing packages in a class
|
|
|