Table of contents
Java
java command
run the class boot.Main with the current directory as classpath
java boot.Main -cp .
run a standalone jar
java -jar myjar.jar
show garbage collection
-verbose:gc
Thread dumps
have linux process with id [pid] dump its threads to the console
kill -3 [pid]
jstack [id]
lazy method, simply force all java processes to make thread dumps (not for production)
pkill -3 java
thread dump analysis
http://fastthread.io/
Manifest
specifying a main class
Main-Class: com.example.Type
Unused imports
a simple script for removing unused imports
https://gist.github.com/rodrigosetti/4734557
dealing with warnings
ignoring heap pollution warning
@SafeVarargs
ignoring compiler warnings
@SuppressWarnings("[value]")
valid values
https://stackoverflow.com/questions/1205995/what-is-the-list-of-valid-suppresswarnings-warning-names-in-java
Print JavaBean properties
public static void dump(Object obj) {
Map<String, Object> map = new HashMap<>();
for (Method method : obj.getClass().getMethods()) {
try {
if (method.getName().startsWith("get") && method.getParameters().length == 0) {
method.setAccessible(true);
Object result = method.invoke(obj, new Object[] {});
map.put(method.getName(), result);
}
} catch (Exception e) {
System.out.println("## " + method.getName() + " ##");
e.printStackTrace();
System.out.println();
}
}
map.forEach((key, value) -> System.out.println(key + " => " + value));
}