算法第四版编程环境配置(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《算法》第四版环境

参考

posted @ 2020-08-11 23:42  Howfar's  阅读(1870)  评论(0编辑  收藏  举报