Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with package

 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like your blog talks about the Development Environment, but I don't see anything about packages.

Eric
 
eric elysia
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 3061
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic