Category Archives: Java

Java – Runtime.getRuntime().addShutdownHook()

a useful method when program done and run last step task.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 
 
public class AddShutdownHookTest {
 
	static class Message extends Thread {
 
		public void run() {
			System.out.println("Bye.");
		}
	}
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
 
			// register Message as shutdown hook
			Runtime.getRuntime().addShutdownHook(new Message());
 
			// print the state of the program
			System.out.println("Program is starting...");
 
			// cause thread to sleep for 3 seconds
			System.out.println("Waiting for 3 seconds...");
			Thread.sleep(3000);
 
			// print that the program is closing
			System.out.println("Program is closing...");
 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

Tomcat Src – tomcat logging

This is starting of tomcat source code analysis.

Tomcat Log is simple.

the following is main example on how to invoke tomcat logging

1
2
3
4
5
6
7
8
9
10
11
12
13
 
package main;
 
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
 
public class tomcat {
	public static void main(String[] args) {
		Log log = LogFactory.getLog(tomcat.class);
		log.info("this is a info message from tomcat");
		log.debug("this is a debug message from tomcat");
	}
}

but when debugging tomcat src, I find that org.apache.juli.ClassLoaderLogManager is invoked.

Its entry is in catalina.sh as starting up script for tomcat.

1
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager

Anypoint Studio Frozen Problem when starting on Mac Platform

Anypoint Studio Frozen Problem when starting on Mac Platform

solution: most important is to install jdk 1.8.0 151, this is max version of anypoint studio that support.
any version older that 151 would fail to work.

Edit target AnypointStudio.ini
AnypointStudio.app/Contents/Eclipse/AnypointStudio.ini

use
-vm
/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/bin
to point to JAVA_HOME of jdk 1.8 151

use
-clean to clean up workspace

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-clean
-startup
../Eclipse/plugins/org.eclipse.equinox.launcher_1.3.100.v20150511-1540.jar
-vm
/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/bin
--launcher.library
../Eclipse/plugins/org.eclipse.equinox.launcher.cocoa.macosx.x86_64_1.1.300.v20150602-1417
-vmargs
-Xms512m
-Xmx1536m
-XX:MaxPermSize=512m
-Dosgi.instance.area.default=@user.home/AnypointStudio/workspace
-Dhttps.protocols=TLSv1.1,TLSv1.2
-Djava.awt.headless=true
-XstartOnFirstThread
-Dorg.eclipse.swt.internal.carbon.smallFonts

Spring Tomcat – integration error

Spring with Tomcat

add spring security, then restart tomcat get the following error.

1
2
3
4
5
6
7
8
9
10
11
12
Caused by: java.util.zip.ZipException: invalid LOC header (bad signature)
	at java.util.zip.ZipFile.read(Native Method)
	at java.util.zip.ZipFile.access$1400(ZipFile.java:60)
	at java.util.zip.ZipFile$ZipFileInputStream.read(ZipFile.java:717)
	at java.util.zip.ZipFile$ZipFileInflaterInputStream.fill(ZipFile.java:419)
	at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
	at sun.misc.IOUtils.readFully(IOUtils.java:65)
	at java.util.jar.JarFile.getBytes(JarFile.java:425)
	at java.util.jar.JarFile.getManifestFromReference(JarFile.java:193)
	at java.util.jar.JarFile.getManifest(JarFile.java:180)
	at org.apache.catalina.webresources.AbstractSingleArchiveResourceSet.initInternal(AbstractSingleArchiveResourceSet.java:140)
	... 13 more

try to build on console.

$ mvn package.

if mvn says some jar/war is interuptted,
then delete these files and let mvn to download them again

Spring 5 has to default a passwordEncoder

However, if passwordEncoder is set to be NoOpPasswordEncoder to ‘ignore’ passwordEncorder

1
2
3
<bean id="passwordEncoder"
class="org.springframework.security.crypto.password.NoOpPasswordEncoder"
factory-method="getInstance">

Lucene – Build Lucene and import to eclipse.

This is first article on Lucene source code analysis.
There are two ways to get source code, one of them are download directly form lucene’s WebSite http://www.apache.org/dyn/closer.lua/lucene/java/6.2.1

you could choose to download lucene-6.2.1-src.tgz and release binary build lucene-6.2.1.tgz .

However, lucene-6.2.1-src.tgz are not easy to import to eclipse.run “ant -p” to check all commands and “ant eclipse” command is not ready yet. you have to check out https://github.com/apache/lucene-solr. From readme you could find the following message
“To compile the sources run ‘ant compile’
To run all the tests run ‘ant test’
To setup your ide run ‘ant idea’, ‘ant netbeans’, or ‘ant eclipse’”

install ivy for ant.
Download ivy file such as “apache-ivy-2.4.0-bin.zip” and put to ant library file.

after git clone from https://github.com/apache/lucene-solr, run “ant eclipse” and then run eclipse , “Import” -> “Existing Projects into workspace”