• 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 classes into java programs

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to import two classes into a java program, and a google search as to how to do this yielded the following: it has to have a .class extension, then use import java.
However, when I tried this, I got the following errors, one for each class:

2 errors found:

Error: cannot find symbol
symbol : class Jam
location: package java

Error: cannot find symbol
symbol : class Pantry
location: package java

Does anyone know what I am doing wrong?
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you typed:

import java.yourClass;

But your class isn't located in the java package, that is the core api.
If you have the source of those packages then add at the top:
package theContainingFolderName;
You might need to add this in the other classes to...
Then compile them and put them in the same directory as the other classes, that is the directory you specified as "theContainingFolder".
When you import them in the source of the other classes do it like this:
import theContainingFolderName.yourClass;

Hope it helps...somebody will probably give you a better explanation than this.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Theresa:

When you are using the import directive, you need to use the correct package name. For example, If I have a class called Example, in a package called com.example.project.subproject, then the import call would look like:

John.
 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey...

without look into your code is hard to say what kind of error in your code... so if possible post your code...
 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you everyone for your help! I am taking an AP Computer Science class, and dealing with a teacher not writing back to any of my emails. Your help is greatly appreciated!

My two classes currently are:


and


I have to change the classes though and write a testing program so that it takes user input for how much jam needs to be spread, and then returns how much jam is left in the jar, until one of the jars is empty. I figured the first thing I should do is import the two classes into the program, I can not yet figure out how to.
As far as typing package TheContainingFolderName at the top, should I write just the folder that the program is in, or the whole path? For example, can I just write package Nov23-30, or do I have to write package Users/Theresa/School2009-2010/ComputerScience/Nov23-30; ?

John, my program is Pantry Tester and my classes are Jam and Pantry. I tried import PantryTester.Jam;, and it said Error: package PantryTester does not exist. Did I do it wrong?

Thank you again SO MUCH for all your help!!!



 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Check the following link for naming convention:
http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html


Regards,
Rok
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Theresa:

Can you post the code for your PantryTester class? Since your Jam and Pantry classes appear to be in the default package it will depend on where your PantryTester class resides.

John.
 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thank you again for all your help!
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Theresa:

Since all of your classes are in the default package, just remove the import lines.

John.
 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John, Thank you very much for all your help! I'm sorry I was such a bother, my assignment said that the first step should be importing the other two classes. Thanks again!
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Theresa:

Glad I could help. In the future, you should generally avoid using the default package. It's better to use something meaningful, even if it's something simple like 'test'.

John.
 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I will. Thanks again!
 
reply
    Bookmark Topic Watch Topic
  • New Topic