0

I'm a beginner in java using VS Code and I'm creating a simple program that just makes a board. For some reason when I go into my terminal to compile the Main class it gives me these errors. But in debug mode it works just fine, why is this?

PS C:\Users\blake\Desktop\javavscode\BattleShip> javac Main.java
Main.java:10: error: cannot find symbol
        Board board = new Board(input);
        ^
  symbol:   class Board
  location: class Main
Main.java:10: error: cannot find symbol
        Board board = new Board(input);
                          ^
  symbol:   class Board
  location: class Main

Here are is my code:

package BattleShip;

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("What size do you want the board to be?: ");
        int input = Integer.valueOf(scanner.nextLine());
        Board board = new Board(input);
        board.print();

    }
}



package BattleShip;

public class Board {
    private char[][] board;
    private char water = '~';
    private int size;

    public Board(int size){
        this.size = size;
        this.board = new char[size][size];

        for (int i = 0; i < board.length; i++){
            for (int j = 0; j < board.length; j++){
                board[i][j] = water;
            }
        }
    }

    public void print(){
        for (int i = 0; i < board.length; i++){
            for (int j = 0; j < board.length; j++){
                System.out.print(board[i][j] + " ");
            }
            System.out.println("");
        }
    }


}

In vscode when i used debug and then run it works for some reason but I don't understand what this really means

PS C:\Users\blake\Desktop\javavscode\BattleShip> c:; cd 'c:\Users\blake\Desktop\javavscode'; & 'C:\Users\blake\AppData\Local\Programs\Eclipse Adoptium\jdk-17.0.7.7-hotspot\bin\java.exe' '-XX:+ShowCodeDetailsInExceptionMessages' '-cp' 'C:\Users\blake\AppData\Roaming\Code\User\workspaceStorage\b656ed2f78ffde906270d9c291aeeeb0\redhat.java\jdt_ws\javavscode_a7ed4f9c\bin' 'BattleShip.Main' What size do you want the board to be?:

Also in my directory for the Board and Main class, there are now Board.class and Main.class, do i need to use these instead?

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
NATCHY
  • 9
  • "Cannot find symbol" is a compiler error, and should affect *both* debug mode *and* standard mode runs. – Hovercraft Full Of Eels Jul 02 '23 at 20:02
  • Try the following: `javac -d classes *.java;java -cp classes BattleShip.Main` Package names should really be all lower case – g00se Jul 02 '23 at 20:02
  • Does this answer your question? [How to compile and run a java class with a package](https://stackoverflow.com/questions/31636728/how-to-compile-and-run-a-java-class-with-a-package) – tevemadar Jul 02 '23 at 21:39

1 Answers1

0

Inside an IDE classes might be loaded and the situation is not as in reality.

  • You should have all sources/classes on the class-path, respecting the packages as subdirectories
  • Better compile in the project root where the source directory tree resides
  • The target directory where the built classes are generated is relevant here

So manual compilation should be thus:

C:\Users\blake\Desktop\javavscode> javac -cp . BattleShip\*.java

Where the class-path -cp . specifies the current directory, above BattleShip.


It should be mentioned that IDEs like IntelliJ Community Edition, NetBeans, eclipse and VSCode are normally used for developing, instead of the command line.

Package names are normally in small letters.

It is advisable to use a build infrastructure like maven or gradle which provide conventions, library version management and more.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138