2016-05-15 74 views
-2

我想在命令行上運行java註釋程序。我無法運行該程序。 這裏是我編使用命令javac的 COM /拉拉/註釋/ G.java 它編譯成功,但我在運行程序面臨的問題該程序的程序如何在命令行上運行java註釋程序?

package com.lara.annontations; 
@interface F 
{ 
    public String somemessage(); 
} 
class G 
{ 
    @F(somemessage="My First Method") 
    public static void main(String[] args) 
    { 
     System.out.println("Hello World!"); 
    } 
} 

。我用命令運行程序爲 java com.lara.annotation G 但它不起作用。 請任何人都可以幫助我運行這個程序。 謝謝。

+2

元數據你怎麼知道它不工作? – shmosel

+0

定義「不工作」。你期望什麼?究竟發生了什麼?如果出現錯誤,請顯示錯誤消息。 – Andreas

回答

0

首先你應該定義註釋的保留到運行時... 而你需要使用反射來打印剛註釋的方法

G gClass = new G(); 
Method m = gClass.getClass().getMethod("main"); 
Annotation[] annos = m.getAnnotations(); 
System.out.println("All annotations for myMeth:"); 
for(Annotation a : annos) 
    System.out.println(a); 
+0

如果您只想訪問_static_方法,爲什麼要創建'G'實例? – Seelenvirtuose