简体   繁体   English

如何从Java中的外部库导入包?

[英]How to import packages from the external Library in Java?

I'm a n00b coder.我是一名 n00b 编码员。 I found an interesting library and trying to start toying with it.我发现了一个有趣的图书馆,并试图开始玩弄它。 Which is not going great.这不是很好。 This library is from 99' and uses JUnit (which I'm unfamiliar with) so there is a lot of confusing stuff.这个库来自 99' 并使用 JUnit(我不熟悉)所以有很多令人困惑的东西。 But it seems like the source of my failing even more elementary.但这似乎是我失败的根本原因。 Namely I have troubles importing packages.也就是说,我在导入包时遇到了麻烦。

This library has a test called StandardEvalTest.java.这个库有一个名为 StandardEvalTest.java 的测试。 I moved to it to main Java directory and now I'm trying and failing to launch it using JUnit.我将其移动到 Java 主目录,现在我正在尝试使用 JUnit 启动它,但失败了。

在此处输入图像描述

This package path org.pokersource.game.Deck goes directly from the directory where the test StandardEvalTest.java sits.这个 package 路径 org.pokersource.game.Deck 直接来自测试 StandardEvalTest.java 所在的目录。

在此处输入图像描述

I also added the main java directory to the PATH environmental variable.我还将主要的 java 目录添加到 PATH 环境变量中。 Which as I assumed will allow import to locate the package.我假设这将允许导入找到 package。

在此处输入图像描述

None of those two things help.这两件事都没有帮助。 Also I was suspecting that maybe Deck.java and Deck.class are not enough and I have to do some work to create a package from it.我还怀疑 Deck.java 和 Deck.class 可能还不够,我必须做一些工作才能从中创建一个 package。 But as far as I can say from Oracle doc the only thing needed is a package name in the header. Which seems to be present.但据我所知,根据 Oracle 文档,唯一需要的是 header 中的 package 名称。这似乎存在。

在此处输入图像描述

So I'm out of moves.所以我没有动作了。 Please help!请帮忙!

PS: Some additional info inspired by @Dhrubo 's answer: The test I'm trying to run indeed sits in the main java folder of the library. PS:受@Dhrubo 的回答启发的一些附加信息:我正在尝试运行的测试确实位于库的主 java 文件夹中。 (I moved it here hoping that when running from here it would be easier to find the package) (我把它移到这里希望从这里运行时更容易找到包)

在此处输入图像描述

If I'm trying to compile the test instead of running it with JUnit he seem to fail to find JUnit classes and other JUnit related stuff.如果我尝试编译测试而不是使用 JUnit 运行它,他似乎无法找到 JUnit 类和其他 JUnit 相关内容。

在此处输入图像描述

[Oh OK I'm an idiot! [哦好吧我是个白痴! Dont't mind me]别管我]

You should include the package while running StandardEvalTest.java as below您应该在运行 StandardEvalTest.java 时包含 package,如下所示

javac -cp [classpath] org.pokersource.game.StandardEvalTest.java javac -cp [类路径] org.pokersource.game.StandardEvalTest.java

and run it from package root directory, I am assuming it is custom java file that you want to compile.并从 package 根目录运行它,我假设它是您要编译的自定义 java 文件。 You run directory should be parent of your package directory.您运行的目录应该是 package 目录的父目录。

** I also see, you are trying to compile StandardEvalTest.java instead of Deck.java... then check your StandardEvalTest.java file whether it exists in desired location. ** 我还看到,您正在尝试编译 StandardEvalTest.java 而不是 Deck.java... 然后检查您的 StandardEvalTest.java 文件是否存在于所需位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM