張世旭
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 1111018-package -[Java 套件(package)](https://www.twcode01.com/java/java-package.html) > 注意不管是使用筆記本或是NotePad++來編輯,存檔格式請用ANSI,使用UTF8反而會讓中文輸出會變成亂碼。 <-- 實作..可用utf-8 .. > javac命令是把.java文件編譯成.class文件的命令 先建立一個目錄 d:\1018 ## 建立三個檔案 > Computer.java, Linear.java, Test.java ## this.getClass() 可知目前物件的類別名 [Determine class name in Java: getClass()](https://www.techiedelight.com/determine-class-name-java/) ```java= //Computer.java package dyu.csie; public class Computer{ public int xxx; public void print1(){ // The following prints <package name> + <class name> System.out.println(this.getClass().getName()); System.out.println(this.getClass().getCanonicalName()); System.out.println(this.getClass().getTypeName()); System.out.println(this.getClass().getSimpleName()); System.out.println("xxx="+xxx); } } ``` >>cmd >>javac Computer.java <--與別人無關..所以可編譯成功. 會產生Computer.class ```java= // Linear.java package dyu.csie; public class Linear extends dyu.csie.Computer{ public void test1(int value){ xxx=value; print1(); } } ===================== 使用import..可減少打package name. package dyu.csie; import dyu.csie.Computer; public class Linear extends Computer{ public void test1(int value){ xxx=value; print1(); } } ``` ```= javac Linear.java <--會出現找不到Computer.class error: cannot find symbol ``` 方法一: d:\1018\Linear.java d:\1018\dyu\csie\Computer.class 這樣編譯就會成功. (將Computer.class搬進package目錄) (javac 編譯時 可找到Computer.class) D:\1018>javac Linear.java 方法二: 將 Computer.java, Linear.java 搬到dyu\csie\下 回到 d:\1018\ 下編譯 D:\1018>javac dyu\csie\Linear.java 工作目錄\dyu\csie\Linear.java 檔案要擺在package目錄下.. 工作目錄\dyu\csie\Linear.class 類別要擺在package目錄下.. ```java= //Test.java public class Test{ public static void main(String [] args){ dyu.csie.Linear obj=new dyu.csie.Linear(); obj.test1(3); dyu.csie.Computer obj2=new dyu.csie.Computer(); obj2.print1(); //雖然xxx沒有設定..但物件初始化為0. } } ``` 工作目錄\Test.java <--它沒有package.. 編譯 D:\1018>javac Test.java ```java= //可使用import..不用再打package-name import dyu.csie.Linear; public class Test{ public static void main(String [] args){ Linear obj=new Linear(); obj.test1(); } } ``` <hr> > 編譯三個檔案 > javac Computer.java > javac Linear.java > javac Test.java > java Test <hr> ## javac -d 參數說明 > 使用javac與參數-d ddd目錄, 將編譯好之檔案xxx.class置放於ddd > 下編譯指令 > javac -d 2 dyu\csie\Computer.java 會自動建立 2\dyu\csie\Computer.class > javac -d 3 dyu\csie\Computer.java 會自動建立 3\dyu\csie\Computer.class ## javac -cp 告知編譯器, 所需要的class在那裡(路徑) 編譯Linear.java > javac -d 3 dyu\csie\Linear.java 編譯時需要Computer.class 因Computer.class 在3\package\Computer.class 所以編譯時加入參數 -cp 接著 類別class的路徑.. 此例 -cp 3 <--告訴編譯器 到目前目錄下的3\ 去找找看是否有Computer.class javac -d 3 Linear.java -cp 3;d:\abc;c:\jar\;.;.. javac -d 3 Linear.java -cp 3;.;.. ## javac -cp 用分號隔開各個路徑 用分號隔開各個路徑, 一般會加入 . 目前目錄 與 .. 上一層目錄 <hr> 編譯Test.java > javac -cp 3 -d 3 Test.java > ## java 執行時也要加-cp > java -cp 3;. Test 執行時 也要給classpath 路徑 會到classpath找尋Test.class 找到後再執行.. <hr> ## [產生jar檔: jar -cvf](https://zh.wikipedia.org/zh-tw/JAR_(%E6%96%87%E4%BB%B6%E6%A0%BC%E5%BC%8F)) jar命令主要是用來把class文件打包成jar文件的工具; 使用方式如下: > jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ... -c 創建新檔案 -t 列出檔案目錄 -x 從檔案中提取指定的 (或所有) 文件 -u 更新現有檔案 -i 為指定的 jar 文件生成索引 index.. 其他參數 -v 在標準輸出中生成詳細輸出 -f 指定檔案文件名 -m 包含指定清單文件中的清單信息 -n 創建新檔案後執行 Pack200 規範化 -e 為捆綁到可執行 jar 文件的獨立應指定應用程序入口點 -0 僅存儲; 不使用任何 ZIP 壓縮 -P 保留文件名中的前導 '/' (絕對路徑 -M 不創建條目的清單文件 -C 更改為指定的目錄並包含以下文 <hr> D:\10181>jar -cvf my.jar -C 3 dyu\csie\Computer.class -C 3 dyu\csie\Linear.class added manifest adding: dyu/csie/Computer.class(in = 1113) (out= 606)(deflated 45%) adding: dyu/csie/Linear.class(in = 306) (out= 235)(deflated 23%) 或 <hr> jar -cvf my.jar -C 3 dyu\csie\\*.class ## jar -tf 查看jar包文件列表內容 ```= jar -tf my.jar ``` ## java執行 -cp 可給jar檔, 多個jar可用分號隔開 ```= java -cp .\my.jar;3;. Test ``` 需要Linear.class 去my.jar裡找..找不到 再到3\目錄下找.. my.jar 上 按右鍵 選7-zip 開啟壓縮檔 <hr> ## jar -cvfe > 可執行JAR檔案 一個可執行Java程式以及其使用的庫檔案可以打包在一個JAR檔案中。 可執行的JAR檔案中的Manifest檔案用代碼 Main-Class: myPrograms.MyClass 指定了入口點類,注意要指明該類的路徑(-cp參數將被忽略)。 有些作業系統可以在點擊後直接執行可執行JAR檔案。 ```= -cvfe <--加入e 告訴jar要產生可執行的jar檔...還要告知那一個檔要執行, 此例為 Test類別 ``` <hr> jar -cvfe my1.jar Test -C 3 Test.class -C 3 dyu\csie\\*.class <--星號無法作用 jar -cvfe my1.jar Test -C 3 Test.class -C 3 dyu\csie <--改成給目錄名..會抓目錄下所有class類別.. jar -cvfe my2.jar Test 3\dyu\csie\*.class 3\Test.class <-不要用-C 可成功. <hr> ```= D:\10181>jar -cvfe my1.jar Test -C 3 Test.class -C 3 dyu\csie\Computer.class 3\dyu\csie\Linear.class ``` > java -jar my1.jar <--執行命令.. <hr> ```java= package dyu.ee; public class Circuit extends dyu.csie.Computer{ void test1(int value){ xxx=value; print1(); } } ``` <hr> ```java= package dyu.ee; import dyu.csie.Computer; public class Circuit extends Computer{ void test1(){ xxx=100; } } ``` <hr> https://jasonblog.github.io/note/java/yong_javac_ming_ling_xing_bian_yi_duo_ge_java_wen_.html > 在使用javac編譯的時候,可以通過-cp (-classpath)參數來執行要引用的jar包 如果是多個jar包,windows下以;分割,linux下以:分割 如 linux 下 javac -cp ".:/usr/lib/java/lib/*" ./HttpTest.java java -cp ".:/usr/lib/java/lib/*" HttpTest 注意 1、-cp要指定到具體的jar包或者路徑,如果是一個路徑下的所有jar包, 可以使用 通配符 javac -cp ".:/path/*" 2、-cp 後面參數最好要使用引號包含 <hr> > 假設要引用的jar放在D:/test目錄下,名字為t1.jar, java原始檔放在D:/test/src目錄下,名字為t2.java。 編譯: > javac -cp d:/test/t1.jar d:/test/src/t2.java 執行: > java -cp d:/test/t1.jar;d:/test/src t2 注意,分號後面沒有空格,否則報錯。 需要注意的是,如果java原始檔是有package的,比如package是com.example, 原始檔為com.example.t2.java. 比如檔案的儲存目錄為: D:/test/src/com/example/t2.java, 然後比如t1.jar放在D:/test/lib/ 下面, 那麼javac編譯的時候要在D:/test/目錄下進行。 > 可以用-d選項指定.class的生成位置。 javac -cp d:/test/lib/t1.jar;classes;. -d classes src/com/example/t2.java <hr>

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully