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

huangapple go评论62阅读模式
英文:

How to import packages from the external Library in Java?

问题

以下是您要翻译的内容:

我是一个新手程序员。我找到了一个有趣的库,想开始尝试使用它玩耍。但情况并不是很好。这个库是99年的,使用了我不熟悉的JUnit(单元测试框架),所以有很多让人困惑的东西。但似乎导致我失败的根本问题更简单。也就是说,我在导入包时遇到了问题。

这个库中有一个名为StandardEvalTest.java的测试文件。我将它移动到了主Java目录下,现在我正尝试并且失败地使用JUnit来运行它。

这个包路径org.pokersource.game.Deck直接从测试文件StandardEvalTest.java所在的目录引用。

我还将主Java目录添加到了PATH环境变量中。我假设这样做可以让导入语句找到这个包。

这两个方法都没有起作用。我还怀疑也许仅有Deck.java和Deck.class是不够的,我可能需要做一些工作来将其创建成一个包。但根据我从Oracle文档中了解到的,唯一需要的是在文件头部指定包名。而这似乎已经有了。

所以我已经无计可施了。请帮帮我!

附加信息,受@Dhrubo回答启发:
我尝试运行的测试确实位于该库的主Java文件夹中。(我将它移动到这里是希望从这里运行时能更容易地找到这个包)

如果我尝试编译这个测试而不是使用JUnit运行它,似乎无法找到JUnit类和其他与JUnit相关的内容。

[哦,好吧,我是个白痴!别理我]

英文:

I'm a n00b coder. 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. 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. I moved to it to main Java directory and now I'm trying and failing to launch it using JUnit.

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

This package path org.pokersource.game.Deck goes directly from the directory where the test StandardEvalTest.java sits.

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

I also added the main java directory to the PATH environmental variable. Which as I assumed will allow import to locate the package.

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

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. 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.

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

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. (I moved it here hoping that when running from here it would be easier to find the package)

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

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.

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

[Oh OK I'm an idiot! Dont't mind me]

答案1

得分: 2

你应该在运行StandardEvalTest.java时包含如下的包

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

并且从包的根目录运行它,我假设这是你想要编译的自定义Java文件。你运行的目录应该是你包目录的父目录。

** 我还注意到,你正在尝试编译StandardEvalTest.java而不是Deck.java ... 那么请检查你的StandardEvalTest.java文件是否存在于所需的位置。

英文:

You should include the package while running StandardEvalTest.java as below

javac -cp [classpath] 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. You run directory should be parent of your package directory.

** 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.

huangapple
  • 本文由 发表于 2020年9月20日 15:12:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63976419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定