# 編譯並執行 console

> 這裡示範如何在windows下，利用筆記本及CMD(命令提示字元)編譯並執行Java，若不是windows環境就快速掠過吧，或是直接看下一章的eclipce。

1. **安裝JDK**

   到Oracle的[官網](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)下載相應平台及位元數的JDK，並安裝。(安裝可以使用下一步大法) 安裝完畢後，開起檔案管理員到剛剛的安裝目錄下，找到 javac.exe，複製該目錄路徑。 ![](https://1201963393-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MLRnhT9aTkN9HvaBP28%2Fsync%2Fc9cd8d0569d93fb774695145d9413db741c16ffe.JPG?generation=1604653626952741\&alt=media) 開起命令提示字元(windows+R 輸入 cmd)。 輸入： `path <剛剛複製的路徑>` 按enter 輸入： `javac` 檢查有沒有錯誤 ![](https://1201963393-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MLRnhT9aTkN9HvaBP28%2Fsync%2F31a284df82de668f81b07179a601a634d049dd35.JPG?generation=1604653626376738\&alt=media) 看到這個畫面代表設定成功，命令提示字元先放旁邊，不要關掉！ (關掉就要重新設定，除非去控制台設定系統變數，在此不贅述，有興趣去google)
2. **撰寫Java程式**

   在C槽(純粹方便好找，可以放別的地方)下開一個 Test.java 的文字，注意副檔名不可以是 .txt，必須是 .java，也不能是 .java.txt。

   在Test.java中，鍵入以下程式碼：

   ```java
   class Test{
      public static void main(String[] args){
          System.out.println("Hello Java~~");
      }
   }
   ```

   確認沒打錯字後儲存。
3. **編譯Java程式**

   開啟剛剛放在一旁的命令提示字元， 輸入； `cd C:\` 此時命令提示字元的工作目路會切換到C槽底下。

   輸入：

   `javac Test.java`

   ![](https://1201963393-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MLRnhT9aTkN9HvaBP28%2Fsync%2Ff1cb995ba3ef99741bb566c61eb2e8f8c323f8c7.JPG?generation=1604653626511546\&alt=media)

   若沒有顯示任何錯誤訊息，即代表編譯成功，可以觀察一下，Test.java旁邊是不是多一個Test.class，那就是利用.java檔編譯產生的.class執行檔。
4. **執行Java程式**

   執行Java程式的方式很簡單，只要下命令java <執行檔名稱>就可以，不用加副檔名。

   `java Test`
5. **觀察執行結果**

   ![](https://1201963393-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MLRnhT9aTkN9HvaBP28%2Fsync%2F7c819e29e57f036624fa9adeced9a32abbbf6373.JPG?generation=1604653626712266\&alt=media)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yubin551.gitbook.io/java-note/compile_run_console.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
