java 包不存在错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6838872/
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-10-30 17:31:12  来源:igfitidea点击:

Package does not exist error

javaimportpackage

提问by masher

I've just come back to fix some bugs in a small program that I've written, and now I can't compile because a package doesn't exist (according to javac).

我刚刚回来修复我写的一个小程序中的一些错误,现在我无法编译,因为一个包不存在(根据javac)。

As far as I know, I haven't changed anything in my class path, or the contents of my directories. I don't know if there have been any Java updates in between.

据我所知,我没有更改我的类路径或目录中的任何内容。我不知道中间是否有任何 Java 更新。

In my root directory, I have the four Java files that comprise my program. They don't have any package ascribed to them. Additionally, I have the directory structure org\apache\commons\cliin the root directory which contains the CLI java files from the Apache Commons. The Apache files do have a package ascribed that mirrors the directory structure in which they lie.

在我的根目录中,我有四个组成我的程序的 Java 文件。他们没有任何归于他们的包裹。此外,我org\apache\commons\cli在根目录中有目录结构,其中包含来自 Apache Commons 的 CLI java 文件。Apache 文件确实有一个包,该包反映了它们所在的目录结构。

If I issue the command javac -verbose -d build CommandLine.java, it fails with

如果我发出命令javac -verbose -d build CommandLine.java,它会失败

CommandLine.java:28: package org.apache.commons.cli does not exist
import org.apache.commons.cli.*;
^

CommandLine.javais the entry point of the program.

CommandLine.java是程序的入口点。

There is no attempt by the compiler to compile the CLI files, and if I compile them beforehand, the failure still takes place. Also, the CLI is also available in my classpath.

编译器没有尝试编译 CLI 文件,如果我事先编译它们,仍然会发生失败。此外,CLI 也可在我的类路径中使用。

I am running javac 1.6.0_12.

我正在运行 javac 1.6.0_12。

What can I do/try?

我能做什么/尝试什么?

.

.

EDITED FOR ANSWER:

编辑答案:

As accepted below, I altered the compilation command to be:

正如下面接受的那样,我将编译命令更改为:

javac -verbose -d build -classpath . CommandLine.java

Now it works. Don't know why it did before, and why it stopped now though...

现在它起作用了。不知道以前为什么会这样,为什么现在停止了......

.

.

EDITED FOR CAUSE OF PROBLEM

针对问题原因进行编辑

I installed a program that also used the CLASSPATH to run its own things. It changed the CLASSPATH that belonged to the user, but left the global one alone. I re-added the java classpath things back in and it works again!

我安装了一个程序,它也使用 CLASSPATH 来运行它自己的东西。它更改了属于用户的 CLASSPATH,但保留了全局路径。我重新添加了 java 类路径的东西,它又可以工作了!

采纳答案by duffymo

Did you unpack that Apache JAR and add the .class files to your application? Why didn't you just add the JAR to your CLASSPATH?

您是否解压了该 Apache JAR 并将 .class 文件添加到您的应用程序中?你为什么不直接将 JAR 添加到你的CLASSPATH?

You should not be depending on a CLASSPATHenvironment variable. Better to use the -classpathoption for javac.exewhen you compile and java.exewhen you run.

您不应该依赖于CLASSPATH环境变量。最好在编译和运行时使用该-classpath选项。javac.exejava.exe

回答by Jim Garrison

Unless it's a typo, notice you said com\apache\commons\clibut the import wants org.apache.commons.cli-- orgvs com

除非是打字错误,请注意您说的com\apache\commons\cli但导入需要org.apache.commons.cli-- orgvscom