This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help with package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with package" Watch "Help with package" New topic
Author

Help with package

eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
Hello,

I am attempting to put two classes into a package.

I do not have any problems if I comment out the package statements.

I keep getting the following errors when I try to put the two classes into package hwone:







If someone can help me understand what I am doing wrong, I would really appreciate it.

Thanks,
Eric
[ January 28, 2006: Message edited by: eric elysia ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Try compiling with a classpath tag. For example, if the hwone directory is under c:\java\myLibrary then compile with...

javac -classpath c:\java\myLibrary AlphabetizerTester.java


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by marc weber:
Try compiling with a classpath tag. For example, if the hwone directory is under c:\java\myLibrary then compile with...

javac -classpath c:\java\myLibrary AlphabetizerTester.java


Extending on marc's example, you might also try compiling from c:\java\myLibrary (i.e. the directory that contains your package's directory). As long as the current dirctory . (dot) is in the path, this should work as well.

Layne


Java API Documentation
The Java Tutorial
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
OK, I tried what you suggested.

I tried:

javac -classpath C:\Documents and Settings\Owner\Desktop\javahw

I got an invalid flag error. My guess is because of the "space" after Documents.

So I moved the javahw folder to the root of C:\

Then I went to Environmental Variables and changed the end of the path from C:\Documents and Settings\Owner\Desktop\javahw to C:\javahw.

I also did the same thing for the CLASSPATH.

Now, in jGrasp, it runs without the errors.

What was wrong?

Thanks,
Eric
[ January 28, 2006: Message edited by: eric elysia ]
MI Mohammed
Ranch Hand

Joined: Feb 16, 2005
Posts: 146
Hi Eric,
You have to understand that packages are just mapping of java objects to the system file structure. From your code, commenting out the package statement put the compiled class file in the default package. This is not a good placce to put your custom classes.
For details on dealing with packages visit my blogger www.javaden.blogspot.com.


SCJA(Beta) SCJP 1.4 SCWCD 1.4 SCBCD 1.3 SCBCD 5.0 beta <br />The more practice we get, the better we are at the exams and in life in general. Pls join me at My DEN.
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
It looks like your blog talks about the Development Environment, but I don't see anything about packages.

Eric
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
OK, sorry, now that I have read it, I see that it mentions packages. I am still a little confused about it.

Thanks,
Eric
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
Before I moved the javahw folder to the root of C, I was able to run the program with the package statements commented out.

I was also able to run javadoc.

Now that I have moved the javahw folder to the root of C, I am able to run the program with the package statements included, but I am not able to run javadoc. I have tried:

C:\>cd javahw

C:\JAVAHW>javadoc -doctitle "Alphabetizer" -author -version *.java
javadoc: error - File not found: "*.java"
1 error

C:\JAVAHW>C:\java\jdk1.5.0_06\bin\javadoc -doctitle "Alphabetizer" -author -version *.java
javadoc: error - File not found: "*.java"
1 error

C:\JAVAHW>

I would like to understand what I am doing wrong and how to fix it.

Thank you,
Eric
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by eric elysia:
...I also did the same thing for the CLASSPATH...

Did you already have a CLASSPATH set? If so, what was the complete setting? In particular, did it include a dot "." to indicate the current directory?

Just for clarification...
  • Setting the PATH variable allows you "to conveniently run the JDK executables (javac.exe, java.exe, javadoc.exe, etc.) from any directory without having to type the full path of the command." (Ref.)
  • Setting the CLASSPATH variable "tells JDK tools and applications where to find third-party and user-defined classes." (Ref.)
  • I recommend reading the page linked to by the classpath "Ref" above. In addition, the package chapter from Bruce Eckel's Thinking in Java might be very helpful.
    [ January 28, 2006: Message edited by: marc weber ]
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    Originally posted by eric elysia:
    OK, I tried what you suggested.

    I tried:

    javac -classpath C:\Documents and Settings\Owner\Desktop\javahw

    I got an invalid flag error. My guess is because of the "space" after Documents.


    [ January 28, 2006: Message edited by: eric elysia ]


    That is correct. In order to use spaces other special characters in command line arguments like this, you must put quotes around everything you want to treat as a single string. For example:

    javac -classpath "C:\Documents and Settings\Owner\Desktop\javahw" MyClass.java

    This is one reason why I like my alternative suggestion. If it was unclear, in this case you would use the following commands:

    cd "C:\Documents and Settings\Owner\Desktop\javahw"
    javac packagename\ClassName.java

    This assumes that either the CLASSPATH is not set or that it contains the current directory . (dot).

    Layne
    [ January 28, 2006: Message edited by: Layne Lund ]
     
    Consider Paul's rocket mass heater.
     
    subject: Help with package
     
    Similar Threads
    gettin error with TextReader
    Generics problem?
    Sorting and Comparators
    index question-arraylist
    'class' or 'interface' expected