Java notes
From OriWiki
Contents |
JAR
create a new archive
jar cvf my_archive.jar *.class
extract an archive
jar xvf my_archive.jar
In order to create a jar that contains a package, one needs to archive the complete folder with its internal hierarchy:
jar cvf my_package.jar my_package/
or
jar cvf my_package.jar my_package/path/to/files/*.class
(this still preserves the folder's hierarchy)
Class path
The environment variable CLASSPATH defines all the paths that javac and java treats as root_dir for packages.
Note that in DOS the set command shouldn't involve spaces:
set CLASSPATH=%CLASSPATH%;c:\my\new\path
and not:
set CLASSPATH = %CLASSPATH%;c:\my\new\path
One can add path also at compilation & execution:
javac -cp c:\progra~1\dtn\iqfeed LookupClient.java
java -cp c:\progra~1\dtn\iqfeed;. LookupClient
== ArrayList == (generics?) import java.util.*;
ArrayList<A> lst = new ArrayList<A>(); lst.add(new A()); A a = lst.get(0); // note: no need for downcast due to <A> a.foo();
reflection
http://java.sun.com/developer/technicalArticles/ALT/Reflection/
A a = new A(); System.out.println(a.getClass().getName());
static public void main(String[] args) throws Exception {
Class c = Class.forName("A");
Method[] ms = c.getDeclaredMethods();
for(int i=0; i<ms.length; ++i)
System.out.print(ms[i]+",");
}
Class ca = Class.forName("A"), cb = Class.forName("B"); A a = new A(); B b = new B(); if(cb.isInstance(a)) System.out.println("a is instacne of B"); if(ca.isInstance(b)) System.out.println("b is instance of A");
import java.lang.reflect.*;
public class Check {
private int f1(
Object p, int x) throws NullPointerException
{
if (p == null)
throw new NullPointerException();
return x;
}
public static void main(String args[])
{
try {
Class cls = Class.forName("Check");
Method methlist[] = cls.getDeclaredMethods();
for (int i = 0; i < methlist.length; ++i) {
Method m = methlist[i];
System.out.println("name = " + m.getName());
System.out.println("decl class = " + m.getDeclaringClass());
Class pvec[] = m.getParameterTypes();
for (int j = 0; j < pvec.length; j++)
System.out.println(" param #" + j + " " + pvec[j]);
Class evec[] = m.getExceptionTypes();
for (int j = 0; j < evec.length; j++)
System.out.println("exc #" + j + " " + evec[j]);
System.out.println("return type = " + m.getReturnType());
System.out.println("-----");
}
}
catch (Throwable e) {
System.err.println(e);
}
}
}
import java.lang.reflect.*;
public class Check {
private double d;
public static final int i = 37;
String s = "testing";
public static void main(String args[]) throws Throwable
{
Class cls = Class.forName("Check");
Field fieldlist[] = cls.getDeclaredFields();
for (int i = 0; i < fieldlist.length; i++) {
Field fld = fieldlist[i];
System.out.println("name = " + fld.getName());
System.out.println("decl class = " +
fld.getDeclaringClass());
System.out.println("type = " + fld.getType());
int mod = fld.getModifiers();
System.out.println("modifiers = " + mod + "," +
Modifier.toString(mod));
System.out.println("-----");
}
}
}
invoke mwthods
import java.lang.reflect.*;
public class Check {
public static void main(String args[]) throws Throwable {
Class<?> c = Class.forName("A"); // the <?> is required to avoid a generic warning..
Class[] params = {Integer.TYPE,Integer.TYPE};
Method m = c.getMethod("add",params);
Object[] as = {new Integer(2), new Integer(3)};
A a = new A();
Object o = m.invoke(a,as);
System.out.println(o);
}
}
class A {
public int add(int a, int b) {
return a + b;
}
}
jdk download
- I've downloaded 1.5 from here

