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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:57:25  来源:igfitidea点击:

Java error: package 'x' does not exist

javafiledirectoryjavacjava-package

提问by Jerfov2

So I have a file tree in Linux that looks like this:

所以我在 Linux 中有一个文件树,看起来像这样:

  • ~/keyboard_warriors/
    • test/
      • ConfigTest.java
    • ConfigParser.class
    • ConfigParser.java
  • ~/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_warriorsclasspath, 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 -cpflag on javac.

根据您编译这些类的方式,您必须将其添加到带有-cp标志的类路径中javac

回答by T.J. Crowder

Given the structure you've quoted, it would be odd to be importing keyboard_warriorsinto ConfigTest; from the structure, you'd expect to see package keyboard_warriors;instead, because ConfigTestis in the keyboard_warriorsdirectory.

鉴于您引用的结构,导入keyboard_warriorsConfigTest;会很奇怪;从结构中,您希望看到的是package keyboard_warriors;,因为ConfigTestkeyboard_warriors目录中。

If ConfigTestisn'tin the keyboard_warriorspackage, it's probably in the wrong directory.

如果ConfigTest不是keyboard_warriors包装,它可能是在错误的目录。

In general: When you're compiling classes, the .javafile should be in a directory named for the package it's in. So for instance, if ConfigTestis 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 ConfigTestisn'tmeant to be in that package, you'd probably move it out of that directory; perhaps:

如果ConfigTest打算在该包中,您可能会将其移出该目录;也许:

  • ~/keyboard_warriors/
    • test/
      • ConfigTest.java
  • 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.*;就有意义了,前提是您的主目录在您的类路径中。