JDK6.0發(fā)布有段時(shí)間了,新的JDK也有不少新的特性,我去網(wǎng)上搜集了一下,列在下面和大家一起學(xué)習(xí).
1.
Desktop和SystemTray. 在JDK6中
,AWT新增加了兩個(gè)類:Desktop和SystemTray,前者可以用來打開系統(tǒng)默認(rèn)瀏覽器瀏覽指定的URL,打開系統(tǒng)默認(rèn)郵件客戶端給指定的郵箱
發(fā)郵件,用默認(rèn)應(yīng)用程序打開或編輯文件(比如,用記事本打開以txt為后綴名的文件),用系統(tǒng)默認(rèn)的打印機(jī)打印文檔;后者可以用來在系統(tǒng)托盤區(qū)創(chuàng)建一個(gè)托
盤程序。
我隨便找了幾張圖,在Tray里面都是空的,沒有圖,可能是圖太大,有xdjm知道希望告訴我.
import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;


public class DesktopTrayTest ...{
private static Desktop desktop;
private static SystemTray st;
private static PopupMenu pm;

public static void main( String[] args ) ...{

if( Desktop.isDesktopSupported() ) ...{
desktop = Desktop.getDesktop();
}

if( SystemTray.isSupported() ) ...{
st = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().createImage( "http://www.51ppt.com.cn/Article/Uploadphotos/200604/20064147333288.png" );
createPopupMenu();
TrayIcon ti = new TrayIcon( image, "Demo", pm );

try...{
st.add( ti );

} catch( AWTException awte ) ...{
awte.printStackTrace();
}
}
}

public static void sendMail( String mail ) ...{
if( desktop != null &&

desktop.isSupported( Desktop.Action.MAIL ) ) ...{

try ...{
desktop.mail( new URI( mail ) );

} catch (IOException e) ...{
e.printStackTrace();

} catch (URISyntaxException e) ...{
e.printStackTrace();
}
}
}

public static void openBrowser( String url ) ...{
if( desktop != null &&

desktop.isSupported( Desktop.Action.BROWSE )) ...{

try ...{
desktop.browse( new URI( url ) );

} catch (IOException e) ...{
e.printStackTrace();

} catch (URISyntaxException e) ...{
e.printStackTrace();
}
}
}

public static void edit() ...{
if( desktop != null &&

desktop.isSupported( Desktop.Action.EDIT ) ) ...{
File file = new File( "test.txt" );

try ...{

if( file.exists() == false ) ...{
file.createNewFile();
}
desktop.edit( file );

} catch( IOException ioe ) ...{
ioe.printStackTrace();
}
}
}

public static void createPopupMenu() ...{
pm = new PopupMenu();
MenuItem ob = new MenuItem( "Open url" );

ob.addActionListener( new ActionListener() ...{

public void actionPerformed( ActionEvent ae ) ...{
openBrowser( "http://blog.csdn.net/xumingming64398966" );
}
});
MenuItem sm = new MenuItem( "Send Mail" );

sm.addActionListener( new ActionListener() ...{

public void actionPerformed( ActionEvent ae ) ...{
sendMail( "64398966@qq.com" );
}
});
MenuItem ed = new MenuItem( "Edit" );

ed.addActionListener( new ActionListener() ...{

public void actionPerformed( ActionEvent ae ) ...{
edit();
}
});
MenuItem ex = new MenuItem( "Exit" );

ex.addActionListener( new ActionListener() ...{

public void actionPerformed( ActionEvent ae ) ...{
System.exit( 0 );
}
});
pm.add( ob );
pm.add( sm );
pm.add( ed );
pm.addSeparator();
pm.add( ex );
}
}

2.
Console. JDK6中提供了java.io.Console類專用來訪問基于字符的控制臺(tái)設(shè)備.
你的程序如果要與Windows下的cmd或者Linux下的Terminal交互,就可以用Console類代勞.
但我們不總是能得到可用的Console, 一個(gè)JVM是否有可用的Console依賴于底層平臺(tái)和JVM如何被調(diào)用.
如果JVM是在交互式命令行(比如Windows的cmd)中啟動(dòng)的,并且輸入輸出沒有重定向到另外的地方,那么就可以得到一個(gè)可用的Console實(shí)
例. 下面代碼演示了Console類的用法:
import java.io.Console;


public class ConsoleTest ...{

public static void main( String[] args ) ...{
Console console = System.console();

if( console != null ) ...{
String user = new String( console.readLine( "Enter User:", new Object[ 0 ] ) );
String pwd = new String( console.readPassword( "Enter Password:", new Object[ 0 ] ));

console.printf( "User name is:%s", new Object[]...{user} );

console.printf( "Password is:%s", new Object[]...{pwd} );

} else ...{
System.out.println( "No Console!" );
}
}
}

你如果是在一個(gè)IDE中如eclipse, netbeans中運(yùn)行你將得到:
No Console!
因?yàn)橹挥性诿钚兄胁拍艿玫紺onsole對(duì)象。
3.
Compiler API. 現(xiàn)在我們可以用JDK6 的Compiler API(JSR 199)去動(dòng)態(tài)編譯Java源文件,Compiler
API結(jié)合反射功能就可以實(shí)現(xiàn)動(dòng)態(tài)的產(chǎn)生Java代碼并編譯執(zhí)行這些代碼,有點(diǎn)動(dòng)態(tài)語言的特征。這個(gè)特性對(duì)于某些需要用到動(dòng)態(tài)編譯的應(yīng)用程序相當(dāng)有用,比
如JSP Web Server,當(dāng)我們手動(dòng)修改JSP后,是不希望需要重啟Web
Server才可以看到效果的,這時(shí)候我們就可以用Compiler API來實(shí)現(xiàn)動(dòng)態(tài)編譯JSP文件,當(dāng)然,現(xiàn)在的JSP Web
Server也是支持JSP熱部署的,現(xiàn)在的JSP Web
Server通過在運(yùn)行期間通過Runtime.exec或ProcessBuilder來調(diào)用javac來編譯代碼,這種方式需要我們產(chǎn)生另一個(gè)進(jìn)程去
做編譯工作,不夠優(yōu)雅而且容易使代碼依賴與特定的操作系統(tǒng);Compiler
API通過一套易用的標(biāo)準(zhǔn)的API提供了更加豐富的方式去做動(dòng)態(tài)編譯,而且是跨平臺(tái)的。 下面代碼演示了Compiler API的使用:
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;


public class CompilerAPITest ...{
private final static String srcFileName = "Test.java";
private final static String classFileName = "Test.class";
private final static String className = "Test";

public static void main( String[] args ) ...{
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

if( compiler == null ) ...{
System.err.println( "Compiler is null!" );
return;
}
StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null );
generateJavaClass();

Iterable < ? extends JavaFileObject> sourceFiles = fileManager.getJavaFileObjects( new String[]...{ srcFileName } );
compiler.getTask( null, fileManager, null, null, null, sourceFiles ).call();

try ...{
fileManager.close();
Class.forName( className ).newInstance();

} catch (IOException e) ...{
e.printStackTrace();

} catch (InstantiationException e) ...{
e.printStackTrace();

} catch (IllegalAccessException e) ...{
e.printStackTrace();

} catch (ClassNotFoundException e) ...{
e.printStackTrace();
}
}

public static void generateJavaClass() ...{

try ...{
FileWriter rw = new FileWriter( srcFileName );
BufferedWriter bw = new BufferedWriter( rw );
bw.write( "public class " + className + " {" );
bw.newLine();
bw.write( "public " + className + "() {");
bw.newLine();
bw.write( "System.out.println( 'you are in the constructor of Class Test' );" );
bw.write( "}" );
bw.newLine();
bw.write( "}" );
bw.flush();
bw.close();

} catch (IOException e) ...{
e.printStackTrace();
}
}
}

