如何在 Eclipse 中使用库进行算法第一部分 Coursera 课程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15008099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:02:31  来源:igfitidea点击:

How to use libraries for Algorithms part I Coursera course in Eclipse

javaeclipseide

提问by Seekeer

I've started Coursera Algorythms course. Practice assignments must be done on Java and they suggest using DrJava as IDE, but it's really unconvenient. So I'd like to use Eclipse, but the problem is that I couldn't use 2 libraries(algs4.jarand stdlib.jar), which are provided for this course. After doing steps from this guideI was able to include them in my project:

我已经开始了 Coursera Algorythms 课程。练习作业必须在Java上完成,他们建议使用DrJava作为IDE,但确实不方便。所以我想使用 Eclipse,但问题是我无法使用本课程提供的2 个库(algs4.jarstdlib.jar)。完成本指南中的步骤后,我能够将它们包含在我的项目中:

enter image description here

在此处输入图片说明

but when I try to use their classes in my project, Eclipse doesn't allow to do it, and I couldn't import them by Ctrl+ Space:

但是当我尝试在我的项目中使用它们的类时,Eclipse 不允许这样做,并且我无法通过Ctrl+导入它们Space

enter image description here

在此处输入图片说明

And even when I am trying directly import class like import stdlib.StdRandom;it still doesn't work.

甚至当我尝试直接导入类时,import stdlib.StdRandom;它仍然不起作用。

What can I do to make it work properly?

我该怎么做才能使其正常工作?

采纳答案by MorningFung

Why don't you use these two JARs—— stdlib-package.jar and algs4-package.jar.

为什么不使用这两个JAR——stdlib-package.jar 和algs4-package.jar。

