Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Ivor Horton's Beginning Java, Java 7 Edition
|
BOOK: Ivor Horton's Beginning Java, Java 7 Edition
This is the forum to discuss the Wrox book Ivor Horton's Beginning Java, Java 7 Edition by Ivor Horton ; ISBN: 978-0-470-40414-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Java, Java 7 Edition section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
View Poll Results: Was the thread adequately explanatory ?
Yes 0 0%
No 0 0%
Voters: 0. You may not vote on this poll

 
Old May 7th, 2012, 02:17 AM
Registered User
 
Join Date: May 2012
Posts: 9
Thanks: 0
Thanked 2 Times in 2 Posts
Lightbulb [HOW TO] Compile and Use Java Packages in 5 Steps

Before you start reading this, I highly recommend that you open the image below and refer to it from time to time.
http://img4.imageshack.us/img4/3820/packaging.png
NOTE: Chrome users might experience problems with the link above. In that case, use IE

I’m using Ivor Horton’s example of Point and Line class but with a few changes. The changes aren’t anything major; I have just excluded a few methods and added a couple of mine.

With that said, let’s get into the actual process of packaging up the two classes. Here is a summary of where things are located
Name Of My Package : Geometry
Location : D:\MyPackages
Name Of My Source File : TryGeometry.java
Location : D:\JavaSource

The ‘Geometry’ package is actually a folder by the name ‘Geometry’ that has both Line.java and Point.java source files in it. For the package to work, we need to convert these into .class files which, by default, will be stored in that very folder.

Step1: Navigate to WITHIN the Geometry folder. In other words, make it the current folder.
Step 2: To compile the classes in the package , you need to set the classpath. The classpath will be the full path to the *parent* of Geometry folder. In this case, D:\MyPackages.
Execute the command:
Code:
 javac –classpath “D:\MyPackages” *.java
I know that since Line and Point are inter-related classes they will get compiled even if only Line.java is compiled but I preferred using wildcard operator.
Step 3: Navigate to the folder where your source file is located. In this case, D:\JavaSource.
Step 4: to compile the TryGeometry.java source file , you will have to provide the path to your package , compulsorily , as they are not a part of Java Standard Package or been made extension to Java Standard Package
Execute the command :
Code:
javac –classpath “D:\MyPackages” TryGeometry.java.
Again, classpath will be to the *parent* of the package
Step 5: To execute , you need to insert a period followed by semicolon and the package path in classpath.
Execute the command :
Code:
java –classpath “.;D:\MyPackages” TryGeometry
The period is necessary because without this , java looks for .class file of TryGeometry in the package’s folder.

I hope that sums it up :)

Last edited by javaKid1337; May 7th, 2012 at 05:34 AM.. Reason: Made a typing mistake in -classpath





Similar Threads
Thread Thread Starter Forum Replies Last Post
Question, Any Java Gui CPU/PacketperSec Objects or Packages d-mart Java GUI 5 April 3rd, 2009 07:43 AM
why can't i compile this java file? poorguy Java Basics 1 June 11th, 2007 08:03 AM
Compile Java classes to EXE vb_programmer Java GUI 7 December 14th, 2004 08:16 AM
is java beens are types of packages? vipul JSP Basics 1 February 21st, 2004 07:26 AM
My Java program won't compile emilaghayev JSP Basics 0 October 21st, 2003 06:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.