Follow Slashdot blog updates by subscribing to our blog RSS feed

 



Forgot your password?
typodupeerror
×
User Journal

Journal RevMike's Journal: For StB: Some practical Java info... 1

Sam, the following are some things that I remember confusing lots of beginners and didn't seem to be covered very well it lots of books. I'd suggest trying to wrap your head around this as early as possible.

Packages

Imagine that I wrote a class called Log that handles log files. Now Blinder wrote a class called Log that provides a whole bunch of mathematical operations based on Logarithms. Finally, Fort Knox writes a class called Log that is part of the data model for a timber company. Now say you want to write an application that uses all three of our classes. How do you keep all three straight?

This problem comes up so often in the software world that Java provided a set of best practices on how to handle it right from the start: packages.

A package forms part of the name for a class. In that way, all three Log classes can have different names.

A set of rules and conventions are generally followed when naming packages. Here they are off the top of my head:

  1. package names are always in lower case, while class names start with case characters
  2. package names consist of one or more words with dots between them
  3. package names that start with java are part of the java language as defined by Sun
  4. package names that start with javax are optional parts or extensions to the java language as defined by sun
  5. everyone else uses reverse internet domains to distinguish packages

The full name of the Map class provided by Sun is java.util.Map. When I write a class as part of professional services engagement for my company, I write com.mycompanyname.ps.projectname.other.descriptive.text.MyClass. This seems like a pain in the ass at first, but it quickly becomes second nature.

The packages normally translate into directories. So when I write the code for com.mycompanyname.ps.projectname.other.descriptive.text.MyClass I do it in a file named com/mycompanyname/ps/projectname/other/descriptive/text/MyClass.java. When that file is compiled, the output will go to com/mycompanyname/ps/projectname/other/descriptive/text/MyClass.class. Again, this seems like a pain in the ass, but it too will become second nature.

I can take a bunch of class files and put them into a zip file, then use that zip file as a "library". This zip file is usually a "jar" file, which stands for "Java ARchive", and has the extension "jar". The directory structure inside the jar will still contain com/mycompanyname/ps/projectname/other/....

For things like classwork, your email address is probably the good root for a package name. You'll probably want to put your work in packages like com.gmail.samthebutcher.schoolname.cs101.proj1...

Typing out the full name of a class every time you need to reference it can be very tedious. By using the keyword "import" at the top of java file, you can avoid having to type out the whole name every time you need to reference it. So I can use the command "import com.mycompanyname.ps.projectname.other.descriptive.text.MyClass;", then I can just use the short name MyClass within my code, and Java knows what I mean.

I can also import an entire package at once by typing "import com.mycompanyname.ps.projectname.other.descriptive.text.*;". Understand, however, that this only imports that specific package. If there is another package with a name that includes that name but then has another leve, like com.mycompanyname.ps.projectname.other.descriptive.text.anextralevel.MyClass, it is a totally seperate package and won't get included with the *.

The package java.lang is always imported automatically. String and java.lang.String are the same thing.

Jar Utility

As mentioned briefly above, Java has the capability to work with lots of class files bundled together in a zip format. While Java can use a plain old zip file, most of the time java uses a specialized zip file called a Java ARchive, with a jar extension. A jar contains an extra directory, META_INF, and an extra file META-INF/MANIFEST.MF. The manifest file is used to store metadat about the jar, and do some other things you don't need to know about right now.

Java provides a jar utility to make, update, and view jar files. The jar utility takes many of the same command line switches as the unix tar command, so it is pretty easy to use. BTW, the "M" switch suppresses the generation of the manifest, so the command "java cfM MyZip.zip ..." is a pretty good way to zip up a file or directory.

Ant nad your project structure

The Apache Foundation supplies lots of great Java utilities. One of which is Ant, available at http://ant.apache.org/. Ant is basically a build automation tool. A file, build.xml, resides in the base direcotyr of your project. Then you issue command like "ant compile" or "ant clean" and ant builds your project. Ant is, by far, the most widely used tool for doing this.

Typically, you'll give each project its own base dir. It might be ~/mycode/myproject, or it could be somewhere else. Under that base dir, you'll normally have your build.xml file a dir src/, and maybe a dir lib/. The lib directory will contain any third party libraries and jar files. You'll write your code under src, taking care to have the right subdirectories to tie in with your packages.

After you have written some source, you need to compile it. Here is where you start using ant. You'll issue the command "ant compile". First ant will notice that the compile task is dependent on a task nameed "init". "init" is responsible for building out the remainsing subdirectories that you need. I usually have init create a directory build/ for any generated files, build/classes/ for the class files that correspond to my code under src/, and dist/ for any jar files that I generate. After init runs, then the compile task runs, compiling each of the source files and placing them in a tree that corresponds to their package names under build/classes/.

Now, after going back and fixing some syntax errors, I want to try running my program. I use the command "ant run" and ant will set up the appropriate classpath, then execute my program. Finally, I want to bundle up my program in a jar file. I can issue the command "ant jar" and ant will build my jar file.

