javac - 未找到超类
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试将
-cp .
添加到javac
调用中,以便它在当前目录中搜索已编译的类。第二次调用是:或者,一次性提供所有
.java
文件:通常最好的方法是设置一个特定的构建目录(我将其称为 /some/dir/build)并使用:
以便
.class
文件不会污染您的源文件目录。请记住,您应该使目录结构与包层次结构保持同步。如果您的类位于解释器包中,则应将它们放置在具有该名称的目录中。
因此,在这个特定实例中,您的编译命令应如下所示:
Try adding
-cp .
to yourjavac
calls so that it searches the current directory for already compiled classes. Second call would be:Alternatively, give it all your
.java
files in one go:Generally the best would be to set up a specific build directory (i'll call it /some/dir/build) and use:
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:
如果您希望编译器识别超类或将生成的类添加到类路径(如 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
通常的包目录问题。
对于编译包中的类,请将它们放在与包结构相对应的目录中,并从层次结构的根调用
javac
(或提供层次结构根作为-sourcepath
,以及-d
和-classpath
中的输出目录)。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
).