Java 错误:包“x”不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26962727/
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
Java error: package 'x' does not exist
提问by Jerfov2
So I have a file tree in Linux that looks like this:
所以我在 Linux 中有一个文件树,看起来像这样:
- ~/keyboard_warriors/
- test/
- ConfigTest.java
- ConfigParser.class
- ConfigParser.java
- test/
- ~/keyboard_warriors/
- 测试/
- 配置测试文件
- ConfigParser.class
- 配置解析器
- 测试/
The problem is that when I try to type in Terminal:javac ConfigTest.java
问题是当我尝试在终端中输入时:javac ConfigTest.java
I get the error:
我收到错误:
ConfigTest.java:2: error: package keyboard_warriors does not exist
import keyboard_warriors.*;
^
This is really bugging me and I could not find any answers anywhere. If you could solve my problem, I would be a very happy person.
这真的让我烦恼,我在任何地方都找不到任何答案。如果你能解决我的问题,我会很高兴的。
采纳答案by Jerfov2
I figured out the answer a long time ago but forgot to post it. sorry :). It is actually quite simple. I was trying to compile the classes from inside the test/
folder. What I did was go up to where all the classes were accessible without having to use a absolute path and without using ../
in the path (AKA the keyboard_warriors/
folder. I just compile from there and everything was A-OK. Thanks for the input though, it did help some ;).
我很久以前就想出了答案,但忘记发布了。对不起 :)。其实很简单。我试图从test/
文件夹内部编译类。我所做的是转到所有类都可以访问的地方,而无需使用绝对路径,也无需../
在路径中使用(也就是keyboard_warriors/
文件夹。我只是从那里编译,一切都很好。不过感谢您的输入,它确实有帮助;)。
回答by Makoto
It's not going to recognize a directory outside of the classpath as being part of the classpath.
它不会将类路径之外的目录识别为类路径的一部分。
What you probably want is to include another folder inside of where your test is being run:
您可能想要的是在运行测试的地方包含另一个文件夹:
test/
keyboard_warriors/
ConfigTest.java
ConfigParser.java
This also presumes that you have declared these classes to be in the keyboard_warriors
classpath, by this:
这还假定您已通过以下方式将这些类声明在keyboard_warriors
类路径中:
package keyboard_warriors;
Depending on how you're compiling these classes, you'd have to add it to the classpath with the -cp
flag on javac
.
根据您编译这些类的方式,您必须将其添加到带有-cp
标志的类路径中javac
。
回答by T.J. Crowder
Given the structure you've quoted, it would be odd to be importing keyboard_warriors
into ConfigTest
; from the structure, you'd expect to see package keyboard_warriors;
instead, because ConfigTest
is in the keyboard_warriors
directory.
鉴于您引用的结构,导入keyboard_warriors
到ConfigTest
;会很奇怪;从结构中,您希望看到的是package keyboard_warriors;
,因为ConfigTest
在keyboard_warriors
目录中。
If ConfigTest
isn'tin the keyboard_warriors
package, it's probably in the wrong directory.
如果ConfigTest
不是在keyboard_warriors
包装,它可能是在错误的目录。
In general: When you're compiling classes, the .java
file should be in a directory named for the package it's in. So for instance, if ConfigTest
is meant to be in the package, you'd keep your directory/file structure as it is and use
一般而言:当您编译类时,.java
文件应位于以其所在包命名的目录中。因此,例如,如果ConfigTest
打算在包中,则应保持目录/文件结构不变,并且利用
package keyboard_warriors;
...in ConfigTest.java
.
...在ConfigTest.java
.
If ConfigTest
isn'tmeant to be in that package, you'd probably move it out of that directory; perhaps:
如果ConfigTest
不打算在该包中,您可能会将其移出该目录;也许:
- ~/keyboard_warriors/
- test/
- ConfigTest.java
- test/
- ConfigParser.class
- ConfigParser.java
- ~/keyboard_warriors/
- 测试/
- 配置测试文件
- 测试/
- ConfigParser.class
- 配置解析器
...and then import keyboard_warriors.*;
would make sense, provided that your home directory is in your classpath.
...然后import keyboard_warriors.*;
就有意义了,前提是您的主目录在您的类路径中。