Getting errors in CMD

I’ve made a game which runs perfectly fine in Eclipse, but doesn’t work with CMD.

I can compile the file, but whenever I try to run it through CMD, it says “Error: Could not find or load main class Game” (Game is my main class). I tried using CMD with this simple program:


public class Test() {
     public static void main(String[] args) {
          System.out.println("Test");
     }
}

And it worked. I got the output “Test” in my CMD.
(My game contains more than one class. Four, to be exact.)

do you have the “public static void main(String[] args) {};” method in the class you’re trying to run?

Is your “Game” class within a package?

My Game class contains this:


public static void main(String[] args) {
     Game game = new Game();
     game.start();
}

And yes, it’s in the package “com.hansi.pong”

Then your main class is: com.hansi.pong.Game, not Game

I’ve tried that too.

I’m able to run this in CMD:


public class Test {
     public static void main(String[] args
     {
          System.out.println("Test!");
     }
}

But when I try to run this the exact same way as I do with the above program I get this: “Error: Could not find or load main class com.hansi.spaceinvaders.Game”


package com.hansi.spaceinvaders;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class Game extends Canvas implements Runnable {
	private static final long serialVersionUID = 1L;
	Thread gameThread;
	JFrame frame;
	
	public int width = 500;
	public int height = width;
	public String title = "Space Invaders";
	
	boolean running = false;
	
	public Game() {
		setSize(width, height);
		
		frame = new JFrame(title);
		frame.setResizable(false);
		frame.setSize(width, height);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(this, BorderLayout.CENTER);
		
		frame.pack();
		frame.setVisible(true);
	}
	
	public void start() {
		running = true;
		if (gameThread == null) {
			gameThread = new Thread(this);
			gameThread.start();
		}
	}
	
	public void stop() {
		running = false;
		if (gameThread != null) {
			try {
				gameThread.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void run() {
		while (running) {
			tick();
			render();
			
			try {
				Thread.sleep(16);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void tick() {
		
	}
	
	public void render() {
		BufferStrategy bs = getBufferStrategy();
		if (bs == null) {
			createBufferStrategy(3);
			return;
		}
		
		Graphics g = bs.getDrawGraphics();
		//Render here
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, width, height);
		
		g.dispose();
		bs.show();
	}
	
	public static void main(String[] args) {
		Game game = new Game();
      game.start();
	}
}

What is the exact java command line you are running?

Where are the class files generated by javac? Are they sitting in a series of folders under the current location such as com/hansi/spaceinvaders?

Suppose you have a folder called as root, the root of your project files. If your main class has no package, then the location would be


<root> $ dir
Test.java
Test.class
<root> $

Now if your class is in a package, for example, say [icode]com.me.mypackage[/icode], then the location will be like this.


<root> $ dir
com
<root> $ cd com
<root/com> $ dir
me
<root/com> $ cd me
<root/com/me> $ dir
mypackage
<root/com/me> $ cd mypackage
<root/com/me/mypackage> $ dir
Test.java
Test.class
<root/com/me/mypackage> $

Remember that you can only run these java classes from the root directory. So in the first case, you will call this.


<root> $ java Test
Hello World ;)
<root> $

And even if you have any packages, you should be calling java from the first directory, i.e., root. Like this.


<root> $ java com.me.mypackage.Test
Hello World ;)
<root> $

I hope now you understand what is wrong with your calls. Hope this helps.

Thank you, SHC!
I finally got it working! :smiley: