算法第四版編程環境配置(Windows10系統+IntelliJ Idea環境導入algs4.jar)


算法第四版編程環境配置(Windows10系統+IntelliJ Idea環境導入algs4.jar)

Contents

算法第四版當中的程序都使用到了algs4.jar作為依賴,所以在學習這本書中的程序之前,首先得配置好編程環境,將algs4.jar導入到我們的環境當中。這個過程還是比較復雜的,也有一點坑。

官網教程概覽

一開始我以為這個東西很簡單,官網就有教程,以為下載下來放到某個位置就行了,結果這個官網教程我其實就沒有理解明白,導致走了彎路,接下來我解釋一些教程中的一些比較容易迷惑的地方。
官網上對於環境搭建有兩個網頁,先來看一下。

  • Hello World in Java (Windows)
    • 這里邊要求我們安裝一個改裝版的idea,名字叫做lift,好處就是他做了一些額外的操作,使得我們可以直接運行書中的例程。但是問題在於我們不想重裝idea,想使用已經裝好的idea,該如何操作呢?
  • Java Algorithms and Clients,翻到頁面的中間位置。

導入algs4.jar的各種方法

編譯和運行java程序,無非兩個途徑:

  1. 命令行,可以是cmd,bash,powershell等shell程序。
  2. IDE,比如Eclipse,Idea等。

文檔對於這些情況都提及到了。接下來一一解釋一下。

1. 在Idea導入algs4.jar

  • IntelliJ. The IntelliJ project folders that we suppply for COS 226 and Coursera are configured to put algs4.jar in the Java classpath.

對於每個項目,需要配置java的classpath。
步驟參考IDEA配置java《算法》第四版環境

2. 在Git Bash中導入algs4.jar(必須使用lift安裝包)

  • Windows Git Bash (automatic). The Windows installer downloads algs4.jar to the C:\Program Files\LIFT-CS folder; adds it the Git Bash classpath; and provides the wrapper scripts javac-algs4 and java-algs4, which classpath in algs4.jar, for use from Git Bash.

這里就是重點了,講了windows installer也就是上面提及到的lift的那個安裝包做的額外操作:

  1. 自動下載algs4.jar文件並放到C:\Program Files\LIFT-CS位置(這是lift軟件的安裝目錄)
  2. algs4.jar添加到Git Bashclasspath
  3. 提供了兩個腳本(wrapper scripts)
    • javac-algs4
    • java-algs4

其實就相當於定義了2個命令,腳本內容大概就是在javacjava的基礎上增加了-classpath選項,使得編譯或者運行時能夠順利找到algs4.jar文件在哪里。而且cmd,powershell都是不行的,必須用git bash。

但是, 能夠使用上述兩個命令的條件就是得用lift安裝包安裝,否則是沒有兩個wrapper scripts的,所以運行javac-algs4java-algs4命令的結果肯定是'javac-algs4' 不是內部或外部命令,也不是可運行的程序 或批處理文件。
我沒有理解這一條,然后傻傻的在cmd/git bash當中測試兩個根本沒有的命令,肯定是無法成功的。
如果要實現這兩個命令,需要自己實現所謂的“wrapper scripts”,可以參考算法系列-0-使用javac-algs4和java-algs4 | 未知

3. 在Windows命令提示符中導入algs4.jar(手動操作)

  • Windows Command Prompt (manual). Download algs4.jar to a folder, say C:\Users\username\algs4. Next, add algs4.jar to the CLASSPATH environment variable.
    Windows 7: Start -> Computer -> System Properties -> Advanced system settings -> Environment Variables -> User variables -> CLASSPATH.
    Vista: Start -> My Computer -> Properties -> Advanced -> Environment Variables -> User variables -> CLASSPATH.
    Prepend the following to the beginning of the CLASSPATH variable:
    C:\Users\username\algs4\algs4.jar;
    The semicolons separate entries in the CLASSPATH.
    Click OK three times.
    If you don't see a variable named CLASSPATH, click New and in the popup window enter CLASSPATH for the variable name. Then, perform the instructions above.

就是配置CLASSPATH環境變量,把algs4.jar加到CLASSPATH當中。
做了這一步,可以用javac/java命令正常編譯/運行例程。如下圖。

但是是不能用 javac-algs4java-algs4命令,原因已經講過了。

4. 在Windows命令提示符中導入algs4.jar(笨辦法)

  • Windows Command Prompt (heavy handed). Download algs4.jar and put it in the directory %SystemRoot%\Sun\Java\lib\ext.

除了修改CLASSPATH變量之外,還可以直接將algs4.jar文件放到%SystemRoot%\Sun\Java\lib\ext\文件夾當中。

適合我的方法

前面說了,我的要求是不重裝idea,所以上面的第2個方法對我來說是沒有意義的。
我們需要做的步驟只有1,3,4(3和4選一個就可以)。
然后發現其實很多人都寫過這個方法了,我就不再重復寫具體的步驟,可以參考IDEA配置java《算法》第四版環境

參考


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM