天天看點

Windows、Linux系統安裝JDK配置Java環境變量

一、Windows系統安裝JDK

  1、準備JDK安裝程式,可到Sun官網 http://www.oracle.com/technetwork/java/javaee/downloads/index.html 下載下傳需要的版本 (注意,32位系統選擇X86;64位系統選擇X64)。我這裡安裝 "jdk-6u23-windows-i586.exe"。

  2、輕按兩下運作 jdk-6u23-windows-i586.exe 程式,同意Sun協定,選擇安裝目錄,JDK目錄: C:\Java\jdk1.6.0_23;JRE目錄 C:\Java\Jre (根據情況自己定),下一步直到完成,退出重新開機系統。

二、Windows系統Java環境變量配置

  1、準備參數

  我的JDK安裝路徑為 "C:\Java\jdk1.6.0_23" ,建議此路徑不包含空格、目錄層次不要太深;具體根據機器情況确定。

  定義系統環境變量:

  JAVA_HOME = C:\Java\jdk1.6.0_23

  CLASSPATH = .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tool.jar

  Path = %JAVA_HOME%\bin;      

  注意:

    1) JAVA_HOME 指定 JDK 安裝路徑,該目錄下有 "bin"、"jre"、"lib" 等子目錄。很容易錯寫成 "C:\Java\jdk1.6.0_23\" ,請不要在 JDK 安裝路徑最後添加 "\"。

    2) CLASSPATH 使用系統變量 JAVA_HOME 作為參數(用%JAVA_HOME%表示),一般就按照上面的配置填寫就行。需要注意在最前端有一個 "." 表示目前目錄,并用 ";" 分隔。

    3) Path 需在系統原先配置的基礎上增加 Java 環境的配置,指向 JDK 的 "bin" 目錄,路徑最後不要加 "\"。與系統其他配置值之間用 ";" 分隔。

  2、配置步驟

  1. Windows XP/Windows 2003

    桌面 ----(右擊"我的電腦", 點選"屬性")----> 系統屬性 ----(點選"進階"頁籤, 點選"環境變量")----> 環境變量 ----(下面的系統變量, 點選"建立..."或選中點選"編輯")----> 填寫變量名和變量值 ----(點選所有的"确定"儲存更改)----> 完成

  2. Windows7

    桌面 ----(右擊"計算機", 點選"屬性")----> 控制台\系統和安全\系統 ----(點選"進階系統設定")----> 系統屬性 ----(點選"進階"頁籤, 點選"環境變量")----> 環境變量 ----(下面的系統變量, 點選"建立..."或選中點選"編輯")----> 填寫變量名和變量值 ----(點選所有的"确定"儲存更改)----> 完成

  3、版本驗證

  運作 "cmd" 打開Dos指令行;可先輸入 "java -version" 指令;再輸入 "java" 指令;輸入 "javac" 指令。如下:

C:\Users\Lilibo>java -version
java version "1.6.0_23"
Java(TM) SE Runtime Environment (build 1.6.0_23-b05)
Java HotSpot(TM) Client VM (build 19.0-b09, mixed mode, sharing)      
C:\Users\Lilibo>java
Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)

where options include:
-client       to select the "client" VM
-server       to select the "server" VM
-hotspot      is a synonym for the "client" VM  [deprecated]
                  The default VM is client.

-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
                  A ; separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
-D<name>=<value>
                  set a system property
-verbose[:class|gc|jni]
                  enable verbose output
-version      print product version and exit
-version:<value>
                  require the specified version to run
-showversion  print product version and continue
-jre-restrict-search | -jre-no-restrict-search
                  include/exclude user private JREs in the version search
-? -help      print this help message
-X            print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
                  enable assertions
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
                  disable assertions
-esa | -enablesystemassertions
                  enable system assertions
-dsa | -disablesystemassertions
                  disable system assertions
-agentlib:<libname>[=<options>]
                  load native agent library <libname>, e.g. -agentlib:hprof
                    see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
                  load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
                  load Java programming language agent, see java.lang.instrument

-splash:<imagepath>
                  show splash screen with specified image      
C:\Users\Lilibo>javac
用法:javac <選項> <源檔案>
其中,可能的選項包括:
-g                         生成所有調試資訊
-g:none                    不生成任何調試資訊
-g:{lines,vars,source}     隻生成某些調試資訊
-nowarn                    不生成任何警告
-verbose                   輸出有關編譯器正在執行的操作的消息
-deprecation               輸出使用已過時的 API 的源位置
-classpath <路徑>            指定查找使用者類檔案和注釋處理程式的位置
-cp <路徑>                   指定查找使用者類檔案和注釋處理程式的位置
-sourcepath <路徑>           指定查找輸入源檔案的位置
-bootclasspath <路徑>        覆寫引導類檔案的位置
-extdirs <目錄>              覆寫安裝的擴充目錄的位置
-endorseddirs <目錄>         覆寫簽名的标準路徑的位置
-proc:{none,only}          控制是否執行注釋處理和/或編譯。
-processor <class1>[,<class2>,<class3>...]要運作的注釋處理程式的名稱;繞過預設
的搜尋程序
-processorpath <路徑>        指定查找注釋處理程式的位置
-d <目錄>                    指定存放生成的類檔案的位置
-s <目錄>                    指定存放生成的源檔案的位置
-implicit:{none,class}     指定是否為隐式引用檔案生成類檔案
-encoding <編碼>             指定源檔案使用的字元編碼
-source <版本>               提供與指定版本的源相容性
-target <版本>               生成特定 VM 版本的類檔案
-version                   版本資訊
-help                      輸出标準選項的提要
-Akey[=value]              傳遞給注釋處理程式的選項
-X                         輸出非标準選項的提要
-J<标志>                     直接将 <标志> 傳遞給運作時系統


