JavaLanguage Section Index | Page 4
How do I configure a default handler for exceptions that happen in all threads?
The setDefaultUncaughtExceptionHandler() method of Thread, introduced in JDK 5.0, allows you to configure this behavior by letting you attach an implementation of Thread.UncaughtExceptionHandler. more
How do I create a method that accepts a variable number of arguments?
Variable arity or vararg methods were added to JDK 5.0. The last argument to a method can be declared as Type... name, where Type is the Java class type for the argument, and name is how it is ac...more
Where can I find Java 6, aka Mustang?
Early access versions of Mustang are available at https://mustang.dev.java.net/.
Is null a keyword?
Simple answer. No. See http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html for a complete set of keywords. true and false are not keywords either. They are reserved words in t...more
Using Reflection, how do I find out if a field is enumerated ?
The java.lang.reflect.Field class has an isEnumConstant() method for just such a check.
How do I access environment variables? -- 10.31.05
How do I access environment variables?
How do I loop through a set of enum values in 1.5+?
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
for (Suit suit : Suit.values()) {
System.out.println(suit);
}
How do you clear a system property?
Prior to JDK 1.5, you couldn't. With 1.5, you can use the clearProperty method of System. Provided you have write PropertyPermission on the key, this will succeed.
With what precision can you measure time intervals?
The nanoTime() method of System allows you to measure time in nanoseconds, provided this level of precision is available on the current platform. Mechanical overflow will happen if you try to test...more
How can I customize the output of a stack trace?
The Throwable class has a getStackTrace() method that returns an array of StackTraceElement objects. With an element, you can then print line numbers, file names, and method names in any style you...more
How can I replace what is sent in a stack trace to "hide" internal details of a class library?
In addition to the typical obfuscation options, you can use the setStackTrace() method of Throwable to replace the StackTraceElement array. If you really want to, you can "move" line numbers and r...more
How do I change the starting directory for running a command with ProcessBuilder?
Use the directory() method of ProcessBuilder to change the starting / working directory. Once setting the directory, start() the sub-process.
ProcessBuilder processBuilder = new ProcessBuilder...more
How do I enable assertions in only a single package?
Use the -enableassertions, or -ea, command line switch, as in
java -ea:com.example.other MyClass.
What's the difference between Runtime.exec() and ProcessBuilder?
JDK 5.0 added a new class, ProcessBuilder to do the equivalent of the exec() method of Runtime. In addition to running commands in forked off subprocesses, you can adjust environment variables and...more
When using ProcessBuilder, how do I set an environment variable in the subprocess?
Get the set of environment variables by getting a Map with the environment() method. Then, add variables with the put(key, value) method of Map.
ProcessBuilder processBuilder = new ProcessBuil...more