Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Create a package for source file ""

Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic