开发者

javac - Superclasses not found

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 projec开发者_运维技巧t 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
}


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


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