Java 編譯器 command() 方法



描述

Java 編譯器 command() 方法檢查引數型別及其欄位,並執行一些已記錄的操作。

注意 - 此 API 自 Java 9 起已棄用,並且在 Java 21 及更高版本中不可用。

宣告

以下是 java.lang.Compiler.command() 方法的宣告

public static boolean command(Object arg)

引數

Object arg - 一個編譯器特定的引數

返回值

此方法返回一個編譯器特定的值,包括 null。

異常

NullPointerException - 如果編譯器不喜歡 null 引數。

為編譯器準備命令示例

以下示例顯示了 java.lang.Compiler.command() 方法的使用。在此程式中,我們建立了兩個類 CompilerDemo 和 SubClass1。在 main 方法中,建立了 CompilerDemo 和 SubClass1 的例項。使用 getClass() 方法,我們檢索了它們的類。現在使用 command() 方法,我們編譯了 CompilerDemo 類,並列印了結果。

package com.tutorialspoint;

public class CompilerDemo {

   public static void main(String[] args) {

      CompilerDemo cls = new CompilerDemo();
      CompilerDemo subcls = new SubClass1();

      // class CompilerDemo
      Class c = cls.getClass(); 
      System.out.println(c);

      // sub class SubClass1
      Class c1 = subcls.getClass();
      System.out.println(c1);

      /* Let's compile CompilerDemo class using command method */
      Object retval = Compiler.command("javac CompilerDemo");

      System.out.println("Return Value = " + retval); 
   }
} 

class SubClass1 extends CompilerDemo {
   // sub class
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

class com.tutorialspoint.CompilerDemo
class com.tutorialspoint.SubClass1
Return Value = null
java_lang_compiler.htm
廣告