Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes creating a package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating a package" Watch "creating a package" New topic
Author

creating a package

Dan Maples
Ranch Hand

Joined: Jun 21, 2004
Posts: 153
hey guys, I'm trying to creat a package, I've looked at online tutorials including the one from sun. I am having a difficult time here and I don't know what I'm doing wrong. here is the code for my package and then for a app that calls on the package respectively.

and the app is:

Please tell me what I'm doing wrong.
Thanks-Dan


-Dan
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348


it should be
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24185
    
  34

Well, I imagine you could be doing all manner of things wrong without telling us about it But as far as what you've shown here: a "*" in an import statement always means precisely "all the classes in a package" so your line



means to import all the classes in the myUtils.myUtils package, and of course, there's no such package. Either "import myUtils.*;" or "import myUtils.myUtils;" would be correct, but what you've written is wrong.

While I've got you: please, please, please follow the extremely well-established Java naming conventions. Class names should always start with an upper-case letter, and package names should always be all lower-case. Furthermore, "myUtils" is kind of a silly name; something like "util", or far better, "com.danmaples.util", would be vastly superier. Ditto for the class, and finally, having a class and a package by the same name is always a bad idea: it's confusing! I think I'd recommend



[Jess in Action][AskingGoodQuestions]
Dan Maples
Ranch Hand

Joined: Jun 21, 2004
Posts: 153
Sorry Ernest I am not too familiar with the nameing conventions, although I will change it as soon as I can get this to work I promise. I changed it to:


and it gave me this error message:
-----------------------------------------------------------------------
outtest.java:1: package myUtils does not exist
import myUtils.myUtils;
^
D:\junit3.8.1\myUtils.java:2: package chn.util does not exist
import chn.util.*;
^
outtest.java:4: cannot access myUtils
bad class file: D:\junit3.8.1\myUtils.java
file does not contain class myUtils
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
String inner=myUtils.getString("enter a string");
^
3 errors
------------------------------------------------------------------------
I see it says package does not exist twice, and The third error is because it does not understand myUtils. So why can't it find the packages? When I compiled myUtils I typed:
javac -classpath D:\...\chnutil.jar;. myUtil.java
and for outtest.java I typed:
javac outtest.java
and that is what gave me those 3 errors.

thanks for your help-Dan
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Not only is naming the package and class the same a bad idea, IIRC, in some cases, it's illegal. The reason is that naming of packages and classes also dictates how they are stored, usually on the local file system. For example:



When you write



This means that you will have a file myUtils.java in a directory or subdirectory called myUtils.

The JLS 7.1 states that you can't have a package that has two members that have the same name. That is, you can't have



Besides being utterly confusing, the poor compiler would be at a loss as to what you refer to when you refer to myUtils.myUtils

Show us how you organize your directories and files and we'll tell you how you should have your package references and class names.


Junilu - [How to Ask Questions] [How to Answer Questions]
Dan Maples
Ranch Hand

Joined: Jun 21, 2004
Posts: 153
Ok so now I have this:

and


I compiled DansUtil.java using:
javac -classpath D:\junit3.8.1\chnutil.jar;. DansUtil.java
it compiled fine with no errors.

I compiled DansUtilRunner.java first using:
javac DansUtil.java
it gave me 2 errors.

Then I compiled DansUtilRunner.java using:
javac -classpath D:\junit3.8.1\chnutil.jar;. DansUtil.java
and it gave me the exact same 2 errors, they are as follows

---------------------------------------------------------------------------
DansUtilRunner.java:1: package com.danmaples.util does not exist
import com.danmaples.util.*;
^
DansUtilRunner.java:4: cannot access DansUtil
bad class file: .\DansUtil.class
class file contains wrong class: com.danmaples.util.DansUtil
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
String inner=DansUtil.getString("enter a string");
^
2 errors
---------------------------------------------------------------------------

What am I doing wrong?

Thanks for your help guys
-Dan
Anthony Lastralla
Greenhorn

Joined: Jul 21, 2004
Posts: 2
Is the directory structure for your package com.danmaples.util as follows?

c:\com\danmaples\util\DansUtil.class
(ie separate folders for each part separated by a period)
Dan Maples
Ranch Hand

Joined: Jun 21, 2004
Posts: 153
Is the directory structure for your package com.danmaples.util

yup, it sure is.

I think as soon as I fix the first one the second error will go away.

Thanks a bunch guys
-Dan
[ July 21, 2004: Message edited by: Dan Maples ]
Anthony Lastralla
Greenhorn

Joined: Jul 21, 2004
Posts: 2
Hummm strange....

You mentioned something about setting the classpath. From my very limited experience it seems a wrong classpath makes for strange errors. I always set my path to look in the current directory (by adding .; --- classpath=. I figure, I'm not sure if it's the best practice but it helps solve more problems than it creates. Try adding that, at the worse it will rule that out.

I noticed in your code (for DansUtil) above you had import chn.util.*; then you mentioned you compiled with
javac -classpath D:\junit3.8.1\chnutil.jar;. DansUtil.java

Did you mean chn.util with a period? I'm having a little trouble understanding if this is the same or not. Also wondering if the relationship between chn.util being imported and the class importing it. (Sorry if I'm leading you on wild goose chases).

Anyway, I recreated a similar (package) scenario on my system with very stripped down code and it's working fine.


and


I didn't put in any of the chn.util code so I'm guessing it has to do with that. Where is chn.util saved in relation to the other packages?
Dan Maples
Ranch Hand

Joined: Jun 21, 2004
Posts: 153
chnutil.jar is save in a different folder than com/damaples..... but when I compiled it I specified the path to find it, so that wasn't the problem. The problem had something to do with the classpath enviromental variable, I had somebody here help me with that and the problem is solved now, thanks guys for all your help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: creating a package