Nigh7Sh4de
Nigh7Sh4de

Reputation: 455

Problems with the import statement

I've created the following java file:

import java.awt.*;

public class Text {
    public static void main(String[] args) {
        String str = "I AM A SENTENCE";
        String[] lines = wrap(str, 5);
        for (int i=0;i<lines.length;i++) {
            if (lines[i] != null) System.out.println(lines[i]);
        }
        Font myFont = new Font("Impact", Font.BOLD, 36);
        System.out.println(String.valueOf(charToPixel(str, myFont)));
    }
    public static String[] wrap(String str, int w) {
        char[] string = str.toCharArray();
        System.out.println("string.length: " + String.valueOf(string.length));
        int charCounter = 0;
        String[] line = new String[20];
        String work = "";
        int x = 0;
        for (int i=0;i<string.length;i++) {
            charCounter++;
            System.out.println("charCounter: " + String.valueOf(charCounter));
            System.out.println("i: " + string[i]);
            if (charCounter > w) {
                charCounter = 0;
                System.out.println(String.valueOf(x));
                line[x] = work;
                x++;
                work = "";
                i--;
            }
            else {
                work += string[i];
            }
        }
        line[x] = work;
        return line;
    }
}

Now, I also created a simple applet that I want to use to receive the String[] and then one by one output it using Graphics.drawString(). I created a .jar file using the default manifest and the previous class file. The class file's directory is as follows within the jar: Dennis\Text.class. I added my jar into the CLASSPATH. I used the import statement as follows: import Dennis.*;

However when I compile the applet (btw the Text.class had compiled perfectly) I get the following compilation error:

bad class file: B:\Apps\Java\JDK\lib\Text.jar(Dennis/Text.class) class file contains wrong class: Text Please remove or make sure it appears in the correct subdirectory of the classpath.

As far as I can tell, I put everything in the right place and the import statement was successful.

So what am I doing wrong?

Upvotes: 0

Views: 131

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500135

The class file's directory is as follows within the jar: Dennis\Text.class.

It shouldn't be. It's not in any package, so it should just be directly within the root directory of the jar file. Ideally put it within a package (not Dennis, which violates Java naming conventions) and then make your jar file structure match the package structure.

Upvotes: 2

Related Questions