javac - 未找到超类

发布于 2024-11-08 02:17:34 字数 806 浏览 4 评论 0原文

我正在尝试在 Unix(具体来说是 Solaris)中编译一个 Java 项目(Lisp 解释器)。我的问题是,我有一些扩展其他类的类,但是当 javac 尝试编译扩展类时,它说找不到超类。

我输入的命令:

javac -g LispObject.java
javac -g Sexp.java

第一个工作正常;但第二个因以下错误而停止:(

Sexp.java:7: Superclass interpreter.LispObject of class interpreter.Sexp not found.
public class Sexp extends LispObject
                          ^
1 error

我不知道它是否非常相关,但这是一个类项目,所以我必须使用 makefile 进行编译,所以我不能使用任何 IDE 进行编译..这很糟糕,因为在 NetBeans 中一切正常。)

LispObject.java 代码:

package interpreter;

public class LispObject extends java.lang.Object
{
   // empty
}

Sexp.java 代码:

package interpreter;

public class Sexp extends LispObject
{
   // body stuff goes here
}

I'm trying to compile a Java project (of a Lisp interpreter) in Unix (Solaris to be specific). My problem is that I have a few classes that extend other classes, but when javac attempts to compile the extended classes, it says it can't find the superclass.

Commands I type:

javac -g LispObject.java
javac -g Sexp.java

The first one works fine; but the second halts with the following error:

Sexp.java:7: Superclass interpreter.LispObject of class interpreter.Sexp not found.
public class Sexp extends LispObject
                          ^
1 error

(I don't know if it's very relevant but this is for a class project so I have to use a makefile for the compilation, so I can't use any IDEs for the compilation... which sucks because everything worked fine in NetBeans.)

Code for LispObject.java:

package interpreter;

public class LispObject extends java.lang.Object
{
   // empty
}

Code for Sexp.java:

package interpreter;

public class Sexp extends LispObject
{
   // body stuff goes here
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

菩提树下叶撕阳。 2024-11-15 02:17:34

尝试将 -cp . 添加到 javac 调用中,以便它在当前目录中搜索已编译的类。第二次调用是:

javac -cp . -g Sexp.java

或者,一次性提供所有 .java 文件:

javac -g LispObject.java Sexp.java

通常最好的方法是设置一个特定的构建目录(我将其称为 /some/dir/build)并使用:

javac -cp /some/dir/build -d /some/dir/build your_file.java

以便 .class 文件不会污染您的源文件目录。

请记住,您应该使目录结构与包层次结构保持同步。如果您的类位于解释器包中,则应将它们放置在具有该名称的目录中。

因此,在这个特定实例中,您的编译命令应如下所示:

javac -cp /some/dir/build -d /some/dir/build interpreter/LispObject.java
javac -cp /some/dir/build -d /some/dir/build interpreter/Sexp.java

Try adding -cp . to your javac calls so that it searches the current directory for already compiled classes. Second call would be:

javac -cp . -g Sexp.java

Alternatively, give it all your .java files in one go:

javac -g LispObject.java Sexp.java

Generally the best would be to set up a specific build directory (i'll call it /some/dir/build) and use:

javac -cp /some/dir/build -d /some/dir/build your_file.java

so that the .class files don't come polluting your source file directorie(s).

Keep in mind that you should keep your directory structure in sync with your package hierarchy. If your classes are in the interpreter package, they should be placed in a directory with that name.

So in this specific instance, your compile command should look like:

javac -cp /some/dir/build -d /some/dir/build interpreter/LispObject.java
javac -cp /some/dir/build -d /some/dir/build interpreter/Sexp.java
为你鎻心 2024-11-15 02:17:34

如果您希望编译器识别超类或将生成的类添加到类路径(如 ds_student 所说),则必须在一行中编译所有类

you have to compile all the classes in one line if you want the compiler to recognize the superClass or add the generated class to classpath as said by ds_student

錯遇了你 2024-11-15 02:17:34

通常的包目录问题。

对于编译包中的类,请将它们放在与包结构相对应的目录中,并从层次结构的根调用javac(或提供层次结构根作为-sourcepath,以及 -d-classpath 中的输出目录)。

javac interpreter/LispObject.java
javac interpreter/Sexp.java

The usual package-directory problem.

For compiling classes in packages, put them in a directory corresponding to the package structure, and call javac from the root of the hierarchy (or provide the hierarchy root as -sourcepath, and the output directory in -d and -classpath).

javac interpreter/LispObject.java
javac interpreter/Sexp.java
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文