Allan Bassett
Allan Bassett

Reputation: 31

When putting my classes in a package the code breaks

I have some java classes that work fine until I put them in a package (which they really should be in) and I can't work out why.

Class Board

import javax.swing.JPanel;

public class Board extends JPanel {
    public Board() {
    }

    public static void main(String[] args) {
    }
}

Class Skeleton

import javax.swing.JFrame;

public class Skeleton extends JFrame {

    public Skeleton() {
        add(new Board());
        setTitle("Skeleton");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300, 280);
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(false);
    }
    public static void main(String[] args) {
        new Skeleton();
    }
}

And all this works fine until I put

package skeleton;

at the top of each one.

upon doing so I get

skeleton.java:9: error: cannot find symbol
        add(new Board());
                ^
  symbol:   class Board
  location: class Skeleton
1 error
Error: Could not find or load main class skeleton.Skeleton

Its probably something simple that i have overlooked but I can't seem to find out what is wrong with it.

Edit

Both the classes are in a file called skeleton and skeleton is always spelled correctly.

Im am running javac on both Board.java and Skeleton.java and java on skeleton.Skeleton

EDIT 2

I fixed the

skeleton.java:9: error: cannot find symbol
            add(new Board());
                    ^
      symbol:   class Board
      location: class Skeleton

error, I forgot I was passing each file to javac individually, but the

Error: Could not find or load main class skeleton.Skeleton

still persists.

Upvotes: 2

Views: 913

Answers (3)

Sanito Machiavelli
Sanito Machiavelli

Reputation: 133

I was having the same problem with the same type of the files. However, I was successful to compile both of the files doing: cd .. and compiling from parent folder like this:

zetcode/*.java

the compilation was successful. But then when i tried

zetcode>java Snake

Error: Could not find or load main class Snake

The only solution was to go up to two folders, root of the package and from there issue

java com.zetcode.Snake

and it worked.

Upvotes: 1

John Eipe
John Eipe

Reputation: 11246

I recreated your code in my machine and it works fine.

Named them as Board.java and Skeleton.java


~$ cd skeleton
~/skeleton$ ls
Board.java  Skeleton.java
~$ cd ..
~$ javac skeleton/*.java
~$ ls skeleton
Board.class  Board.java  Skeleton.class  Skeleton.java
~$ java skeleton.Skeleton

Output came flashing on my screen!!

I tired this way too.


~$ cat arglist
skeleton/Board.java
skeleton/Skeleton.java
~$ javac @arglist
~$ java skeleton.Skeleton 

That too works.

Upvotes: 1

Mike Samuel
Mike Samuel

Reputation: 120586

Unless you pass both files to javac, it will look for skeleton.Board in a file skeleton/Board.java relative to a directory on your source path.

You need to rework the directory structure so that both files are in a directory named "skeleton" which is in the class path, or pass both files as arguments to javac.

Upvotes: 0

Related Questions