File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Create a package for source file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Create a package for source file """ Watch "Create a package for source file """ New topic

Create a package for source file ""

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
In my posting „Declare and use interface“ Anthony Villanueva showed my a code which creates Package „math.Vector“
For source file and imports package „math.Vector“ in
But he only told me the coding not the way to create / compile the source files and
I think it is quite difficult to create packages so I did it myself for this example and want you to show the results:

1. CREATE PACKAGE: Save Source File into package „math“
Here's a sample for creating „“ in Package „math“.I have a "root" directory in C:\anthony\java\applications. I create a C:\anthony\java\applications\math folder and define a in this folder with the following code:

When you compile please name path where it is stored c:\anthony\java\applications\math otherwise compiler won’t find source file and issue error: „error: cannot read:“.
Also state root directory <c:\anthony\java\applications> and –d (package option) when compile source file with javac ...
Therefore compilation statement is „javac –d\ c:\anthony\java\applications\
 A .class file is created in folder „c:\anthony\java\applications\math“.
DOS-statements to compile into package „math“
> cd C:\anthony\java\applications\math
> javac -d c:\anthony\java\applications
C:\anthony\java\applications\math>javac -d c:\anthony\java\applications

Note the package declaration at the very top.
In C:\anthony\java\applications I define which imports math.Vector:

Note the import command at the very top. Since it is very possible that classes may share the same name, packages are used to differentiate them, in the same sense that different file paths distinguish different files with the same name. For example, there is a java.util.Date as well as a java.sql.Date class. In the example above we now have a math.Vector class in addition to the usual java.util.Vector.
DOS-Statements to compile „“
> cd C:\anthony\java\applications
> javac
Run „“:
DOS-Statements to run „“
> cd C:\anthony\java\applications
> java Vectorpackagetest
C:\anthony\java\applications>java Vectorpackagetest
> cd C:\anthony\java\applications
> java Vectorpackagetest

C:\anthony\java\applications>java Vectorpackagetest
2.0 0.0 2.0
I agree. Here's the link:
subject: Create a package for source file ""
It's not a secret anymore!