• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

importing packages in a class

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the link I posted. It explains what the classpath is, why you need to set it, and how you set it.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic