How to check if a path points to an existing file with Java 7’s new File API?

Use the Files class: Files.exists(path); EDIT: to answer your subsequent question, I think the reason that the method is in another class is that Path is an interface, and they wanted to provide an implementation (similar to putting sorting methods in the Collections class instead of the List interface). Not directly related to the question, … Read more

Java 7 Automatic Resource Management JDBC (try-with-resources statement)

try(Connection con = getConnection()) { try (PreparedStatement prep = con.prepareConnection(“Update …”)) { //prep.doSomething(); //… //etc con.commit(); } catch (SQLException e) { //any other actions necessary on failure con.rollback(); //consider a re-throw, throwing a wrapping exception, etc } } According to the oracle documentation, you can combine a try-with-resources block with a regular try block. IMO, … Read more

java.security.cert.CertificateException: Certificates does not conform to algorithm constraints

Background MD2 was widely recognized as insecure and thus disabled in Java in version JDK 6u17 (see release notes http://www.oracle.com/technetwork/java/javase/6u17-141447.html, “Disable MD2 in certificate chain validation”), as well as JDK 7, as per the configuration you pointed out in java.security. Verisign was using a Class 3 root certificate with the md2WithRSAEncryption signature algorithm (serial 70:ba:e4:1d:10:d9:29:34:b6:38:ca:7b:03:cc:ba:bf), … Read more

Comparison method violates its general contract! Java 7 only

see this: From http://www.oracle.com/technetwork/java/javase/compatibility-417013.html#source Area: API: Utilities Synopsis: Updated sort behavior for Arrays and Collections may throw an IllegalArgumentException Description: The sorting algorithm used by java.util.Arrays.sort and (indirectly) by java.util.Collections.sort has been replaced. The new sort implementation may throw an IllegalArgumentException if it detects a Comparable that violates the Comparable contract. The previous implementation silently … Read more

“Comparison method violates its general contract!” – TimSort and GridLayout

It seems to me like you’ve hit a bug in the JDK since the error seems to come from Swing classes. Options: Define the property java.util.Arrays.useLegacyMergeSort as true. Either using in your code the line System.setProperty(“java.util.Arrays.useLegacyMergeSort”, “true”); before any Swing code. As the first line in the main method should work. Or adding -Djava.util.Arrays.useLegacyMergeSort=true to … Read more

Close resource quietly using try-with-resources

I found this answered on the coin-dev mailing list: http://mail.openjdk.java.net/pipermail/coin-dev/2009-April/001503.html 5. Some failures of the close method can be safely ignored (e.g., closing a file that was open for read). Does the construct provide for this? No. While this functionality seems attractive, it is not clear that it’s worth the added complexity. As a practical … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)