C:\Users\Lilibo>      

  由此可見,安裝配置的 JDK 環境正常,版本顯示為 java version "1.6.0_23" 和環境變量配置一緻。到此Windows環境變量配置完成。

三、Linux系統安裝JDK

  1、準備JDK安裝檔案,同樣可以在Sun官網進行下載下傳,下載下傳前選擇好相應的系統版本,可以下載下傳bin檔案也可以下載下傳rpm封包件。我這裡安裝 "jdk-1_6_0_17-linux-i586.bin" 此版本。

  2、登陸Linux伺服器(建議使用root使用者,可使用SSH工具操作Linux,SSH工具的File Transfer上傳檔案),上傳檔案至伺服器目錄,如:"/home/appuser/"。

  3、将檔案複制到需要安裝的目錄之下。指令:

  cp /home/appuser/jdk-1_6_0_17-linux-i586.bin /usr/local/jdk-1_6_0_17-linux-i586.bin      

  4、到JDK所在目錄(cd /usr/local/),賦予 jdk-1_6_0_17-linux-i586.bin 檔案可執行權限。指令:

  chmod 777 jdk-1_6_0_17-linux-i586.bin      

  5、執行檔案,安裝JDK。指令:

  ./jdk-1_6_0_17-linux-i586.bin      

  6、同意Sun協定,完成安裝,會生成目錄 "jdk1.6.0_17"。若沒有錯誤,到此則JDK安裝完成。可以使用指令(mv jdk1.6.0_17 jdk)将該目錄名更改為你需要的,例如 "jdk"。

四、Linux系統Java環境變量配置

  1、/etc/profile 檔案就是Linux系統配置檔案,為保險起見,先備份系統配置檔案(cp /etc/profile /etc/profile-bak)。

  2、進入/etc/目錄(cd /etc/)使用vi工具編輯profile檔案(使用 vi profile 指令進入編輯器指令模式,輸入"i"開始插入内容,将光标移動至目标位置,輸入完畢之後按Esc退出編輯模式回到指令模式,指令模式下輸入":wq!"儲存退出),在末尾輸入如下内容(Linux系統中的分隔符為":"):

  export JAVA_HOME=/usr/local/jdk
  export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

  export PATH=$JAVA_HOME/bin      

  3、儲存完畢之後使配置生效,指令:

  source /etc/profile      

  4、驗證版本

appuser@usm:~> java -version
java version "1.6.0_17"
Java(TM) SE Runtime Environment (build 1.6.0_17-b04)
Java HotSpot(TM) 64-Bit Server VM (build 14.3-b01, mixed mode)
appuser@usm:~>      
appuser@usm:~> java
Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)

where options include:
-d32          use a 32-bit data model if available

-d64          use a 64-bit data model if available
-server       to select the "server" VM
                  The default VM is server.

-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
                  A : separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
-D<name>=<value>
                  set a system property
-verbose[:class|gc|jni]
                  enable verbose output
-version      print product version and exit
-version:<value>
                  require the specified version to run
-showversion  print product version and continue
-jre-restrict-search | -jre-no-restrict-search
                  include/exclude user private JREs in the version search
-? -help      print this help message
-X            print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
                  enable assertions
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
                  disable assertions
-esa | -enablesystemassertions
                  enable system assertions
-dsa | -disablesystemassertions
                  disable system assertions
-agentlib:<libname>[=<options>]
                  load native agent library <libname>, e.g. -agentlib:hprof
                    see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
                  load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
                  load Java programming language agent, see java.lang.instrument
-splash:<imagepath>
                  show splash screen with specified image
appuser@usm:~>      
appuser@usm:~> javac
Usage: javac <options> <source files>
where possible options include:
-g                         Generate all debugging info
-g:none                    Generate no debugging info
-g:{lines,vars,source}     Generate only some debugging info
-nowarn                    Generate no warnings
-verbose                   Output messages about what the compiler is doing
-deprecation               Output source locations where deprecated APIs are used
-classpath <path>          Specify where to find user class files and annotation processors
-cp <path>                 Specify where to find user class files and annotation processors
-sourcepath <path>         Specify where to find input source files
-bootclasspath <path>      Override location of bootstrap class files
-extdirs <dirs>            Override location of installed extensions
-endorseddirs <dirs>       Override location of endorsed standards path
-proc:{none,only}          Control whether annotation processing and/or compilation is done.
-processor <class1>[,<class2>,<class3>...]Names of the annotation processors to run; bypasses default discovery process
-processorpath <path>      Specify where to find annotation processors
-d <directory>             Specify where to place generated class files
-s <directory>             Specify where to place generated source files
-implicit:{none,class}     Specify whether or not to generate class files for implicitly referenced files 
-encoding <encoding>       Specify character encoding used by source files
-source <release>          Provide source compatibility with specified release
-target <release>          Generate class files for specific VM version
-version                   Version information
-help                      Print a synopsis of standard options
-Akey[=value]              Options to pass to annotation processors
-X                         Print a synopsis of nonstandard options
-J<flag>                   Pass <flag> directly to the runtime system

appuser@usm:~>      

  至此,Windows、Linux系統下JDK安裝,Java環境變量配置全部完成。

轉載于:https://www.cnblogs.com/lioillioil/archive/2011/09/14/2176595.html