我在運(yùn)行這個(gè)例子的時(shí)候發(fā)現(xiàn)ToolProvider.getSystemJavaCompiler得到的是NULL,后來上網(wǎng)一查,原來是一個(gè)Bug!鏈接如下:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6477844
Closed, not reproducible
那為什么我一直在reproduce阿?
4.Http
Server API. JDK6提供了一個(gè)簡單的Http Server API,據(jù)此我們可以構(gòu)建自己的嵌入式Http
Server,它支持Http和Https協(xié)議,提供了HTTP1.1的部分實(shí)現(xiàn),沒有被實(shí)現(xiàn)的那部分可以通過擴(kuò)展已有的Http Server
API來實(shí)現(xiàn),程序員必須自己實(shí)現(xiàn)HttpHandler接口,HttpServer會(huì)調(diào)用HttpHandler實(shí)現(xiàn)類的回調(diào)方法來處理客戶端請(qǐng)求,在
這里,我們把一個(gè)Http請(qǐng)求和它的響應(yīng)稱為一個(gè)交換,包裝成HttpExchange類,HttpServer負(fù)責(zé)將HttpExchange傳給
HttpHandler實(shí)現(xiàn)類的回調(diào)方法.下面代碼演示了怎樣創(chuàng)建自己的Http Server .
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;


public class HttpServerAPITest ...{
private static int count = 0;

public static void main( String[] args ) ...{

try ...{
HttpServer hs = HttpServer.create( new InetSocketAddress( 8888 ), 0 );
hs.createContext( "/", new MyHandler() );
hs.createContext( "/java", new MyHandler() );
hs.setExecutor( null );
hs.start();
System.out.println( "---begin---" );
System.out.println( "Listening on " + hs.getAddress() );

} catch( IOException ioe ) ...{
ioe.printStackTrace();
}
}

static class MyHandler implements HttpHandler ...{

public void handle( HttpExchange he ) throws IOException ...{
System.out.println( "Request " + count++ );
System.out.println( he.getHttpContext().getPath() );
InputStream is = he.getRequestBody();
String response = "<font color='blue'>Happy Spring Festerval</font>";
he.sendResponseHeaders( 200, response.length() );
OutputStream os = he.getResponseBody();
os.write( response.getBytes() );
os.close();
}
}
}

效果如圖:

5.對(duì)腳本語言的支持如: ruby, groovy, javascript.
代碼如下:
import java.io.FileReader;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;


public class ScriptTest ...{

public static void main( String[] args ) ...{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName( "ECMAScript" );

try ...{
engine.eval( new FileReader( "C:\test.js" ) );
Invocable invocableEngine = (Invocable)engine;
Object ret = invocableEngine.invokeFunction( "test", null );
System.out.println( "The result is :" + (Double)ret );

} catch( Exception e ) ...{
e.printStackTrace();
}
}
}

test.js如下:

function test()...{
return Math.round( 11.2 );
}
6.
插入式注解處理API(Pluggable Annotation Processing API),插入式注解處理API(JSR
269)提供一套標(biāo)準(zhǔn)API來處理Annotations.JSR 269用Annotation
Processor在編譯期間而不是運(yùn)行期間處理Annotation, Annotation
Processor相當(dāng)于編譯器的一個(gè)插件,所以稱為插入式注解處理.如果Annotation
Processor處理Annotation時(shí)(執(zhí)行process方法)產(chǎn)生了新的Java代碼,編譯器會(huì)再調(diào)用一次Annotation
Processor,如果第二次處理還有新代碼產(chǎn)生,就會(huì)接著調(diào)用Annotation
Processor,直到?jīng)]有新代碼產(chǎn)生為止.每執(zhí)行一次process()方法被稱為一個(gè)"round",這樣整個(gè)Annotation
processing過程可以看作是一個(gè)round的序列.
舉個(gè)例子:們想建立一套基于Annotation的單元測試框架(如TestNG),在測試類里面用Annotation來標(biāo)識(shí)測試期間需要執(zhí)行的測試方法,如下所示:
@TestMethod

public void testCheckName()...{
//do something here
}
這時(shí)我們就可以用JSR 269提供的API來處理測試類,根據(jù)Annotation提取出需要執(zhí)行的測試方法.
再舉個(gè)例子: 下面我用代碼演示如何來用JSR 269提供的API來處理Annotations和讀取Java源文件的元數(shù)據(jù)(metadata)
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;
import javax.tools.Diagnostic.Kind;

@SupportedAnnotationTypes( "ToBeTested" )
@SupportedSourceVersion( SourceVersion.RELEASE_6 )

public class MyAnnotationProcessor extends AbstractProcessor ...{

private void note( String msg ) ...{
processingEnv.getMessager().printMessage( Kind.NOTE, msg );
}

public boolean process( Set< ? extends TypeElement > annotations, RoundEnvironment roundEnv ) ...{

for( TypeElement te : annotations ) ...{
note( "annotation: " + te.toString() );
}
Set< ? extends Element > elements = roundEnv.getRootElements();

for( Element e : elements ) ...{
List< ? extends Element > enclosedElems = e.getEnclosedElements();
List< ? extends ExecutableElement > ees = ElementFilter.methodsIn( enclosedElems );

for( ExecutableElement ee : ees ) ...{
note( "Executable Element Name: " + ee.getSimpleName() );
List< ? extends AnnotationMirror > as = ee.getAnnotationMirrors();
note( " as: " + as );

for( AnnotationMirror am : as )...{
Map< ? extends ExecutableElement, ? extends AnnotationValue > map = am.getElementValues();
Set< ? extends ExecutableElement > ks = map.keySet();

for( ExecutableElement k : ks ) ...{
AnnotationValue av = map.get( k );
note("----"+ee.getSimpleName()+"."+k.getSimpleName()+"="+av.getValue());
}
}
}
}
return false;
}
}



public class Testing ...{
@ToBeTested(group="A")

public void m1()...{
}
@ToBeTested(group="B",owner="QQ")

public void m2()...{
}
}

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention( RetentionPolicy.RUNTIME )
@Target( ElementType.METHOD )

public @interface ToBeTested ...{
String owner() default "Chinajash";
String group();
}

效果如下:

7.StAX.
StAX是The Streaming API for XML的縮寫,是繼DOM(Document Object
Model)和SAX(Simple API for
XML)之后的又一種處理xml的api,一種利用拉模式解析(pull-parsing)XML文檔的API.StAX通過提供一種基于事件迭代器
(Iterator)的API讓程序員去控制xml文檔解析過程,程序遍歷這個(gè)事件迭代器去處理每一個(gè)解析事件,解析事件可以看做是程序拉出來的,也就是
程序促使解析器產(chǎn)生一個(gè)解析事件然后處理該事件,之后又促使解析器產(chǎn)生下一個(gè)解析事件,如此循環(huán)直到碰到文檔結(jié)束符;SAX也是基于事件處理xml文檔,
但卻是用推模式解析,解析器解析完整個(gè)xml文檔后,才產(chǎn)生解析事件,然后推給程序去處理這些事件;DOM采用的方式是將整個(gè)xml文檔映射到一顆內(nèi)存
樹,這樣就可以很容易地得到父節(jié)點(diǎn)和子結(jié)點(diǎn)以及兄弟節(jié)點(diǎn)的數(shù)據(jù),但如果文檔很大,將會(huì)嚴(yán)重影響性能。
下面是個(gè)例子:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;


