Easy to Learn Java: Programming Articles, Examples and Tips

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

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
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: 12

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


Bookmark and Share

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


Table of Contents

1. How can Java program can catch when someone
sends a "kill" to my app in Unix or does a Ctrl-C in windows?
2. Does it possible to have two thread running at the same time
which reads from the same socket...
3. How do I launch a native Document by its Associated MIME Type?
4. Why cannot I cast from double to java.lang.Object?
5. Can anyone write me a short method that lets me know what files
are in a particular directory?
6. I'm researching methods by which one JVM can interact with
another JVM, which is running on the same machine...
7. I want to execute a command whose argument (a filename)
has spaces in it.
******************************************************************

Hello dear friends!

We keep our promise and today we glad to announce that our first
version of "100 Java Tips" E-Book is ready. From the beginning
only Windows users can read this book, but we are going to
prepare pdf-version (Acrobat Reader) soon also. You can read
about book and download from page:
http://javafaq.nu/java/advert/our_book.shtml

We are planning regularly update this book (every 2-3 weeks). So
one nice beautiful sunny day you will discover that your
"100 Java Tips" become "100's Java Tips"! Please feel free to
present it to your friends, colleagues and just good people!

******************************************************************

Tip 1

I would like to know how my Java program can catch when someone
sends a "kill" to my app in Unix or does a Ctrl-C in windows?

In Unix there is atexit() function that handles this type of
situation. Is this possible in Java ?

Answer: Starting with 1.3 there is Runtime.addShutdownHook().
This is for cleanup only.
from API: "A shutdown hook is simply an initialized but unstarted
thread. When the virtual machine begins its shutdown sequence it
will start all registered shutdown hooks in some unspecified order
and let them run concurrently. When all the hooks have finished it
will then run all uninvoked finalizers if finalization-on-exit has
been enabled.
Finally, the virtual machine will halt. Note that daemon threads
will continue to run during the shutdown sequence, as will
non-daemon threads if shutdown was initiated by invoking the
exit method. Once the shutdown sequence has begun it can be
stopped only by invoking the halt method, which forcibly
terminates the virtual machine.
Once the shutdown sequence has begun it is impossible to register
a new shutdown hook or de-register a previously-registered hook.
Attempting either of these operations will cause an
IllegalStateException to be thrown.
Shutdown hooks should also finish their work quickly. When a
program invokes exit the expectation is that the virtual machine
will promptly shut down and exit. When the virtual machine is
terminated due to user logoff or system shutdown the underlying
operating system may only allow a fixed amount of time in which
to shut down and exit. It is therefore inadvisable to attempt
any user interaction or to perform a long-running computation in
a shutdown hook."
more read here: http://java.sun.com/j2se/1.3/docs/api/index.html

******************************************************************

Tip 2

Does it possible to have two thread running at the same time
which reads from the same socket. If a message is received, does
both threads then receive it?

Answer: Two threads can read data from the same socket input
stream, but they WON'T each get copies of the same data. They'll
each get separate parts of the message.

******************************************************************

Tip 3

How do I launch a native Document by its Associated MIME Type?
For example, I would like to ask the 'operating system' what
application is associated with .DOC and then launch it.

Answer: On WinNt,

String docName = "c:someyourdirnameofdoc.doc";
Runtime.getRuntime().exec("cmd.exe /c " + docName);

Tip 4

Why cannot I cast from double to java.lang.Object?

Q: I'm trying to build a vector, however, one of the objects that
I'm passing to the vector is of type double. How do I cast the
double as an object so that I may insert the value into a vector?
Does this make sense? Here is the following snippet of code I was
trying to use:

myVector.add (1, (Object)myDouble);

Of course when I try to compile I get the following message:

Invalid cast from double to java.lang.Object

Could someone please explain why? I realize that Object is the
mother of all objects and therefore ANY reference data type "is an"
Object. So therefore I shouldn't have to cast the double, right?

Answer: A double is not a reference type, but a primitive one.
Hence, it doesn't inherit from Object (or anything else, for that
matter). To put primitives (byte, short, int, long, float, double,
boolean, char) into something that requires an Object, use
Java's wrapper classes.

The wrapper classes are Double, Integer, Long, Boolean, etc., and
are basically an object "wrapped" around a primitive type. You
make a Double object by:
Double d = new Double (myDouble);

and to get the actual value back,

double z = d.doubleValue();

It works the same way for all the rest of the primitive/wrapper
pairs.

******************************************************************

Tip 5

Q: Can anyone write me a short method that lets me know what files
are in a particular directory?
For example, I want to know that directory, c:mystuff, has files
hello.txt, world.txt, and readme.txt.

Answer: Create a File with the name of the directory whose contents
you are interested in. listFiles () will then give you an array of
File objects, representing both files and directories in that
directory:

File dir = new File ("c:");
File [] contents = dir.listFiles ();

Contents now contain a list of files and directories in c:.
To see which of these are files and which are directories, use
isFile () or isDirectory () on each of them:

for (int i=0; i if (contents[i].isFile()) {
System.out.println (contents [i]. getName ()
+ " is a file");
} else if (contents[i].isDirectory()) {
System.out.println (contents [i]. getName ()
+ " is a directory");
}
}


******************************************************************

Tip 6

Q: I'm researching methods by which one JVM can interact with
another JVM, which is running on the same machine.
I know that there are various network models, which can be applied
if a JVM needs to talk to another one across a network, but in
addition to these (which could I guess be applied to JVMs on the
same machine) I wondered if you knew of a system of JVM
communication that requires less system resources, where the JVMs
are both running on the same system.

Answer: CORBA, RMI, HTTP, sockets....
But if you have no TCP/IP stack on your platform, so for Windows
it could be clipboard...

******************************************************************

Tip 7

Q: I've run into a somewhat exotic problem. I want to execute a
command whose argument (a filename) has spaces in it. Unix allows
it [windows even encourages it, what with Program Files etc] e.g.

cat 'silly file.txt'

works in both sh and csh. However, those quotes (or a before the
space) do not work with Runtime.exec (), because it uses
StringTokenizer with default-delim constructor to parse the command
string. The dflt delims include space, so 'silly file.txt' spawns
two tokens.

Answer: Use the "other" Runtime.exec, i.e. the one that takes an
array of Strings. Pass it your command line, pre-tokenized:

String [] cmd = {"cat", "silly file.txt"};
Runtime.getRuntime (). Exec(cmd);


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

John Andersson, Editor mailto:info@javafaq.nu

Copyright (c) 2000 John Andersson
******************************************************************
* *
* You can find our tips on site also! *
* Please recommend us and our FREE "100 Java Tips" book to your *
* friends and colleagues! *
* http://javafaq.nu/java/advert/our_book.shtml *
* *
******************************************************************


 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