How to list the files inside a JAR file?

CodeSource src = MyClass.class.getProtectionDomain().getCodeSource(); if (src != null) { URL jar = src.getLocation(); ZipInputStream zip = new ZipInputStream(jar.openStream()); while(true) { ZipEntry e = zip.getNextEntry(); if (e == null) break; String name = e.getName(); if (name.startsWith(“path/to/your/dir/”)) { /* Do something with this entry. */ … } } } else { /* Fail… */ } Note that … Read more

implements Closeable or implements AutoCloseable

AutoCloseable (introduced in Java 7) makes it possible to use the try-with-resources idiom: public class MyResource implements AutoCloseable { public void close() throws Exception { System.out.println(“Closing!”); } } Now you can say: try (MyResource res = new MyResource()) { // use resource here } and JVM will call close() automatically for you. Closeable is an … Read more

How to get the current working directory in Java?

Code : public class JavaApplication { public static void main(String[] args) { System.out.println(“Working Directory = ” + System.getProperty(“user.dir”)); } } This will print the absolute path of the current directory from where your application was initialized. Explanation: From the documentation: java.io package resolve relative pathnames using current user directory. The current directory is represented as … Read more

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