I get the error: “Only a type can be imported. XYZ resolves to a package.” Someone has explained the cause here but I am not sure what I supposed to do to fix this. FYI: I am using Eclipse. I have added the code that does the importing below. The java.util.* import works fine. Edit: the actual err…
Suppress deprecated import warning in Java
In Java, if you import a deprecated class: You get this warning: The type SomeDeprecatedClass is deprecated Is there a way to suppress this warning? Answer To avoid the warning: do not import the class instead use the fully qualified class name and use it in as few locations as possible.
Byte order mark screws up file reading in Java
I’m trying to read CSV files using Java. Some of the files may have a byte order mark in the beginning, but not all. When present, the byte order gets read along with the rest of the first line, thus causing problems with string compares. Is there an easy way to skip the byte order mark when it is prese…
Java DNS cache viewer
Is there a way to view/dump DNS cached used by java.net api?
Impossible to make a cached thread pool with a size limit?
It seems to be impossible to make a cached thread pool with a limit to the number of threads that it can create. Here is how static Executors.newCachedThreadPool is implemented in the standard Java library: So, using that template to go on to create a fixed sized cached thread pool: Now if you use this and su…
Java – find the first cause of an exception
I need to check if an exception is caused by some database problem. I receive an Exception and check if its cause contains the “ORA” string and return that (something like “ORA-00001”). The problem here is that the exception I receive is nested inside other exceptions, so if I don̵…
Using Mockito’s generic “any()” method
I have an interface with a method that expects an array of Foo: I am mocking this interface using Mockito, and I’d like to assert that doStuff() is called, but I don’t want to validate what argument are passed – “don’t care”. How do I write the following code using any(), t…
Growing ByteBuffer
Has anyone has ever seen an implementation of java.nio.ByteBuffer that will grow dynamically if a putX() call overruns the capacity? The reason I want to do it this way is twofold: I don’t know how much space I need ahead of time. I’d rather not do a new ByteBuffer.allocate() then a bulk put() eve…
How can I write a byte array to a file in Java?
How to write a byte array to a file in Java?
Java: splitting a comma-separated string but ignoring commas in quotes
I have a string vaguely like this: that I want to split by commas — but I need to ignore commas in quotes. How can I do this? Seems like a regexp approach fails; I suppose I can manually scan and enter a different mode when I see a quote, but it would be nice to use preexisting libraries. (edit: