Skip to content

Instantly share code, notes, and snippets.

View simonwoo's full-sized avatar
💭
I may be slow to respond.

Chong WU simonwoo

💭
I may be slow to respond.
View GitHub Profile
@simonwoo
simonwoo / getResource.md
Last active August 29, 2015 14:19
java classLoader.getResource() and class.getResource() methods
  1. 两个都可以用于从 classpath 里面进行资源读取, classpath包含classpath中的路径和classpath中的jar。
  2. 两个方法的区别是资源的定义不同, 一个主要用于相对与一个object取资源,而另一个用于取相对于classpath的资源,用的是绝对路径。
  3. 在使用Class.getResourceAsStream 时, 资源路径有两种方式, 一种以 / 开头,则这样的路径是指定绝对路径, 如果不以 / 开头, 则路径是相对与这个class所在的包的。

在开发java程序的过程中,我们经常要做的一件事就是获取资源。那么什么是资源呢?说白了,在计算机里那就是一堆数据。只是这堆数据对我们的java程序有多种表现形式,一般来说有File,URL,InputStream等等。而单就文件这一项就有很多种:配置文件,java类文件,jps文件,图片、css、js文件等等。面对这林林总总的资源,我们在设计一个读取资源的接口时,就需要针对不同形式的资源提供方法,这样就导致我们的接口还是与实际的资源形式绑定在一起,未能完全的抽象。另外,在java程序中资源的存放位置也是各异的。有的存放在classpath中,有的存放在文件系统中,有的存放在web应用中。而对于不同位置的资源,java程序获取这些资源的方法各有不同。 A、获取classpath中的资源:

URL url = this.getClass().getResource("resource_name");   
URL url = this.getClass().getClassLoader().getResource("resource_name"); 
@simonwoo
simonwoo / try-with-resources.md
Last active August 29, 2015 14:19
java try with resources

The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource. The following example reads the first line from a file. It uses an instance of BufferedReader to read data from the file. BufferedReader is a resource that must be closed after the program is finished with it:

static String readFirstLineFromFile(String path) throws IOException {
    try (BufferedReader br =
                   new BufferedReader(new FileReader(path))) {
 return br.readLine();
@simonwoo
simonwoo / equals and ==.md
Last active August 29, 2015 14:19
java compare string
  1. == tests for reference equality.
  2. .equals() tests for value equality.Consequently, if you actually want to test whether two strings have the same value you should use .equals().
  • These two have the same value new String("test").equals("test") --> true

  • but they are not the same object new String("test") == "test" --> false

  • neither are these

@simonwoo
simonwoo / java_subclass.md
Last active August 29, 2015 14:19
create a subclass with a string

Example: Creating Subclasses with a String So far I do not have a great gain; the advantage comes from the fact that I have separated the receiver of the creation call from the class of object created. If I later apply Replace Type Code with Subclasses (Organizing Data) to turn the codes into subclasses of employee, I can hide these subclasses from clients by using the factory method:

   static Employee create(int type) {
       switch (type) {
           case ENGINEER:
              return new Engineer();
           case SALESMAN:
              return new Salesman();
 case MANAGER:
@simonwoo
simonwoo / addclasspath.md
Last active March 16, 2025 16:59
add new class path at runtime in java

There are cases where we need to add classpath at runtime. For example adding jar to classpath, but depends on configuration or some logic at runtime.

The class who responsible for handling classpath list is URLClassloader. You can get current system URLClassLoader with:

URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();

If you check javadoc about this class you’ll see:

There are 3 plugins for generating to create a JAR-File in maven:

  • maven-jar-plugin
  • maven-assembly-plugin
  • maven-shade-plugin

maven-jar-plugin:This plugin provides the capability to build and sign jars.But it just compiles the java files under src/main/java and /src/main/resources/.It doesn't include the dependencies JAR files.

<!--exclude all xml files from the jar-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
@simonwoo
simonwoo / Multiple bindings were found on the class path.md
Created April 22, 2015 13:34
Multiple bindings were found on the class path

SLF4J API is designed to bind with one and only one underlying logging framework at a time. If more than one binding is present on the class path, SLF4J will emit a warning, listing the location of those bindings. When multiple bindings are available on the class path, select one and only one binding you wish to use, and remove the other bindings. For example, if you have both slf4j-simple-1.7.12.jar and slf4j-nop-1.7.12.jar on the class path and you wish to use the nop (no-operation) binding, then remove slf4j-simple-1.7.12.jar from the class path. The list of locations that SLF4J provides in this warning usually provides sufficient information to identify the dependency transitively pulling in an unwanted SLF4J binding into your project. In your project's pom.xml file, exclude this SLF4J binding when declaring the unscrupulous dependency. For example, cassandra-all version 0.8.1 declares both log4j and slf4j-log4j12 as compile-time dependencies. Thus, when you include cassandra-all as a dependency in your p

public static Set<String> findDuplicates(List<String> listContainingDuplicates) {
	final Set<String> setToReturn = new HashSet<String>();
	final Set<String> set1 = new HashSet<String>();

	for (String yourInt : listContainingDuplicates) {
		if (!set1.add(yourInt)) {
			setToReturn.add(yourInt);
		}
	}
/**
* execute shell script
*/
private static void executeScript() {
	try {
	    String bash = "/bin/bash";
	    String script = "script.sh";
	    String[] command = { bash, script };
	

java内存管理包括两个方面:

  • 内存分配--创建内存时JVM为该对象在堆内存所分配的空间。
  • 内存回收--java对象失去引用变成垃圾时,JVM的垃圾回收机制自动清理该对象,并收回该对象的内存。

JVM的垃圾回收机制是由一条后台线程完成。