Java Reference
In-Depth Information
before the
f
, the manifest file name,
x.mf
, comes before the file name. This com-
mand inserts into jar file
file-name.jar
the fact that method
main
appears in
class
MainClass
.
You can do both steps together —insert the classes and the main-class indi-
cation— by using the command:
jar -cmf x.mf file-name.jar *.class
You can email file
file-name.jar
to anyone, and they can run it on their
computer, whether it has a Unix, Macintosh, or Windows system, as long as their
system has Java in it. In some systems, you can run the program just by double-
clicking on the jar file. Otherwise, type this line (include the extension
.jar
):
java -jar file-name.jar
If you want to see what is in jar file
file-name.jar
, then type this:
jar tvf file-name.jar
You can find out more about the jar command by typing simply
jar
.
I.5
Java error messages
Method
main
of the class shown below tries to print the value of
5/0
:
public class
Ex {
public static void
main(String[] args)
{ System.out.println(5 / 0); }
}
Division by
0
is not defined, so the attempt to divide by
0
is an error. Java han-
dles this error by
throwing an exception
, which causes the program to terminate
abnormally, with the following messages in the Java console:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Ex.main(Ex.java:3)
(The first part of the first line, which says that an exception occurred in
thread
main
, may be missing, depending on which IDE you use.) The important
information on the first line is that an
ArithmeticException
occurred, a divi-
sion by zero. The second line says where the exception occurred: in method
main
of class
Ex
, on line 3 of file
Ex.java
.
There may be more information following the second line, depending on the
IDE you use, but it is not important and you can disregard it.
The call-stack trace
When the program aborts because of an exception, you have to study the
program to find out why and correct the error. The Java console messages tell
you the kind of exception that occurred and the method that was being executed
Search WWH ::
Custom Search