| |||||||||||
| |||||||||||
Annotated log
Java - Basic Tools example from a Well House Consultants training course
More on Java - Basic Tools [link]
Source code: javalog.001 Module: A502
# Annotated log of command line Java compiles and runs
-----> Which Java runtime engine is the default? wizard:~ graham$ java -version java version "1.6.0_20" Java(TM) SE Runtime Environment (build 1.6.0_20-b02-279-10M3065) Java HotSpot(TM) 64-Bit Server VM (build 16.3-b01-279, mixed mode) wizard:~ graham$ -----> Which Java compilr is the default? wizard:~ graham$ javac -version javac 1.6.0_20 wizard:~ graham$ -----> Edit source file (type in a new java program) Compile it via javac Run it via java (note - .java on the compile, but no extension as you run wizard:java graham$ vi Annie.java wizard:java graham$ javac Annie.java wizard:java graham$ java Annie Java refresh! wizard:java graham$ -----> What files do we now have? Source in Annie.java Class file (compiled code which we ran from) in Annie.class wizard:java graham$ ls Annie.class Annie.java wizard:java graham$ -----> What if we want to edit one source file to make another class source? Really we should use inheritance, but if we insist we need to edit source as the file must have the same name as the class wizard:java graham$ cp Annie.java Bella.java wizard:java graham$ javac Bella.java Bella.java:2: class Annie is public, should be declared in a file named Annie.java public class Annie { ^ 1 error wizard:java graham$ vi Bella.java ------> What if you call another method? It must exist at compile time! wizard:java graham$ javac Bella.java Bella.java:5: cannot find symbol symbol : method walk() location: class Bella walk(); ^ Bella.java:7: cannot find symbol symbol : method walk() location: class Bella walk(); ^ 2 errors wizard:java graham$ vi Bella.java -----> A proper compile and run! wizard:java graham$ javac Bella.java wizard:java graham$ java Bella Who's knicked me shoes? I'll have to drive Java refresh! Who's knicked me shoes? I'll have to drive wizard:java graham$ ------> Create another class which calls an external reference That reference must be available for checking at compile time wizard:java graham$ cp Bella.java Chloe.java wizard:java graham$ vi !$ vi Chloe.java wizard:java graham$ javac Chloe.java Chloe.java:6: cannot find symbol symbol : variable Debbie location: class Chloe Debbie.run(); ^ Chloe.java:8: cannot find symbol symbol : variable Debbie location: class Chloe Debbie.run(); ^ 2 errors wizard:java graham$ vi Chloe.java wizard:java graham$ vi Debbie.java ------> Notice how I compile Chloe.java ... and internally it decides that it must compile Debbie too. Java is case sensitive ... protected and not Protected !! wizard:java graham$ javac Chloe.java ./Debbie.java:2: class, interface, or enum expected Protected class Debbie { ^ 1 error wizard:java graham$ ------> Correct Debbie, recompile Chloe, and it works! wizard:java graham$ vi Debbie.java wizard:java graham$ javac Chloe.java wizard:java graham$ java Chloe Who's knicked me shoes? I'll have to drive Puffered! Java refresh! Puffered! Who's knicked me shoes? I'll have to drive wizard:java graham$ --------> SECTION ON .jar FILES ------> Put all the class files into a jar wizard:java graham$ ls Annie.class Bella.class Chloe.class Debbie.class Annie.java Bella.java Chloe.java Debbie.java wizard:java graham$ jar cf jam.jar *.class --------- and CAREFULLY remove all the .class files wizard:java graham$ ls -l total 72 -rw-r--r-- 1 graham staff 417 7 Jul 10:30 Annie.class -rw-r--r-- 1 graham staff 240 7 Jul 10:31 Annie.java -rw-r--r-- 1 graham staff 547 7 Jul 10:35 Bella.class -rw-r--r-- 1 graham staff 411 7 Jul 10:35 Bella.java -rw-r--r-- 1 graham staff 589 7 Jul 10:39 Chloe.class -rw-r--r-- 1 graham staff 650 7 Jul 10:43 Chloe.java -rw-r--r-- 1 graham staff 389 7 Jul 10:39 Debbie.class -rw-r--r-- 1 graham staff 96 7 Jul 10:39 Debbie.java -rw-r--r-- 1 graham staff 2122 7 Jul 10:47 jam.jar wizard:java graham$ rm *.class wizard:java graham$ ls Annie.java Bella.java Chloe.java Debbie.java jam.jar ------> Try to run one of the classes and it won't work wizard:java graham$ java Chloe Exception in thread "main" java.lang.NoClassDefFoundError: Chloe Caused by: java.lang.ClassNotFoundException: Chloe at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) -----> Set the CLASSPATH to point to the jar and you can now run straight from the jar. wizard:java graham$ export CLASSPATH=jam.jar wizard:java graham$ ls Annie.java Bella.java Chloe.java Debbie.java jam.jar wizard:java graham$ java Chloe Who's knicked me shoes? I'll have to drive Puffered! Java refresh! Puffered! Who's knicked me shoes? I'll have to drive wizard:java graham$ ------> Alter the original source, recomiple and run .... and you'll find the old code still running because your class path only points at the .jar wizard:java graham$ vi Bella.java wizard:java graham$ javac !$ javac Bella.java wizard:java graham$ java Bella Who's knicked me shoes? I'll have to drive Java refresh! Who's knicked me shoes? I'll have to drive wizard:java graham$ ls Annie.java Bella.class Bella.java Chloe.java Debbie.java jam.jar ------> Modify the CLASSPATH so it looks in the current directory ahead of anythin else in the old path. wizard:java graham$ export CLASSPATH=.:$CLASSPATH wizard:java graham$ java Bella Who's removed me shoes? I'll have to drive Java refresh! Who's removed me shoes? I'll have to drive ------> Check that the stuff from the jar also works! wizard:java graham$ java Annie Java refresh! wizard:java graham$ ls Annie.java Bella.class Bella.java Chloe.java Debbie.java jam.jar ------> Look and see what's in the jar! wizard:java graham$ jar tf jam.jar META-INF/ META-INF/MANIFEST.MF Annie.class Bella.class Chloe.class Debbie.class wizard:java graham$ jar tvf jam.jar 0 Wed Jul 07 10:47:04 BST 2010 META-INF/ 60 Wed Jul 07 10:47:06 BST 2010 META-INF/MANIFEST.MF 417 Wed Jul 07 10:30:46 BST 2010 Annie.class 547 Wed Jul 07 10:35:00 BST 2010 Bella.class 589 Wed Jul 07 10:39:54 BST 2010 Chloe.class 389 Wed Jul 07 10:39:54 BST 2010 Debbie.class -------> Run the class that's in the current directory. Delete it, run again, and you'll find that it runs the class that's in the jar which was being masked previously. wizard:java graham$ java Bella Who's removed me shoes? I'll have to drive Java refresh! Who's removed me shoes? I'll have to drive wizard:java graham$ rm Bella.class wizard:java graham$ java Bella Who's knicked me shoes? I'll have to drive Java refresh! Who's knicked me shoes? I'll have to drive wizard:java graham$ Learn about this subject
This module and example are covered on the following public courses:
* Deploying Apache and Tomcat * Deploying Java Applications on Linux / Unix Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering Lamp deployment are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Java - Basic Tools" training module. You'll find a description of the topic and some
other closely related examples on the "Java - Basic Tools" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Web site author
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php4 • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |