where to package

hi I'm gind of new to java and I was wondering after I add a file to a package with the "package" keyword where do I put it and do I compile it?
[152 byte] By [G-primea] at [2007-9-29 6:20:17]
# 1
The online tutorial is a good place to start. This is a section on packages. http://java.sun.com/docs/books/tutorial/java/interpack/packages.html
atmguya at 2007-7-14 20:26:36 > top of Java-index,Developer Tools,Java Compiler...
# 2
thank you I'll go read it right away
G-primea at 2007-7-14 20:26:37 > top of Java-index,Developer Tools,Java Compiler...
# 3
it was helpful but it didn't answer all my questionswhere do you put the file?can you put it in the src.zip folder?do you have to compile it so it will be in the package?can you put two package statements in the same file?thx for your help
G-primea at 2007-7-14 20:26:37 > top of Java-index,Developer Tools,Java Compiler...
# 4

When you compile things, all packge names are relative to the items in the "sourcepath". The default source path is ".", the current directory.

so:

$ mkdir myproject

$ cd myproject

$ mkdir apackage

$ cd apackage

$ echo "package apackage; class Foo {}" > Foo.java

$ cd ..

$ javac apackage/Foo.java

$ java -classpath . apackage.Foo

ERRROR class Foo has no main() method, you tool!

$

pmuurray@bigpond.coma at 2007-7-14 20:26:37 > top of Java-index,Developer Tools,Java Compiler...
# 5

> it was helpful but it didn't answer all my questions

> where do you put the file?

You can put it wherever it is convenient, as long as the directory structure and Classpath are correct. For example, you might want to keep the .class files separate from the .java files. For a Windows machine you could create a directory c:\myjava\myclasses and c:\myjava\mysources. Then put your projects into the mysources directory. You can use the -d option of javac to compile your code into the myclasses directory. If you put c:\myjava\myclasses into your system Classpath, then everything will work.

> can you put it in the src.zip folder?

I wouldn't. If you upgrade the j2sdk, then you will get a new src.zip. You can put packages into jar files.

> do you have to compile it so it will be in the

> package?

By putting the package statement into the source code, it will be compiled as a packaged class. However, it must be in the correct directory structure to use the class. You can put the source into the right directory and the compiler will generate the .class file there. Or you can use the -d option of javac. Or you can manually move files to the right directory. As long as the .class file

> can you put two package statements in the same file?

No. Packages are used to manage access and namespaces. A class has a fully qualified class name which is used to uniquely identify the class. The fully qualified class name is the package name plus the class name.

atmguya at 2007-7-14 20:26:37 > top of Java-index,Developer Tools,Java Compiler...