wood burning stoves*
The moose likes Beginning Java and the fly likes importing other classes and using classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "importing other classes and using classpath" Watch "importing other classes and using classpath" New topic
Author

importing other classes and using classpath

donald croswell
Greenhorn

Joined: Mar 23, 2001
Posts: 6
Howdy
I am having trouble setting my classpath to include an extra class file.
Some of my problems could be:
1. is the class path the same path that I set in DOS? (ie.path=c:/jdk1.3/bin) ?
2. what sort of file is the new class supposed to be?(.class,.java,.jar)
3. what is a package and does my file have to be in one?
4. when I include java.lang.*; where is this package located and what is it called?
I know this is a big question but I have been trying to get this stupid thing to work all day and I'm going crazy. I have the book "Java First Contact" but I cant figure it out with that either.
Help Please!
Terry McKee
Ranch Hand

Joined: Sep 29, 2000
Posts: 173
There are two variables that you need to setup, the CLASSPATH and the PATH. Somewhere in the PATH statement you have to add the bin folder of your newly installed Java Development Kit. For example:
PATH=%PATH%;C:\jdk1.3\bin
(The %PATH% keeps any previously defined directories in the path statement while the C:\jdk1.3\bin adds the bin directory to the path statement)
The CLASSPATH tells the JDK where to find the libraries of programs that it needs to utilize in order to compile and run your programs. You need to make the lib directory and the . current directory available to the CLASSPATH statement. For example,

SET CLASSPATH=%CLASSPATH%;C:\jdk1.3\lib;.
See if this works.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Terry answered 1. pretty good, so I'll take a crack at the others.
2. when you compile *.java files, they turn into *.class files (sometimes more than one of them)
3. just like file directories on your computer, packages are a way to group your classes in logical... well, packages. Sun does this, and you can tell this whenever you import something. If you open up src.jar with Winzip, you will see all of the source files for Java. Examine the path information in the WinZip window, and you'll see that all of the classes you import with the command "import java.awt.event.*;" have source files in the path java\awt\event
You don't need to have your classes inside a package. There is something known as 'the default package' which all classes are part of, when you don't specify any other package. Package stuff doesn't come into importance until you begin to make applications that have many classes, and potentially re-use classes you've previously written. (Like when you import classes that Sun has written, when you import javax.swing.*)
4. java.lang.* is imported by default in any java source file. You don't need to implicity import it. The entire package name is "the java.lang package"
[This message has been edited by Mike Curwen (edited March 28, 2001).]
donald croswell
Greenhorn

Joined: Mar 23, 2001
Posts: 6
Hi
Thanks for the quick reply.
I am a little clearer but the class path is not working with me. If I am in dos and type: path
I get the path
But if I type :classpath
I get an error message that says is not recognisable as a command or batchfile.
So I am assuming that you don't enter the classpath in Dos, you do it somewhere else.
Also when I enter the new path in, I am still getting the message that the package does not exist. I am assuming this is because my classpath is still not set.
Arghhhhhh!
I know it is simple but I am missing something.
donald croswell
Greenhorn

Joined: Mar 23, 2001
Posts: 6
Hey again
I think I am getting closer now because when I compile the program with the imported package, it works!
But then when I try to run the program:
java Chapter4n1
I get an error:
Exception in thread "main" java.lang.NoClassDefFoundError: Chapter4n1/java
now what am I doing wrong??
This is the code I am using.

import java.lanks.* ;
public class Chapter4n1
{
public static void main(String[] args) throws Exception
{
// read in and output a String
BasicIo.prompt("please type in a string ") ;
String string1 = BasicIo.readString() ;
System.out.println("the string you typed was **" + string1 + "**") ;
System.out.println() ;
// read in and output an integer
BasicIo.prompt("please type in an integer ") ;
int intValue = BasicIo.readInteger() ;
System.out.println("the integer you typed was " + intValue) ;
} // end of method main
} // end of class Chapter4n1
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

when you set classpath in your autoexec.bat, you cannot open a DOS window and type "c:>classpath" and have it print out what the classpath is, unlike "c:>path" and have it print the path.
I recall way back when I was being taught DOS, that you could do this for any environment variable, but it's never worked for me in Windows.
Setting classpath is not really necessary, unless you have multiple packages imported, that are not rooted in the current directory.
****************
This is one of those things about Java that I had the hardest time with, and here is how I've come to think of it.
When you use an IDE and click on compile, it will construct the following string and send it to a DOS prompt...
javac -classpath <> -sourcepath <> -d <> *.java
In between each of the <> is an absolute path, or paths seperated by a ;. classpath and sourcepath are self-explaining, and -d is the destination of compiled class files.
Here's where it gets interesting.
If in MyClass.java, you import the following package:
"com.MyPackage.MySubPackage.MyClass5"
*AND* the actual place on your harddrive that this package is 'rooted' at is: "c:\javawork\packages"
(So a full path to MyClass5.class would be:
c:\javawork\packages\com\MyPackage\MySubPackage\MyClass5.class)
Then your Classpath MUST contain "c:\javawork\packages". This is one of the places that the java compiler will start to look for the import "com.MyPackage.MySubPackage.*", and it will find it there.
If your classpath only contains "c:\javawork", then the import would be looking for:
"c:\javawork\com\MyPackage\MySubPackage\MyClass5.class"
But this file does not exist! So you would get a Class Not Found error.
In summary, java compiler, when it attempts an import, will turn each '.' in the import into a '\', and then in turn, will append each entry in CLASSPATH in front of this import. If the file is found, it is imported. If it's not, it will try the next entry in CLASSPATH. If it's still not found, you get a compiler error.
*************
As for the error you do get... I've never seen a main function being declared as throwing an error...
Or perhaps the BasicIo is in a deeper package than java.lanks.*
(One thing about packages that sort of sucks is this: You can only import from one folder at a time with one import statement.)
So if you had packages like this:
com\MyPackage\*.class
com\MyPackage\SubPackage\*.class
com\MyPackage\SubPackage\AnotherPackage
If you want to import a class from the first, second and third package, you can't just say "import com.MyPackage.*;"
you would need to specify all three.
"import com.MyPackage.*;
import com.MyPackage.SubPackage.*;
import com.MyPackage.Subpackage.AnotherPackage.*;
"
Mark Savory
Ranch Hand

Joined: Feb 08, 2001
Posts: 122
Exception in thread "main" java.lang.NoClassDefFoundError: Chapter4n1/java
The above tells me that you're running your program like so:
java Chapter4n1.java
You should be running it without the .java extension:
java Chapter4n1
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

hehehe.
silly me.
LOL
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: importing other classes and using classpath
 
Similar Threads
packages + classpath
Errors when compiling these classes...
help in package/class path !
packages
Javac cannot find the parent class