One of the few annoying things about the Eclipse Java plug-in is the absence of a keyboard shortcut to build the project associated with the current resource. Anyone know how to go about it? Answer In the Preferences dialog box, under the General section is a dialog box called “Keys”. This lets you attach key bindings to many events, including
Regular expression to match URLs in Java
I use RegexBuddy while working with regular expressions. From its library I copied the regular expression to match URLs. I tested successfully within RegexBuddy. However, when I copied it as Java String flavor and pasted it into Java code, it does not work. The following class prints false: Does anyone know what I am doing wrong? Answer Try the following
How do you assert that a certain exception is thrown in JUnit tests?
How can I use JUnit idiomatically to test that some code throws an exception? While I can certainly do something like this: I recall that there is an annotation or an Assert.xyz or something that is far less kludgy and far more in-the-spirit of JUnit for these sorts of situations. Answer It depends on the JUnit version and what assert
Equivalent of PHP’s print_r in Java?
last time I asked how to populate a data structure here. Now I would like to know if there’s something in Java, like the print_r I use in PHP, to represent what I have populated in the Maps and lists without having to do my own algorithm. Any ideas? Answer Calling toString on the collection should return a string containing
Exception thrown inside catch block – will it be caught again?
This may seem like a programming 101 question and I had thought I knew the answer but now find myself needing to double check. In this piece of code below, will the exception thrown in the first catch block then be caught by the general Exception catch block below? I always thought the answer would be no, but now I
How to pretty print XML from Java?
I have a Java String that contains XML, with no line feeds or indentations. I would like to turn it into a String with nicely formatted XML. How do I do this? Note: My input is a String. My output is a String. (Basic) mock result: Answer Now it’s 2012 and Java can do more than it used to with
How can I lock a file using java (if possible)
I have a Java process that opens a file using a FileReader. How can I prevent another (Java) process from opening this file, or at least notify that second process that the file is already opened? Does this automatically make the second process get an exception if the file is open (which solves my problem) or do I have to
What’s the nearest substitute for a function pointer in Java?
I have a method that’s about ten lines of code. I want to create more methods that do exactly the same thing, except for a small calculation that’s going to change one line of code. This is a perfect application for passing in a function pointer to replace that one line, but Java doesn’t have function pointers. What’s my best
Java: Why aren’t NullPointerExceptions called NullReferenceExceptions?
Was this an oversight? Or is it to do with the JVM? Answer Java does indeed have pointers–pointers on which you cannot perform pointer arithmetic. From the venerable JLS: There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). There are, correspondingly, two kinds of data values that can be stored in
Generate a current datestamp in Java
What is the best way to generate a current datestamp in Java? YYYY-MM-DD:hh-mm-ss Answer Using the standard JDK, you will want to use java.text.SimpleDateFormat However, if you have the option to use the Apache Commons Lang package, you can use org.apache.commons.lang.time.FastDateFormat FastDateFormat has the benefit of being thread safe, so you can use a single instance throughout your application. It