|
-
Dec 7th, 2003, 06:02 PM
#1
Thread Starter
Addicted Member
Please Help!!!: packages
I am trying to put some of my more commonly used classes into packages but i cannot figure out how to do this correctly. i have put the "package whatever.MyClass;" at the top of each file i have. The compiler is giving me the "cannot find symbol" error for any other classes in my package. Why is this??? Can someone tell me how to make a package!!! Please!!!
To protect time is to protect everything...
-
Dec 7th, 2003, 06:40 PM
#2
Thread Starter
Addicted Member
to add on: I think i may have figured out to make the package... I got the files to compile, but now i can access them. I get a big error when putting "import mypackage.*;" into another java file.
ALSO: I want to be able to jar my packages after i make them. I can get the files to jar alright, but again, the compiler cant seem to find them once i do. How do i make it so that I can jar a bunch of files and be able to import them and access them???
To protect time is to protect everything...
-
Dec 8th, 2003, 12:25 AM
#3
Dazed Member
I never really got packages to work correctly. As far as i always understood it the package directive that you specify at the top of a .java file is supposed to mirror the directory path where the .class files are contained. So if i keep my class files in a directory such as the following C:\Sort\Algorithms the package directive shoud be package Sort.Algorithms;. Then if i want to be able to use a .class file that is contained in the Algorithms directory i can use import Sort.Algorithms.*; or use the full qualified name ie. Sort.Algorithms.BubbleSort.
I think that you would also need to add any package that you create to your CLASSPATH system variable or else how would the system classloader know where to find a class that you are trying to create an instance of?
-
Dec 8th, 2003, 12:25 PM
#4
Thread Starter
Addicted Member
Hey! I figured it out. Correct you must keep all your package files in a directory structure that represents how they are named. You must also have a root directory for all of the package directories. Then its easiest to compile all files int a director at once ( EX. c:\root_dir\javac package_dir\*.java ) then all you have to do is jar the class files byt doing this: c:\root_dir\jar cf package.jar Package_dir/*.class
To protect time is to protect everything...
-
Dec 8th, 2003, 02:01 PM
#5
The package declaration states which package a class is in.
package com.cornedbee.jknowledger.main;
Java naming convention says that package names are all lowercase.
It also says that a company/individual should use the inverted last part of the homepage at the beginning to avoid name conflicts. I don't own cornedbee.com, but I doubt anyone else ever will, so I use it.
The classloader uses the package name in the search for the class as already said. The .class file is searched for in a subdirectory of any of the classpath directories/archives. This subdirectory is constructed by replacing the . in the package name by the platform dir separator, \ in Windows.
com\cornedbee\jknowledger\main\Classname.class
The compiler does the same in search for sources.
The fully qualified name of a class is given by the local name (e.g. JKnowledger), prepended by the full package name:
com.cornedbee.jknowledger.main.JKnowledger
The full name must be given in order for the classloader to find the class. However, the import statement can be used to centralize this location statement so that it doesn't have to be repeated.
I've heard that a package name must not start with the characters "java" unless it is contained in the bootstrap jar (rt.jar). Not entirely sure on that though.
A package is represented by the java.lang.reflect.Package class.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 15th, 2003, 01:30 PM
#6
Thread Starter
Addicted Member
I got that all to work, however, when I jar my packages, then the JVM can't find them... how do i fix this?
To protect time is to protect everything...
-
Dec 15th, 2003, 02:38 PM
#7
You must mention the jars themselves in the classpath.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|