aspose file tools*
The moose likes Beginning Java and the fly likes importing classes into java programs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "importing classes into java programs" Watch "importing classes into java programs" New topic
Author

importing classes into java programs

Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
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?
Tim Gruentaler
Greenhorn

Joined: Jan 03, 2009
Posts: 12
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.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
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.
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 686

Hey...

without look into your code is hard to say what kind of error in your code... so if possible post your code...


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
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!!!



Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
Hi,

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


Regards,
Rok


SCJP, SCWCD
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
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

Joined: Sep 23, 2009
Posts: 49


Thank you again for all your help!
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Theresa:

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

John.
Theresa Marlin
Ranch Hand

Joined: Sep 23, 2009
Posts: 49
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

Joined: Mar 09, 2009
Posts: 600
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

Joined: Sep 23, 2009
Posts: 49
Okay, I will. Thanks again!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: importing classes into java programs