Java: Packages

Contributor Icon Contributed by William_Wilson  
Tag Icon Tagged: Java programming  

Clean up your class files, by using your own packages.


Have you ever wanted to clean up a folder FULL of .class files, and sort them into folders based on their use?
Or just put the program into a folder so all that is visible is a single .exe, or .bat, or .java?

*Well my friend here is how!

All that is requried is a few lines:
An example seems the best way to go:
I’ll have 3 class files:
1) program.java
2) GUI.java
3) run.java

suppose i want to put program and GUI into folders based on their use, and leave run at the same level as these folders.
I will assume that GUI and program don’t need to know about each other, and run, needs access to both.
Ending File Heirarchy:
GUI.java
program.java
run.class
run.java
PROGRAM <--this is a folder
|
---------program.class
GUI <----this is a folder
|
-----------GUI.class

NOTE that as long as you do not wish to modify a .class file, AND it is not needed to run your program, the corresponding .java file is not necessary, and can be deleted.

To accomplish this do the following:
GUI.java:
as the first line, and it MUST be the first line of any class.
add the line:
Package GUI;
*now compile, and the folder GUI will be created with the GUI.class file inside it.

program.java:
as the first line, and it MUST be the first line of any class.
add the line:
Package Program;
*now compile, and the folder Program will be created with the program.class file inside it.

run.java:
We must now tell this file where to find the .class files we just moved.
simply add to the rest of your imports:
import GUI.*;
import Program.*;

NOTE some compilers don’t like the * operator on personal packages and you may have to do the following:
import GUI.GUI;
import Program.program;

The .class files will still be listed inside the original folder, but i assure you they are no longer needed.
Compile run. java, and you are done.

Now you too can arrange your files like a pro!

Questions/Comments: william_a_wilson@hotmail.com
-William. ยง (marivn_gohan)

 

No Comments -


No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment -