问题描述
所以在根目录下有一个文件夹/usr/share/stuff
So there's a folder /usr/share/stuff
in the root directory
里面有一堆java文件,顶部有package org.name
定义
in stuff there are a bunch of java files with package org.name
definitions at the top
我正在运行 javac test.java
其中 test.java
在子域中
I am running javac test.java
where test.java
is in a subdomain
我将 /usr/share/stuff
添加到我的类路径中.
I added /usr/share/stuff
to my class path.
并在 test.java
的顶部添加 import org.name
但是我收到一个package does not exist
错误...为什么?
But I get a package does not exist
error...why?
推荐答案
它们是否位于正确的子目录中?
Are they in the right subdirectories?
如果你把/usr/share/stuff
放在类路径上,用package org.name
定义的文件应该在/usr/share/stuff/org/name
.
If you put /usr/share/stuff
on the class path, files defined with package org.name
should be in /usr/share/stuff/org/name
.
编辑:如果您还不知道这一点,您可能应该阅读 这个关于理解类路径的文档.
EDIT: If you don't already know this, you should probably read this doc about understanding classpath.
EDIT 2:抱歉,我没有意识到您在谈论 /usr/share/stuff
中的 Java 源文件.它们不仅需要位于适当的子目录中,而且您还需要编译它们..java
文件不需要在 classpath 上,而是在 源路径 上.(生成的 .class
文件需要在 classpath 上.)
EDIT 2: Sorry, I hadn't realised you were talking of Java source files in /usr/share/stuff
. Not only they need to be in the appropriate sub-directory, but you need to compile them. The .java
files don't need to be on the classpath, but on the source path. (The generated .class
files need to be on the classpath.)
如果它们不在正确的目录结构下,您可能会逃脱编译,但它们应该是,否则至少会产生警告.生成的类文件将位于正确的子目录中(如果您指定了 -d
,则在任何位置).
You might get away with compiling them if they're not under the right directory structure, but they should be, or it will generate warnings at least. The generated class files will be in the right subdirectories (wherever you've specified -d
if you have).
你应该使用类似 javac -sourcepath .:/usr/share/stuff test.java
的东西,假设你已经把 .java
文件放在 /usr/share/stuff
位于 /usr/share/stuff/org/name
下(或根据其包名称适当的任何名称).
You should use something like javac -sourcepath .:/usr/share/stuff test.java
, assuming you've put the .java
files that were under /usr/share/stuff
under /usr/share/stuff/org/name
(or whatever is appropriate according to their package names).
这篇关于Java 包不存在错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!