"ant -projecthelp" will list all the possible tasks that have been defined for this project.

Putting it Together

Here is a sample development environment demoing both packages and Ant. I'm not sure if slashdot will let me get away with uuencoding.

begin 755 HelloWorld_src.zip
M4$L#!`H````(`&&>)#40OF:J[0$``'0&```)````8G5I;&0N>&ULC56Y;MPP
M$.T-^!]H(JW(I-^UBS1I`J1S$\#@BB.'NQ0E#*F%#^3?,Q0EKJ1HCXX<SKSW
MYI(V3V^U94=`;QJWY=_$5\Z>'N_O-BTV>R@#<ZJ&+?\!UC;/#5K-F89*=39L
M>=G4K;'`R?W^CK$8T@*&]R%FKU!4]"[BE;.CLMT,29`#9_)Q+=9C>3'V)3I\
MF#;%)P05_C"CLRQ16N5]M/)$(>/Y/V_LW-)S>`8+-;C`;%.JT%=GUQFK?_?>
MX+/T&6Y0^`ICU4H+RF5,38`!F#8X(,D12:ZY:.,#'RD2["J)<29DCOI`L1<I
M)AZW,HQ]ILZWX+1?<.[5497IS%@N)4*UUHK1C_K7:Q"23MFJP8<5\2.3[*FN
M"^X'*XN=C.F@%]E.>5@AFNJ(XY=J)+]\SF;Y[P@5RZF<J<@[6WJK"@'-KJ-6
M)D$_E7'%]TB29YE4B==:&2L0CK4Y@/"J;BU4#=)!3!=.SL$?BN(B6<]3_(K5
M'LF6&$5QRD`N4HA5QNLUIN4K8NO.#`4]GXHLSA=VMNC3PL);:3L]YI3:-$UC
MX=##G=XWD@1<3X*6__R@#*.=1CIOPHU-.U6;W@Y;'K"#B7&^)O-OT"24F!H'
MB`T."&S2MRCNIA2+^3[$VR)%,IT;]2N)K$A<^:+0-S+]3OK;/U!+`P0*````
M```IGB0U````````````````!````'-R8R]02P,$"@``````#IXD-0``````
M``````````@```!S<F,O8V]M+U!+`P0*```````.GB0U````````````````
M#@```'-R8R]C;VTO9VUA:6PO4$L#!`H```````Z>)#4````````````````6
M````<W)C+V-O;2]G;6%I;"]R979M:6ME+U!+`P0*``````!$GB0U````````
M````````(P```'-R8R]C;VTO9VUA:6PO<F5V;6EK92]S86UP;&5F;W)S86TO
M4$L#!`H````(`$*>)#5>S$WLA@```*D````R````<W)C+V-O;2]G;6%I;"]R
M979M:6ME+W-A;7!L969O<G-A;2](96QL;U=O<FQD+FIA=F$UC$T*PC`0A?>!
MW&'HJ@7)!7H!]UVX$!?3=`RADQ^2M"#BW1VK/AZ\@?GX,MH5'8%-P;B`GDVA
M/?B53,60F>ZIR#%JE;>9O07+6"N<B3E=4N$%GEJ!Y/>N#9O,GOP"8HO]U(J/
M[GH#+*X.?_J3Z5$;!9.V9K(PC6/?'=X3'.9N&+_P2ROI&U!+`0(4`PH````(
M`&&>)#40OF:J[0$``'0&```)``````````````"D@0````!B=6EL9"YX;6Q0
M2P$"%`,*```````IGB0U````````````````!````````````!``[4$4`@``
M<W)C+U!+`0(4`PH```````Z>)#4````````````````(````````````$`#M
M038"``!S<F,O8V]M+U!+`0(4`PH```````Z>)#4````````````````.````
M````````$`#M05P"``!S<F,O8V]M+V=M86EL+U!+`0(4`PH```````Z>)#4`
M```````````````6````````````$`#M08@"``!S<F,O8V]M+V=M86EL+W)E
M=FUI:V4O4$L!`A0#"@``````1)XD-0```````````````",````````````0
M`.U!O`(``'-R8R]C;VTO9VUA:6PO<F5V;6EK92]S86UP;&5F;W)S86TO4$L!
M`A0#"@````@`0IXD-5[,3>R&````J0```#(``````````````*2!_0(``'-R
M8R]C;VTO9VUA:6PO<F5V;6EK92]S86UP;&5F;W)S86TO2&5L;&]7;W)L9"YJ
9879A4$L%!@`````'``<`T`$``-,#````````
`
end

The uuencode almost works. Pull out the random spaces and everything should be ok.

This discussion has been archived. No new comments can be posted.

For StB: Some practical Java info...

Comments Filter:

Intel CPUs are not defective, they just act that way. -- Henry Spencer

Working...