Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java Newsletters Archive: 16

JavaFAQ Home » Java Newsletters Go to all tips in Java Newsletters

Bookmark and Share


* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* > The Java FAQ Daily Tips, weekly publication < *
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* *
* Issue No: 16 20 December 2000
* http://www.javafaq.nu/java
* Please recommend our FREE "100 Java Tips" book and us to your
* friends and colleagues!
* http://javafaq.nu/java/advert/our_book.shtml

Table of Contents

1. I want to use more fonts that which is not avilable in the
present jdk...
2. What're the differences between classpath and import...
3. What must be done to reduce size of a jar file?
4. Is it cls-like command in DOS (CMD) window? I would like
to clean the screen the user sees?
5. What is difference between Iterator and Enumeration?
6. How can I find the first dimension length of the
2-dimenstions array?
7. I don't know anything about security...

Hello dear friends!

Merry Christmas! And wish you to have good holidays!
You can to present your colleagues very good Christmas gift -
our "100 Java Tips" book!
If you have your good tips send us and we will add it to our free
book! One sunny day when your friends will load the new version of
our book they will find your name there also!


Tip 1 Q: I want to use more fonts in my applet... say for example
Arial... which is not avilable in the present jdk package...
How can i deal with it?

answer: import java.awt.Toolkit;
Toolkit tools : new Toolkit();
String[] fontList = tools.getFontList();


Tip 2 Q: What're the differences between classpath and import in
the java application?

Do I need to have the package in the classpath first before
importing in a java application or need not?

Answer: Classpath is an environment variable of your OS, you have
to set it (or better: Java sets it) to tell Java where to search
for classes. 
You use import in a program to let Java search for the specified
classes within the classpath. This implies that the package must
be in the classpath.
Stephan Effelsberg


Tip 3 Q: What must be done to reduce size of a jar file?
What optimization techniques to use on classes inside the jar file?
What tools if any?

Answer: A JAR file is a ZIP archive. You can influence its size by
choosing the degree of compression you want to have. This is
usually defined by a value between 0 (no compression) and 9
(maximum compression). Although JAR tool does not list a -9 switch,
you might want to create compressed JARs with any ZIP tool like
Winzip or the free Info-ZIP zip command line tool. 
The amount of reduction you get totally depends on the nature of
your data. 
Note that if you use compression in your JAR file, loading goes
slower (classes must be decompressed).


Tip 4 Q: Is it cls-like command in DOS (CMD) window? I would like
to clean the screen the user sees?

Answer: Unfortunately there is no command as "cls", but try this

for (int i=0; i<25; i++) System.out.println();

It will print 25 empty lines and they will move current printouts

Tip 5 Q: What is difference between Iterator and Enumeration?

First of all Java FAQ Team wish you !!!HAPPY NEW YEAR!!! and then
Answer: from

The object returned by the iterator method deserves special
mention. It is an Iterator, which is very similar to an Enumeration,
but differs in two respects: 
Iterator allows the caller to remove elements from the underlying
collection during the iteration with well-defined semantics. 
Method names have been improved. 
The first point is important: There was no safe way to remove
elements from a collection while traversing it with an Enumeration.
The semantics of this operation were ill-defined, and differed from
implementation to implementation. 
The Iterator interface is shown below: 

public interface Iterator {
boolean hasNext();
Object next();
void remove(); // Optional

The hasNext method is identical in function to
Enumeration.hasMoreElements, and the next method is identical in
function to Enumeration.nextElement. The remove method removes
from the underlying Collection the last element that was returned
by next. The remove method may be called only once per call to
next, and throws an exception if this condition is violated. Note
that Iterator.remove is the only safe way to modify a collection
during iteration; the behavior is unspecified if the underlying
collection is modified in any other way while the iteration is
in progress. 
The following snippet shows you how to use an Iterator to filter
a Collection, that is, to traverse the collection, removing
every element that does not satisfy some condition: 

static void filter(Collection c) {
for (Iterator i = c.iterator(); i.hasNext(); )
if (!cond(i.next()))

Two things should be kept in mind when looking at this simple
piece of code: 
1. The code is polymorphic: it works for any Collection that
supports element removal, regardless of implementation. That's
how easy it is to write a polymorphic algorithm under the
collections framework! 
2. It would have been impossible to write this using Enumeration
instead of Iterator, because there's no safe way to remove an
element from a collection while traversing it with an Enumeration.


Tip 6 Q: How can I find the first dimension length of the
2-dimenstions array? I have use the array[].length but it does
not work, how can I solve this problem? 

Answer: Java doesn't really have "multidimensional arrays",
only arrays of arrays. So try: array[0].length and you will get
this dimension.


Tip 7 Q: I need to read C: est.doc for example and store it
somewhere for my future use. I don't know anything about security.
Part 1.

Answer: If you want to read a file on the local file system in
an applet, you are going to digitally sign the applet, and the
user of the applet is going to indicate trust in the signature.
How you go about this depends on a number of questions, like:
In which browser(s) will the users be running the applet?
Will a Java plug-in be installed in the browser?
Can the user install a plug-in?

Here are a few tips on some of the sticky points of signing

If you are using the jar signing tools that come with the JDK,
you'll find that they will only work with the Java plug-in.
Netscape and Microsoft IE don't salute the Sun way of signing

If you can rely on your users to install the Java plug in, you
can sign applets using the JDK 1.1.X javakey command. You will
also have to have them import your certificate into their
'identitydb.obj' file in their home directory, and mark it as
trusted. If importing the certificate is too much to ask, and
it usually is, you could create an 'identitydb.obj' containing
the certificate, already marked as trusted, and have them
download this to their home directory. Be careful not to
distribute the 'identitydb.obj' that you used to sign the jar,
since that would allow anyone to sign code as you!

JDK1.2.X works in a similar way, but the 'javakey' command has
been replaced by 'jarsigner' and 'keytool'. In addition, 1.2
adds granularity to the security model, so that you can assign
privileges outside of the sandbox via a policy file. For example
you could allow an applet to read files, but not write them.
JDK 1.1.X security is kind of an all or nothing proposition.

If you can't count on the Plugin being there, but you know your
users will be using Netscape, you can sign use the Netscape
signtool. Netscape's signtool is a little different than Sun's.
With Netscape's, you put all of your class files into a
directory, like signdir, and then run something like this: 
./signtool -k MyCert -Z MyApplet.jar signdir
The 'MyApplet.jar' file will be created, and, of course, you
would already have to have MyCert in your security database.
You can generate a test certificate through signtool, but it's
only good for a couple of months.
Joe Morrogh - Excite@Home -
The Leader in Broadband http://home.com/faster
Part 2 of this tip will be published next week!

The Java FAQ Daily Tips is a newsletter that is only sent to those
who have specifically subscribed to it.

John Andersson,

Copyright (c) 2000 John Andersson

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy