1.使ç¨javaå¼å代ç ç¼è¾å¨(使ç¨javaå¼å代ç ç¼è¾å¨ç好å¤)
2.å¦ä½ç¨mavenå°java8åç代ç ç¼è¯ä¸ºjava6å¹³å°ç
使ç¨javaå¼å代ç ç¼è¾å¨(使ç¨javaå¼å代ç ç¼è¾å¨ç好å¤)
javaç¼ç¨å¸¸ç¨ç软件æåªäº
æ³è¦å好Javaå¼åï¼æ²¡æ常ç¨çå¼åå·¥å ·å¯ä¸è¡ãèJavaåå¦è å¾å¾å¤äºå¯¹Javaè¿ä¸ç¥å解çç¶æï¼å¯¹äºJavaå¼åå·¥å ·çäºè§£ä¹ä¸ä¼å¤ªå¤ï¼å¸¸å¸¸ä¼ä¸ºäºæ¾å¼åå·¥å ·è浪费大éçæ¶é´ãç»å¤§å®¶ä»ç»ä¸Javaå¼åå·¥ç¨å¸å¸¸ç¨çå¼åå·¥å ·æåªäºã
第ä¸ä¸ªJava常ç¨çå¼åå·¥å ·ä¸ºJDKï¼ä¹å«Java软件å¼åå·¥å ·å ï¼æ¯ä¸ä¸ªç¼åJavaçAppletå°ç¨åºååºç¨ç¨åºçç¨åºå¼åç¯å¢ï¼JDKä¸è¿å æ¬äºJavaçå¼åç¯å¢ï¼æ以ä¸è½½äºJDKå°±å å«äºJavaå¼åç¯å¢ãèä¸JDKä¹æ以被å«åJava软件å¼åå·¥å ·å ï¼æ¯å 为å®å å«äºå ¶å®Javaå¼åå·¥å ·ï¼å¦ï¼ç¼è¯å·¥å ·(javac.exe)ãæå å·¥å ·(jar.exe)çã
第äºä¸ªæ¯JavaIDEï¼Eclipseï¼Eclipseæ¯ä¸ä¸ªå¼æ¾æºä»£ç çãåºäºJavaçå¯æ©å±å¼åå¹³å°ãå°±å ¶æ¬èº«èè¨ï¼å®åªæ¯ä¸ä¸ªæ¡æ¶åä¸ç»æå¡ï¼ç¨äºéè¿æ件ç»ä»¶æ建å¼åç¯å¢ãèä¸ç±äºå®çå¹³çåä¸è´æ§ï¼ä½¿å¾å ¶å®å¼åè¯è¨ä¹å¯ä»¥å¨eclipseä¸ä½¿ç¨ã
第ä¸ä¸ªæ¯notepad++ï¼notepad++æ¯ä¸ä¸ªç¼è¯å·¥å ·ï¼å®çä¼ç¹æ¯å³ä½¿ä½ ææ件å æäºï¼å¦æånotepad++ä¸æå¼è¿ï¼é£ä¹é£éå°±ä¼ä¿çï¼æ以对ä¸äºå®¹æ误å ç人æ¥è¯´æ¯è¾å好ï¼notepad++å¯ä»¥ç¨æ¥æ¿ä»£notepadã
第å个æ¯VisualStudioCodeï¼å®æ¯ä¸ä¸ªè¿è¡äºMacOSXãWindowsåLinuxä¹ä¸ç跨平å°æºä»£ç ç¼è¾å¨ãèä¸è¿ä¸ªç¼è¾å¨æ¯æå¤ç§è¯è¨åæ ¼å¼çç¼åã
以ä¸å°±æ¯æ¯è¾å¸¸ç¨ç4ç§Javaå¼åå·¥å ·ï¼Javaçåå¦è å¨å¯¹å¼åå·¥å ·ä¸äºè§£çæ åµä¸ï¼å¯ä»¥æ ¹æ®ä¸è¿°ä»ç»æ¥éæ©éåèªå·±çå¼åå·¥å ·ï¼è¿æ ·å¯ä»¥åå°èªå·±å°å¤æ¾å¼åå·¥å ·è¿å¯è½æ¾å°ä¸éåçæ浪费çæ¶é´ã
å¦ä½ç¨javaç¼åä¸ä¸ªç®åçææ¬ç¼è¾å¨ï¼
importjava.awt.*;\x0d\importjava.awt.event.*;\x0d\importjavax.swing.*;\x0d\importjava.io.*;\x0d\\x0d\publicclassf1extendsFrameimplementsActionListener\x0d\{ \x0d\privateMenuBarmenubar=newMenuBar();\x0d\privateMenufilemenu=newMenu("æ件");\x0d\privateMenueditmenu=newMenu("ç¼è¾");\x0d\privateMenuformmenu=newMenu("æ ¼å¼");\x0d\privateMenuItem[]itemf=newMenuItem[4];\x0d\privateMenuItem[]iteme=newMenuItem[6];\x0d\privateMenuItem[]items=newMenuItem[2];\x0d\privateTextAreatf=newTextArea();\x0d\\x0d\publicinta=0,抖军团源码b=0,c=0,style=Font.PLAIN,size=;\x0d\publicStrings1="red:"+a+""+"green:"+b+""+"blue"+c,\x0d\s2="å®ä½";\x0d\\x0d\publicString[]sz1={ "","","","","",""},\x0d\sz2={ "å®ä½","é»ä½","å¹¼å","é¶ä¹¦","è¡æ¥·","Arial","Georgia"},\x0d\sz3={ "ç²ä½","å¾æ","常è§","ç²æ"};\x0d\\x0d\JDialogdialog=newJDialog(this,"åä½",true);\x0d\Containercp=dialog.getContentPane();\x0d\JLabel[]lb=newJLabel[8];\x0d\JLabellb1=newJLabel(s1,JLabel.LEFT);\x0d\JButtonb1=newJButton("ç¡®å®"),\x0d\b2=newJButton("åæ¶");\x0d\JComboBoxjc1=newJComboBox(),\x0d\jc2=newJComboBox(),\x0d\jc3=newJComboBox();\x0d\JScrollBarjb1=newJScrollBar(JScrollBar.HORIZONTAL,,5,0,);\x0d\JScrollBarjb2=newJScrollBar(JScrollBar.HORIZONTAL,,5,0,);\x0d\JScrollBarjb3=newJScrollBar(JScrollBar.HORIZONTAL,,5,0,);
javaæºä»£ç ç¼è¾å¨è®¾è®¡ç¨äºç¼åJavaæºä»£ç çç¼è¾å¨ï¼åºæ¬è¦æ±ï¼å¯ä»¥å®ææºç¨åºçæ件æå¼ï¼ç¼è¾åæ件ä¿åä¸.é«äº®çå 容:
éè¦é«äº®çå 容æ:
1.å ³é®å,å¦public,int,trueç.
2.è¿ç®ç¬¦,å¦+,-,*,/ç
3.æ°å
4.é«äº®å符串,å¦"exampleofstring"
5.é«äº®åè¡æ³¨é
6.é«äº®å¤è¡æ³¨é
äº.å®ç°é«äº®çæ ¸å¿æ¹æ³:
StyledDocument.setCharacterAttributes(intoffset,intlength,AttributeSets,booleanreplace)
ä¸.ææ¬ç¼è¾å¨éæ©.
Javaä¸æä¾çå¤è¡ææ¬ç¼è¾å¨æ:JTextComponent,JTextArea,JTextPane,JEditorPaneç,é½å¯ä»¥ä½¿ç¨.ä½æ¯å 为è¯æ³çè²ä¸ææ¬è¦ä½¿ç¨å¤ç§é£æ ¼çæ ·å¼,æ以è¿äºææ¬ç¼è¾å¨çdocumentè¦ä½¿ç¨StyledDocument.
JTextArea使ç¨çæ¯PlainDocument,æ¤documentä¸è½è¿è¡å¤ç§æ ¼å¼ççè².
JTextPane,JEditorPane使ç¨çæ¯StyledDocument,é»è®¤å°±å¯ä»¥ä½¿ç¨.
为äºå®ç°è¯æ³çè²,å¯ä»¥ç»§æ¿èªDefaultStyledDocument,è®¾ç½®å ¶ä¸ºè¿äºææ¬ç¼è¾å¨çdocumet,æè ä¹å¯ä»¥ç´æ¥ä½¿ç¨JTextPane,JEditorPaneæ¥å.为äºæ¹ä¾¿,è¿éå°±ç´æ¥ä½¿ç¨JTextPaneäº.
å.ä½æ¶è¿è¡çè².
å½ææ¬ç¼è¾å¨ä¸æå符被æå ¥æè å é¤æ¶,ææ¬çå 容就åçäºåå,è¿æ¶æ£æ¥,è¿è¡çè².
为äºçè§å°ææ¬çå 容åçäºåå,è¦ç»documentæ·»å ä¸ä¸ªDocumentListenerçå¬å¨,å¨ä»çremoveUpdateåinsertUpdateä¸è¿è¡çè²å¤ç.
èchangedUpdateæ¹æ³å¨ææ¬çå±æ§ä¾å¦åæ¯è²,èæ¯è²,åä½çé£æ ¼æ¹åæ¶æä¼è¢«è°ç¨.
@Override
publicvoidchangedUpdate(DocumentEvente){
}
@Override
publicvoidinsertUpdate(DocumentEvente){
try{
colouring((StyledDocument)e.getDocument(),e.getOffset(),e.getLength());
}catch(BadLocationExceptione1){
e1.printStackTrace();
}
}
@Override
publicvoidremoveUpdate(DocumentEvente){
try{
//å 为å é¤åå æ ç´§æ¥çå½±åçåè¯ä¸¤è¾¹,æ以é¿åº¦å°±ä¸éè¦äº
colouring((StyledDocument)e.getDocument(),e.getOffset(),0);
}catch(BadLocationExceptione1){
e1.printStackTrace();
}
}
äº.çè²èå´:
pos:æåååå æ çä½ç½®.
len:æååçå符æ°.
ä¾å¦æå ³é®åpublic,int
åè¯"publicint",å¨"public"å"int"ä¸æå ¥ä¸ä¸ªç©ºæ ¼ååæ"publicint",ä¸ä¸ªåè¯åæäºä¸¤ä¸ª,è¿æ¶å¯¹"public"å"int"è¿è¡çè².
çè²èå´æ¯publicä¸pçä½ç½®åintä¸tçä½ç½®å 1,å³æ¯posåé¢åè¯å¼å§çä¸æ åpos+lenå¼å§åè¯ç»æçä¸æ .æ以ä¸ä¾ä¸è¦çè²çèå´æ¯"publicint".
æä¾äºæ¹æ³indexOfWordStartæ¥åå¾posååè¯å¼å§çä¸æ ,æ¹æ³indexOfWordEndæ¥åå¾posååè¯ç»æçä¸æ .
publicintindexOfWordStart(Documentdoc,intpos)throwsBadLocationException{
//ä»poså¼å§ååæ¾å°ç¬¬ä¸ä¸ªéåè¯å符.
for(;pos0isWordCharacter(doc,pos-1);--pos);
returnpos;
}
publicintindexOfWordEnd(Documentdoc,intpos)throwsBadLocationException{
//ä»poså¼å§ååæ¾å°ç¬¬ä¸ä¸ªéåè¯å符.
for(;isWordCharacter(doc,pos);++pos);
returnpos;
}
ä¸ä¸ªå符æ¯åè¯çææå符:æ¯åæ¯,æ°å,ä¸å线.
publicbooleanisWordCharacter(Documentdoc,intpos)throwsBadLocationException{
charch=getCharAt(doc,pos);//åå¾å¨ææ¡£ä¸posä½ç½®å¤çå符
if(Character.isLetter(ch)||Character.isDigit(ch)||ch=='_'){ returntrue;}
returnfalse;
}
æ以çè²çèå´æ¯[start,end]:
intstart=indexOfWordStart(doc,pos);
intend=indexOfWordEnd(doc,pos+len);
å .å ³é®åçè².
ä»çè²èå´çå¼å§ä¸æ èµ·è¿è¡å¤æ,å¦ææ¯ä»¥åæ¯å¼æè ä¸å线å¼å¤´,å说ææ¯åè¯,é£ä¹å åå¾è¿ä¸ªåè¯,å¦æè¿ä¸ªåè¯æ¯å ³é®å,å°±è¿è¡å ³é®åçè²,å¦æä¸æ¯,å°±è¿è¡æ®éççè².çè²å®è¿ä¸ªåè¯å,继ç»åé¢ççè²å¤ç.å·²ç»çè²è¿çå符,å°±ä¸åè¿è¡çè²äº.
publicvoidcolouring(StyledDocumentdoc,intpos,intlen)throwsBadLocationException{
//åå¾æå ¥æè å é¤åå½±åå°çåè¯.
//ä¾å¦"public"å¨båæå ¥ä¸ä¸ªç©ºæ ¼,å°±åæäº:"public",è¿æ¶å°±æ两个åè¯è¦å¤ç:"pub"å"lic"
//è¿æ¶è¦åå¾çèå´æ¯pubä¸påé¢çä½ç½®ålicä¸cåé¢çä½ç½®
intstart=indexOfWordStart(doc,pos);
intend=indexOfWordEnd(doc,pos+len);
charch;
while(startend){
ch=getCharAt(doc,start);
if(Character.isLetter(ch)||ch=='_'){
//å¦ææ¯ä»¥åæ¯æè ä¸å线å¼å¤´,说ææ¯åè¯
//pos为å¤çåçæåä¸ä¸ªä¸æ
start=colouringWord(doc,start);
}else{
//SwingUtilities.invokeLater(newColouringTask(doc,pos,wordEnd-pos,normalStyle));
++start;
}
}
}
publicintcolouringWord(StyledDocumentdoc,intpos)throwsBadLocationException{
intwordEnd=indexOfWordEnd(doc,pos);
Stringword=doc.getText(pos,wordEnd-pos);//è¦è¿è¡çè²çåè¯
if(keywords.contains(word)){
//å¦ææ¯å ³é®å,å°±è¿è¡å ³é®åççè²,å¦å使ç¨æ®éççè².
//è¿éæä¸ç¹è¦æ³¨æ,å¨insertUpdateåremoveUpdateçæ¹æ³è°ç¨çè¿ç¨ä¸,ä¸è½ä¿®æ¹docçå±æ§.
//ä½æ们åè¦è¾¾å°è½å¤ä¿®æ¹docçå±æ§,æ以ææ¤ä»»å¡æ¾å°è¿ä¸ªæ¹æ³çå¤é¢å»æ§è¡.
//å®ç°è¿ä¸ç®ç,å¯ä»¥ä½¿ç¨æ°çº¿ç¨,ä½æ¾å°swingçäºä»¶éåéå»å¤çæ´è½»ä¾¿ä¸ç¹.
SwingUtilities.invokeLater(newColouringTask(doc,pos,wordEnd-pos,keywordStyle));
}else{
SwingUtilities.invokeLater(newColouringTask(doc,pos,wordEnd-pos,normalStyle));
}
returnwordEnd;
}
å 为å¨insertUpdateåremoveUpdateæ¹æ³ä¸ä¸è½ä¿®æ¹documentçå±æ§,æ以çè²çä»»å¡æ¾å°è¿ä¸¤ä¸ªæ¹æ³å¤é¢,æ以使ç¨äºSwingUtilities.invokeLateræ¥å®ç°.
privateclassColouringTaskimplementsRunnable{
privateStyledDocumentdoc;
privateStylestyle;
privateintpos;
privateintlen;
publicColouringTask(StyledDocumentdoc,intpos,intlen,Stylestyle){
this.doc=doc;
this.pos=pos;
this.len=len;
this.style=style;
}
publicvoidrun(){
try{
//è¿éå°±æ¯å¯¹å符è¿è¡çè²
doc.setCharacterAttributes(pos,len,style,true);
}catch(Exceptione){ }
}
}
ä¸:æºç
å ³é®åçè²çå®æ代ç å¦ä¸,å¯ä»¥ç´æ¥ç¼è¯è¿è¡.对äºæ°å,è¿ç®ç¬¦,å符串çççè²å¤çå¨ä»¥åçæç¨ä¸ä¼ç»§ç»è¿è¡è¯¦è§£.
importjava.awt.Color;
importjava.util.HashSet;
importjava.util.Set;
importjavax.swing.JFrame;
importjavax.swing.JTextPane;
importjavax.swing.SwingUtilities;
importjavax.swing.event.DocumentEvent;
importjavax.swing.event.DocumentListener;
importjavax.swing.text.BadLocationException;
importjavax.swing.text.Document;
importjavax.swing.text.Style;
importjavax.swing.text.StyleConstants;
importjavax.swing.text.StyledDocument;
publicclassHighlightKeywordsDemo{
publicstaticvoidmain(String[]args){
JFrameframe=newJFrame();
JTextPaneeditor=newJTextPane();
editor.getDocument().addDocumentListener(newSyntaxHighlighter(editor));
frame.getContentPane().add(editor);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(,);
frame.setVisible(true);
}
}
/
***å½ææ¬è¾å ¥åºçæå符æå ¥æè å é¤æ¶,è¿è¡é«äº®.
*
*è¦è¿è¡è¯æ³é«äº®,ææ¬è¾å ¥ç»ä»¶çdocumentè¦æ¯styleddocumentæè¡.æ以ä¸è¦ç¨JTextArea.å¯ä»¥ä½¿ç¨JTextPane.
*
*@authorBiao
*
*/
classSyntaxHighlighterimplementsDocumentListener{
privateSetStringkeywords;
privateStylekeywordStyle;
privateStylenormalStyle;
publicSyntaxHighlighter(JTextPaneeditor){
//åå¤çè²ä½¿ç¨çæ ·å¼
keywordStyle=((StyledDocument)editor.getDocument()).addStyle("Keyword_Style",null);
normalStyle=((StyledDocument)editor.getDocument()).addStyle("Keyword_Style",null);
StyleConstants.setForeground(keywordStyle,Color.RED);
StyleConstants.setForeground(normalStyle,Color.BLACK);
//åå¤å ³é®å
keywords=newHashSetString();
keywords.add("public");
keywords.add("protected");
keywords.add("private");
keywords.add("_int9");
keywords.add("float");
keywords.add("double");
}
publicvoidcolouring(StyledDocumentdoc,intpos,intlen)throwsBadLocationException{
//åå¾æå ¥æè å é¤åå½±åå°çåè¯.
//ä¾å¦"public"å¨båæå ¥ä¸ä¸ªç©ºæ ¼,å°±åæäº:"public",è¿æ¶å°±æ两个åè¯è¦å¤ç:"pub"å"lic"
//è¿æ¶è¦åå¾çèå´æ¯pubä¸påé¢çä½ç½®ålicä¸cåé¢çä½ç½®
intstart=indexOfWordStart(doc,pos);
intend=indexOfWordEnd(doc,pos+len);
charch;
while(startend){
ch=getCharAt(doc,start);
if(Character.isLetter(ch)||ch=='_'){
//å¦ææ¯ä»¥åæ¯æè ä¸å线å¼å¤´,说ææ¯åè¯
//pos为å¤çåçæåä¸ä¸ªä¸æ
start=colouringWord(doc,start);
}else{
SwingUtilities.invokeLater(newColouringTask(doc,start,1,normalStyle));
++start;
}
}
}
/
***对åè¯è¿è¡çè²,并è¿ååè¯ç»æçä¸æ .
*
*@paramdoc
*@parampos
*@return
*@throwsBadLocationException
*/
publicintcolouringWord(StyledDocumentdoc,intpos)throwsBadLocationException{
intwordEnd=indexOfWordEnd(doc,pos);
Stringword=doc.getText(pos,wordEnd-pos);
if(keywords.contains(word)){
//å¦ææ¯å ³é®å,å°±è¿è¡å ³é®åççè²,å¦å使ç¨æ®éççè².
//è¿éæä¸ç¹è¦æ³¨æ,å¨insertUpdateåremoveUpdateçæ¹æ³è°ç¨çè¿ç¨ä¸,ä¸è½ä¿®æ¹docçå±æ§.
//ä½æ们åè¦è¾¾å°è½å¤ä¿®æ¹docçå±æ§,æ以ææ¤ä»»å¡æ¾å°è¿ä¸ªæ¹æ³çå¤é¢å»æ§è¡.
//å®ç°è¿ä¸ç®ç,å¯ä»¥ä½¿ç¨æ°çº¿ç¨,ä½æ¾å°swingçäºä»¶éåéå»å¤çæ´è½»ä¾¿ä¸ç¹.
SwingUtilities.invokeLater(newColouringTask(doc,pos,wordEnd-pos,keywordStyle));
}else{
SwingUtilities.invokeLater(newColouringTask(doc,pos,wordEnd-pos,normalStyle));
}
returnwordEnd;
}
/
**å¦ä½ç¨mavenå°java8åç代ç ç¼è¯ä¸ºjava6å¹³å°ç
ããå¨ä¸è¬çJavaåºç¨å¼åè¿ç¨ä¸ï¼å¼å人å使ç¨Javaçæ¹å¼æ¯è¾ç®åãæå¼æ¯ç¨çIDEï¼ç¼åJavaæºä»£ç ï¼åå©ç¨IDEæä¾çåè½ç´æ¥è¿è¡Java ç¨åºå°±å¯ä»¥äºãè¿ç§å¼å模å¼èåçè¿ç¨æ¯ï¼å¼å人åç¼åçæ¯Javaæºä»£ç æ件ï¼.javaï¼ï¼IDEä¼è´è´£è°ç¨Javaçç¼è¯å¨æJavaæºä»£ç ç¼è¯æå¹³å°æ å ³çåè代ç ï¼byte codeï¼ï¼ä»¥ç±»æ件çå½¢å¼ä¿åå¨ç£çä¸ï¼.classï¼ãJavaèææºï¼JVMï¼ä¼è´è´£æJavaåè代ç å 载并æ§è¡ãJavaéè¿è¿ç§æ¹å¼æ¥å®ç°å ¶âç¼åä¸æ¬¡ï¼å°å¤è¿è¡ï¼Write once, run anywhereï¼â çç®æ ãJavaç±»æ件ä¸å å«çåè代ç å¯ä»¥è¢«ä¸åå¹³å°ä¸çJVMæ使ç¨ãJavaåè代ç ä¸ä» å¯ä»¥ä»¥æ件形å¼åå¨äºç£çä¸ï¼ä¹å¯ä»¥éè¿ç½ç»æ¹å¼æ¥ä¸è½½ï¼è¿å¯ä»¥åªåå¨äºå åä¸ãJVMä¸çç±»å è½½å¨ä¼è´è´£ä»å å«åè代ç çåèæ°ç»ï¼byte[]ï¼ä¸å®ä¹åºJavaç±»ãå¨æäºæ åµä¸ï¼å¯è½ä¼éè¦å¨æççæ Javaåè代ç ï¼ææ¯å¯¹å·²æçJavaåè代ç è¿è¡ä¿®æ¹ãè¿ä¸ªæ¶åå°±éè¦ç¨å°æ¬æä¸å°è¦ä»ç»çç¸å ³ææ¯ãé¦å ä»ç»ä¸ä¸å¦ä½å¨æç¼è¯Javaæºæ件ã
ããå¨æç¼è¯Javaæºæ件
ããå¨ä¸è¬æ åµä¸ï¼å¼å人åé½æ¯å¨ç¨åºè¿è¡ä¹åå°±ç¼åå®æäºå ¨é¨çJavaæºä»£ç 并ä¸æåç¼è¯ã对æäºåºç¨æ¥è¯´ï¼Javaæºä»£ç çå 容å¨è¿è¡æ¶å»æè½ç¡®å®ãè¿ä¸ªæ¶åå°±éè¦å¨æç¼è¯æºä»£ç æ¥çæJavaåè代ç ï¼åç±JVMæ¥å è½½æ§è¡ãå ¸åçåºæ¯æ¯å¾å¤ç®æ³ç«èµçå¨çº¿è¯æµç³»ç»ï¼å¦PKU JudgeOnlineï¼ï¼å 许ç¨æ·ä¸ä¼ Java代ç ï¼ç±ç³»ç»å¨åå°ç¼è¯ãè¿è¡å¹¶è¿è¡å¤å®ãå¨å¨æç¼è¯Javaæºæ件æ¶ï¼ä½¿ç¨çåæ³æ¯ç´æ¥å¨ç¨åºä¸è°ç¨Javaç¼è¯å¨ã
ããJSR å¼å ¥äºJavaç¼è¯å¨APIãå¦æ使ç¨JDK 6çè¯ï¼å¯ä»¥éè¿æ¤APIæ¥å¨æç¼è¯Java代ç ãæ¯å¦ä¸é¢ç代ç ç¨æ¥å¨æç¼è¯æç®åçHello Worldç±»ã该Javaç±»ç代ç æ¯ä¿åå¨ä¸ä¸ªå符串ä¸çã
ãã public class CompilerTest {
ãã public static void main(String[] args) throws Exception {
ãã String source = "public class Main { public static void main(String[] args) { System.out.println(\"Hello World!\");} }";
ãã JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
ãã StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
ãã StringSourceJavaObject sourceObject = newCompilerTest.StringSourceJavaObject("Main", source);
ãã Iterable< extends JavaFileObject> fileObjects = Arrays.asList(sourceObject);
ãã CompilationTask task = compiler.getTask(null, fileManager, null,null, null, fileObjects);
ãã boolean result = task.call();
ãã if (result) {
ãã System.out.println("ç¼è¯æåã");
ãã }
ãã }
ãã
ãã static class StringSourceJavaObject extends SimpleJavaFileObject {
ãã
ãã private String content = null;
ãã public StringSourceJavaObject(String name, String content) ?throwsURISyntaxException {
ãã super(URI.create("string:///" + name.replace('.','/') + Kind.SOURCE.extension), Kind.SOURCE);
ãã this.content = content;
ãã }
ãã
ãã public CharSequence getCharContent(boolean ignoreEncodingErrors) ?throws IOException {
ãã return content;
ãã }
ãã }
ãã }
ããå¦æä¸è½ä½¿ç¨JDK 6æä¾çJavaç¼è¯å¨APIçè¯ï¼å¯ä»¥ä½¿ç¨JDKä¸çå·¥å ·ç±»com.sun.tools.javac.Mainï¼ä¸è¿è¯¥å·¥å ·ç±»åªè½ç¼è¯åæ¾å¨ç£çä¸çæ件ï¼ç±»ä¼¼äºç´æ¥ä½¿ç¨javacå½ä»¤ã
ããå¦å¤ä¸ä¸ªå¯ç¨çå·¥å ·æ¯Eclipse JDT Coreæä¾çç¼è¯å¨ãè¿æ¯Eclipse Javaå¼åç¯å¢ä½¿ç¨çå¢éå¼Javaç¼è¯å¨ï¼æ¯æè¿è¡åè°è¯æé误ç代ç ã该ç¼è¯å¨ä¹å¯ä»¥åç¬ä½¿ç¨ãPlayæ¡æ¶å¨å é¨ä½¿ç¨äºJDTçç¼è¯å¨æ¥å¨æç¼è¯Javaæºä»£ç ãå¨å¼å模å¼ä¸ï¼Playæ¡æ¶ä¼å®ææ«æ项ç®ä¸çJavaæºä»£ç æ件ï¼ä¸æ¦åç°æä¿®æ¹ï¼ä¼èªå¨ç¼è¯ Javaæºä»£ç ãå æ¤å¨ä¿®æ¹ä»£ç ä¹åï¼å·æ°é¡µé¢å°±å¯ä»¥çå°ååã使ç¨è¿äºå¨æç¼è¯çæ¹å¼çæ¶åï¼éè¦ç¡®ä¿JDKä¸çtools.jarå¨åºç¨ç CLASSPATHä¸ã
ããä¸é¢ä»ç»ä¸ä¸ªä¾åï¼æ¯å ³äºå¦ä½å¨Javaéé¢åååè¿ç®ï¼æ¯å¦æ±åºæ¥(3+4)*7-çå¼ãä¸è¬çåæ³æ¯åæè¾å ¥çè¿ç®è¡¨è¾¾å¼ï¼èªå·±æ¥æ¨¡æ计ç®è¿ç¨ãèèå°æ¬å·çåå¨åè¿ç®ç¬¦çä¼å 级çé®é¢ï¼è¿æ ·ç计ç®è¿ç¨ä¼æ¯è¾å¤æï¼èä¸å®¹æåºéãå¦å¤ä¸ç§åæ³æ¯å¯ä»¥ç¨JSR å¼å ¥çèæ¬è¯è¨æ¯æï¼ç´æ¥æè¾å ¥ç表达å¼å½åJavaScriptææ¯JavaFXèæ¬æ¥æ§è¡ï¼å¾å°ç»æãä¸é¢ç代ç 使ç¨çåæ³æ¯å¨æçæJavaæºä»£ç 并ç¼è¯ï¼æ¥çå è½½Javaç±»æ¥æ§è¡å¹¶è·åç»æãè¿ç§åæ³å®å ¨ä½¿ç¨Javaæ¥å®ç°ã
ãã private static double calculate(String expr) throws CalculationException {
ãã String className = "CalculatorMain";
ãã String methodName = "calculate";
ãã String source = "public class " + className
ãã + " { public static double " + methodName + "() { return " + expr +"; } }";
ãã //çç¥å¨æç¼è¯Javaæºä»£ç çç¸å ³ä»£ç ï¼åè§ä¸ä¸è
ãã boolean result = task.call();
ãã if (result) {
ãã ClassLoader loader = Calculator.class.getClassLoader();
ãã try {
ãã Class<?> clazz = loader.loadClass(className);
ãã Method method = clazz.getMethod(methodName, new Class<?>[] { });
ãã Object value = method.invoke(null, new Object[] { });
ãã return (Double) value;
ãã } catch (Exception e) {
ãã throw new CalculationException("å é¨é误ã");
ãã }
ãã } else {
ãã throw new CalculationException("é误ç表达å¼ã");
ãã }
ãã }
ããä¸é¢ç代ç ç»åºäºä½¿ç¨å¨æçæçJavaåè代ç çåºæ¬æ¨¡å¼ï¼å³éè¿ç±»å è½½å¨æ¥å è½½åè代ç ï¼å建Javaç±»ç对象çå®ä¾ï¼åéè¿Javaåå°APIæ¥è°ç¨å¯¹è±¡ä¸çæ¹æ³ã
ããJavaåè代ç å¢å¼º
ããJava åè代ç å¢å¼ºæçæ¯å¨Javaåè代ç çæä¹åï¼å¯¹å ¶è¿è¡ä¿®æ¹ï¼å¢å¼ºå ¶åè½ãè¿ç§åæ³ç¸å½äºå¯¹åºç¨ç¨åºçäºè¿å¶æ件è¿è¡ä¿®æ¹ãå¨å¾å¤Javaæ¡æ¶ä¸é½å¯ä»¥è§å°è¿ç§å®ç°æ¹å¼ãJavaåè代ç å¢å¼ºé常ä¸Javaæºæ件ä¸ç注解ï¼annotationï¼ä¸å使ç¨ã注解å¨Javaæºä»£ç ä¸å£°æäºéè¦å¢å¼ºçè¡ä¸ºåç¸å ³çå æ°æ®ï¼ç±æ¡æ¶å¨è¿è¡æ¶å»å®æ对åè代ç çå¢å¼ºãJavaåè代ç å¢å¼ºåºç¨çåºæ¯æ¯è¾å¤ï¼ä¸è¬é½éä¸å¨åå°åä½ä»£ç å对å¼å人åå±è½åºå±çå®ç°ç»èä¸ãç¨è¿JavaBeansç人å¯è½å¯¹å ¶ä¸é£äºå¿ 须添å çgetter/setteræ¹æ³æå°å¾ç¹çï¼å¹¶ä¸é¾ä»¥ç»´æ¤ãèéè¿åè代ç å¢å¼ºï¼å¼å人ååªéè¦å£°æBeanä¸çå±æ§å³å¯ï¼getter/setteræ¹æ³å¯ä»¥éè¿ä¿®æ¹åè代ç æ¥èªå¨æ·»å ãç¨è¿JPAç人ï¼å¨è°è¯ç¨åºçæ¶åï¼ä¼åç°å®ä½ç±»ä¸è¢«æ·»å äºä¸äºé¢å¤ç ååæ¹æ³ãè¿äºååæ¹æ³æ¯å¨è¿è¡æ¶å»ç±JPAçå®ç°å¨ææ·»å çãåè代ç å¢å¼ºå¨é¢åæ¹é¢ç¼ç¨ï¼AOPï¼çä¸äºå®ç°ä¸ä¹æ使ç¨ã