public class StaxTest ...{

public static void main( String[] arg ) throws XMLStreamException, FileNotFoundException ...{
readXMLByStAX();
writeXMLByStAX();
}

public static void readXMLByStAX() throws XMLStreamException, FileNotFoundException ...{
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader reader = factory.createXMLEventReader( StaxTest.class.getResourceAsStream( "test.xml" ) );
XMLEvent event;
StringBuffer parsingResult = new StringBuffer();

while( reader.hasNext() ) ...{
event = reader.nextEvent();

if( event.isStartElement() ) ...{
StartElement se = event.asStartElement();
parsingResult.append( "<" );
parsingResult.append( se.getName() );

if( se.getName().getLocalPart().equals( "catalog" ) ) ...{
parsingResult.append( "id="" );
parsingResult.append( se.getAttributeByName( new QName( "id" ) ).getValue());
parsingResult.append( """ ) ;
}
parsingResult.append( ">" );

} else if( event.isCharacters() ) ...{
parsingResult.append( event.asCharacters().getData() );

} else if( event.isEndElement() ) ...{
parsingResult.append( "</" );
parsingResult.append( event.asEndElement().getName() );
parsingResult.append( ">" );
}
}
System.out.println( parsingResult );
}

public static void writeXMLByStAX() throws XMLStreamException, FileNotFoundException ...{
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter( new FileOutputStream( "output.xml" ) );
writer.writeStartDocument();
writer.writeCharacters( " " );
writer.writeComment( "testing comment" );
writer.writeCharacters( " " );
writer.writeStartElement( "catalogs" );
writer.writeNamespace( "myNS", "http://blog.csdn.net/Chinajash" );
writer.writeAttribute( "owner", "sina" );
writer.writeCharacters( " " );
writer.writeStartElement("http://blog.csdn.net/Chinajash", "catalog");
writer.writeAttribute("id","007");
writer.writeCharacters("Apparel");
// 寫入catalog元素的結(jié)束標(biāo)簽
writer.writeEndElement();
// 寫入catalogs元素的結(jié)束標(biāo)簽
writer.writeEndElement();
// 結(jié)束 XML 文檔
writer.writeEndDocument();
writer.close();

}
}

test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<catalogs>
<catalog id="001">Book</catalog>
<catalog id="002">Video</catalog>
</catalogs>
8.
Web Service. 由于Web服務(wù)日趨流行,利用Web服務(wù)的功能性的API特征正從最新的Java EE版本中向Java SE
6平臺(tái)遷移。換言之,針對(duì)Web服務(wù)不需另外加入額外的工具,在Java EE和Java
SE平臺(tái)擁有相同的API。野馬將大把不同的Web服務(wù)相關(guān)的API加到標(biāo)準(zhǔn)的工具柜中:以JSR 181針對(duì)Java
平臺(tái)的Web服務(wù)元數(shù)據(jù),通過JSR 224的基于XML 的Web服務(wù)Java API(JAX-WS);針對(duì)Java的帶有附件的SOAP
API(SAAJ)作為JSR 67 。與三個(gè)Web服務(wù)API相關(guān)的包新增到Java SE 6.0里:JAX-WS API
放置到j(luò)avax.xml.ws包; SAAJ類在javax.xml.soap 包; Web服務(wù)的元數(shù)據(jù)類放置在javax.jws包里。
下面是一個(gè)簡單的例子, 下面的代碼是要作為web service發(fā)布的類。
package hello;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService

public class CircleFunctions ...{

public double getArea( int radius ) ...{
return Math.PI * radius * radius;
}

public double getCircumference( int radius ) ...{
return Math.PI * radius * 2;
}

public static void main( String[] args ) ...{
Endpoint.publish( "http://localhost:8888/WebServiceExample/circlefunctions", new CircleFunctions());
}
}

處理的方法如下:
javac -d ./ CircleFunctions.java
wsgen hello.CircleFunctions
java hello.CircleFunctions
然后在瀏覽器中輸入如下url,你將得到一個(gè)xml頁面:
http: //localhost:8888/WebServiceExample/circlefunctions?WSDL
參考網(wǎng)頁:
1.Desktop和SystemTray. http://dev.yesky.com/411/3019911.shtml
2.Console. http://dev.yesky.com/133/3032133.shtml
3.Compiler API. http://developer.51cto.com/art/200701/37359.htm
4.HttpServer API. http://www.testage.net/QA/Dev/200701/1396.htm
5. 對(duì)腳本語言的支持http://blog.edwardro.com/read.php?167
6. 插入式注解處理API. http://ourconan.com.cn/article.php?itemid-2113-type-blog.html
7.StAX. http://ourconan.com.cn/article.php?itemid-2111-type-blog.html
8.Web Service. http://www.360doc.com/showWeb/0/0/298124.aspx
9.JDK1.5的Annotation
http://lzqdiy.bokee.com/viewdiary.14724866.html