1

None of the answers to this common question solve my problem.

I have a file directory tree like such

root
 |
 +--> com
        |
        +--> Game.java
        +--> Window.java

I compiled Game.java and Window.java sucesffully and the resulting tree is like so

root
 |
 +--> com
        |
        +--> Game.java
        +--> Game.class
        +--> Window.java
        +--> Window.class

I changed directory back to root and ran the following command from the osx terminal

java com.Game

and the I received the following error

Could not find or load main class

I'm not sure why, here are my classes

Game.java

package com;

import java.awt.*;

public class Game extends Canvas implements Runnable {

    private static final long mSerialVersionUid = -240870510533527854L;

    public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;

    public Game(){

        new Window(WIDTH, HEIGHT, "Let's build a game!", this);
    }

    public synchronized void start(){

    }

    @Override
    public void run(){

    }

    public static void main(String[] args){


    }
}

Window.java

package com;

import java.awt.Canvas;
import javax.swing.JFrame;
import java.awt.Dimension;

public class Window extends Canvas {

    public Window(int width, int height, String title, Game game){

        JFrame frame = new JFrame(title);

        frame.setPreferredSize(new Dimension(width, height));
        frame.setMaximumSize(new Dimension(width, height));
        frame.setMinimumSize(new Dimension(width, height));

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.add(game);
        frame.setVisible(true);
        game.start();
    }
}
Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
the_prole
  • 8,275
  • 16
  • 78
  • 163
  • The problem was that a class path must be at least a two level deep domain name. I have only a domain name that is only one level deep. – the_prole Jun 11 '17 at 03:44
  • @Stephen C This is not a duplicate, I am not asking what the error means, I am asking how to solve it, there are multiple solutions to this error. – the_prole Jun 11 '17 at 03:46
  • The linked answers explain how to solve it. But you need to read them, and work out which of the solutions apply to your particular version of the problem. But seriously, you can't expect people to provide a fresh answer for your Question, just because you can't find one of the many previous answers. This stuff is not rocket science ... but you do need to **understand** it ... not just copy/paste an solution. If you understand it, then you can solve it. – Stephen C Jun 11 '17 at 05:21
  • Thank you for your concern, but none of the solutions worked for me. Adding an additional path to the class path solved my problem. When I said domain name, I confused the practice of using a domain name to create a class path with the actual nomenclature of the class path. Yes, I get it, a domain name is not a class path. – the_prole Jun 11 '17 at 08:13
  • Did you read this? https://stackoverflow.com/a/18093929/139985 - specifically *"Reason #2 - the application's classpath is incorrectly specified"*. And the material that it links to? – Stephen C Jun 11 '17 at 11:48

1 Answers1

0

This should work as expected — I ran your code on macOS 10.12.4 and it worked just fine ;)

You might have the CLASSPATH environment variable — without the current directory! — defined in your shell. If this is the case, try this instead:

java -cp . com.Game

By the way, to actually show the window you defined, create a Game object in your main method:

public static void main(String[] args) {
    new Game(); 
}
Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
  • `java -cp . com.Game` results in the same error message – the_prole Jun 11 '17 at 03:33
  • The problem was that a class path must be at least a two level deep domain name. I have only a domain name that is only one level deep. – the_prole Jun 11 '17 at 03:44
  • 1
    *"The problem was that a class path must be at least a two level deep domain name. I have only a domain name that is only one level deep."* - I am afraid that that is nonsense. The classpath is a series of pathnames, not domain names. Please READ the Q&A the linked to ... and to the linked articles that explain what the classpath is. – Stephen C Jun 11 '17 at 05:27