Sign in to follow this  

[java] How do I import a custom class? ("Bad class file class file contains wrong class")

This topic is 3042 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I defined my own class foo() in a custom utility package, in foo.java thus:
[source="java"]
package custom_util;

public class foo {
  private int i;

  public foo(int _i) {
    i = _i;
  }

  public int geti() {
    return i;
  }
}

Then in another file I try to use this class in another class in the same directory (in particular, I am trying to test function foo).
[source="java"]
public class footest {
  public void main()
    {
      foo f = new foo(1);
    }
}

When I try javac, I get "Bad class file: foo.class, class file contains wrong class". If I try to explicitly import custom_util.foo, it states that it "cannot access foo" and then continues with the rest of the previous error message. In short, I can't compile, and I'm not honestly clear as to why not. It occurs to me that this is easily solvable...but I have no idea how; thanks for your help in advance.

Share this post


Link to post
Share on other sites
Quote:
Yes, both of these source files are in a folder called custom_util.
Move footest.java one folder up. Unnamed packages shouldn't be inside another package's folder. You may have to set the classpath to this same folder for it to work, but at first, try to run javac from the same folder as footest.java.

Share this post


Link to post
Share on other sites
You're going to show us the exact command line you're running, and the exact directory layout (and where you're running the command line from).

My wild guess would be that you've either got your source and compiled classes laid out differently to what you're telling javac, or you're passing a path to your source files when you should be passing a path to your compiled .class files.

Share this post


Link to post
Share on other sites
My compile line is just

javac footest.java

The directory structure is
DIR/DIR/DIR/custom_util/footest.java
DIR/DIR/DIR/custom_util/foo.java
DIR/DIR/DIR/custom_util/foo.class

Thanks for your efforts, guys; I'll try building footest.java one dir up and see what happens.

EDIT: Trying to build footest.java one directory up (outside of custom_util) gives me "package.custom_util does not exist".

This has got to be a classpath issue. How does one examine/set the class path?

Share this post


Link to post
Share on other sites
That's a bit odd. I've just built the following files:
foo.java
package custom_util;

public class foo {
private int i;

public foo(int _i) {
i = _i;
}

public int geti() {
return i;
}
}
footest.java
import custom_util.foo;

public class footest {
public static void main(String[] args)
{
foo f = new foo(1);
}
}
Using a directory structre like
somewhere/footest.java
somewhere/custom_util/foo.java
And it builds (and runs) just fine with javac footest.java while inside the folder somewhere. If your classpath is already defined as an environment variable ($CLASSPATH), try printing it. You may set the classpath using -classpath blabla1;blabla2, but that's useful when you've got .class files, not .java files. Try building foo.java first, from the parent directory, javac custom_util/foo.java and then build footest the way you did it before. Just for the sake of it, remove previously built .class files in there, if they could be causing any problems..

Share this post


Link to post
Share on other sites
Quote:
Original post by serratemplar
This has got to be a classpath issue. How does one examine/set the class path?


With the -classpath (-cp for short) flag for javac. In particular, try 'javac -cp . footest.java'. (The '.' is of course the special name for the current directory.)

Also, moving to Java Development.

Share this post


Link to post
Share on other sites

This topic is 3042 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this