And below the code page(http://algs4.cs.princeton.edu/code/)

在代码页下方(http://algs4.cs.princeton.edu/code/

Q. If I use a named package to structure my code, the compiler can no longer access the libraries in stdlib.jar or algs4.jar. Why not?

问:如果我使用命名包来构建我的代码,编译器将无法再访问 stdlib.jar 或 algs4.jar 中的库。为什么不?

A. The libraries in stdlib.jar and algs4.jar are in the "default" package. In Java, you can't access classes in the default package from a named package. If you need to use our libraries with a named package, you can use these package versions: stdlib-package.jar and algs4-package.jar.

A. stdlib.jar 和 algs4.jar 中的库在“默认”包中。在 Java 中,您无法从命名包访问默认包中的类。如果您需要将我们的库与命名包一起使用,您可以使用这些包版本:stdlib-package.jar 和 algs4-package.jar。

Warning: if you are taking Princeton COS 226 or Coursera, Algorithms, Part I or II, you must use the default package verison of our libraries to facilitate grading.

警告:如果您正在参加普林斯顿 COS 226 或 Coursera,算法,第一部分或第二部分,您必须使用我们图书馆的默认包版本以方便评分。

Showing my test success: Showing my test success

显示我的测试成功: 显示我的测试成功

回答by OVB

You can't access the APIs because the authors have defined them in the default package and Java doesn't recognize these. The authors mention on their website (http://algs4.cs.princeton.edu/code/) that if you are using these in named packages, configure your build path to include the jar from http://algs4.cs.princeton.edu/code/algs4.jar.

您无法访问这些 API,因为作者已在默认包中定义了它们,而 Java 无法识别这些 API。作者在他们的网站 ( http://algs4.cs.princeton.edu/code/)上提到,如果您在命名包中使用这些,请配置您的构建路径以包含来自http://algs4.cs.princeton的 jar .edu/code/algs4.jar

HTH!

哼!

回答by Lemon Juice

Follow the below steps

请按照以下步骤操作

Right click on your project > Properties. It will open the following window

右键单击您的项目 > 属性。它将打开以下窗口

enter image description here

在此处输入图片说明

Select "Java Build Path" from the left side. Now select "Libraries" tab from the right side

从左侧选择“Java Build Path”。现在从右侧选择“库”选项卡

enter image description here

在此处输入图片说明

Click on "Add External Jars"

点击“添加外部罐子”

enter image description here

在此处输入图片说明

Add the Jar files.

添加 Jar 文件。

Now you are done

现在你完成了

回答by John Humphreys - w00te

All you should have to do in order to include the JARs is right click on the top of your project/object tree in the project explorer panel on the left, do "Build Path/Configure Build Path", click "Libraries" tab, and "add external jars", add them there.

为了包含 JAR,您应该做的就是在左侧的项目资源管理器面板中右键单击项目/对象树的顶部,执行“构建路径/配置构建路径”,单击“库”选项卡,然后“添加外部罐子”,将它们添加到那里。

They should happily become available both while editing your source code and while building it in Eclipse after that.

在编辑源代码和之后在 Eclipse 中构建它时,它们应该很高兴地可用。

Also note that, assuming you have the class name right when you type, you should be able to hover over the class name in your code and the intelli-sense/auto-complete feature will suggest where you should import it from. You can add the import to the correct library that way with ease.

另请注意,假设您在键入时拥有类名,您应该能够将鼠标悬停在代码中的类名上,智能感知/自动完成功能将建议您应该从何处导入它。您可以轻松地将导入添加到正确的库中。

回答by Brett Ryan

One option is that there is a maven repo on githubprovided by slok. The bad part of this is the jars added were those using the default package, so you can't use a package structure.

一种选择是slok提供的github 上有一个maven repo。不好的部分是添加的 jars 是使用默认包的那些,所以你不能使用包结构。

UPDATE: I have added the package versions of the jar to the repo and created a pull requestit has now been accepted.

更新:我已将 jar 的包版本添加到 repo 并创建了一个现在已被接受的拉取请求

Note that I have changed the identifiers to match the original package structure from Princeton.

请注意,我已更改标识符以匹配来自普林斯顿的原始包结构。

<dependencies>
  <dependency>
    <groupId>edu.princeton.cs.introcs</groupId>
    <artifactId>algs4-package</artifactId>
    <version>1.0</version>
  </dependency>

  <dependency>
    <groupId>edu.princeton.cs.introcs</groupId>
    <artifactId>stdlib-package</artifactId>
    <version>1.0</version>
  </dependency>
</dependencies>

<repositories>
  <repository>
    <id>org.coursera.algs4</id>
    <name>Algs4 coursera course custom repository</name>
    <url>https://raw.github.com/slok/algs4-mvn-repo/master</url>
  </repository>
</repositories>

回答by tk_y1275963

From their website: http://algs4.cs.princeton.edu/code/

从他们的网站:http: //algs4.cs.princeton.edu/code/

Eclipse (manual). Download stdlib.jar and algs4.jar to a folder and add each jar >file to the classpath variable to the build path of a project via Project -> >Properties -> Java Build Path -> Libaries -> Add External JARs.

Eclipse(手动)。将 stdlib.jar 和 algs4.jar 下载到一个文件夹中,然后通过 Project ->>Properties -> Java Build Path -> Libaries -> Add External JARs 将每个 jar >file 添加到类路径变量到项目的构建路径。

回答by Khalid Amiri

It is probably because there is no package structure in the libraries, so you can't import classes from it. This might help you, How to use classes in Referenced Libraries in Eclipse.

可能是因为库中没有包结构,所以不能从中导入类。这可能对您帮助,How to use classes in Referenced Libraries in Eclipse

回答by Bharat Gupta

If you are anyone still looking for the solution :-

如果您仍然在寻找解决方案:-

OS X / Linux

OS X / Linux

% javac -cp .:stdlib.jar MyProgram.java % java -cp .:stdlib.jar MyProgram

% javac -cp .:stdlib.jar MyProgram.java % java -cp .:stdlib.jar MyProgram

Windows

视窗

% javac -cp .;stdlib.jar MyProgram.java % java -cp .;stdlib.jar MyProgram

% javac -cp .;stdlib.jar MyProgram.java % java -cp .;stdlib.jar MyProgram

For refrence :- https://introcs.cs.princeton.edu/java/stdlib/

参考:- https://introcs.cs.princeton.edu/java/stdlib/

回答by Kate

I'm using Gradle. In this case the build.gradlefile should have a dependency and can look like

我正在使用Gradle。在这种情况下,build.gradle文件应该有一个依赖项,看起来像

repositories {
    jcenter()
}

dependencies {
    compile 'edu.princeton.cs:algs4:1.0.3'
}

After this you can import anything from it in your class. For example:

在此之后,您可以在您的班级中从中导入任何内容。例如:

import edu.princeton.cs.algs4.StdIn;

回答by Sanju Varghese

Incase of Gradle, add the maven repository entry in repositories section of build.gradle as follows:

在 Gradle 的情况下,在 build.gradle 的 repositories 部分添加 maven 存储库条目,如下所示:

repositories {
    mavenCentral()
    maven {
        url 'https://dl.bintray.com/algs4/maven/'
    }
}

Add the dependency as follows:

添加依赖如下:

compile group: 'edu.princeton.cs', name: 'algs4', version: '1.0.4'

In case of Maven, add the entry in repositories tag in build.xml, as follows:

如果是 Maven,在 build.xml 中的 repositories 标签中添加条目,如下所示:

<repositories>
  <repository>
    <id>org.coursera.algs4</id>
    <name>Algs4 lib</name>
    <url>https://dl.bintray.com/algs4/maven/</url>
  </repository>
</repositories>

Add the dependency as follows:

添加依赖如下:

<dependencies>
  <dependency>
    <groupId>edu.princeton.cs</groupId>
    <artifactId>algs4</artifactId>
    <version>1.0.4</version>
  </dependency>
</dependencies>