0

Since my command prompt messages are in portuguese, I do not know the exact words, but when I try to execute java -cp . My file, it says that it was unable to locate or execute the main class. I´ve searched, but since I am new to programming at all, so far I know that it may be a problem with Classpath. This is what I have:

A ShooterGame.class file already compiled properly with javac -cp . FILE. But when I do java -cp . CLASS, it shows that message.

How can I fix this? Thanks.

Edit: Yes, there is a ShooterGame.class in the directory. More specifically, there is a folder called game. Inside this folder you find the class I just mentioned. There are 4 entire classes I could post here. Comment if you need any of them(although I think the problem is in this class we are talking about).

ShooterGame.java (it has no package)

import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO; 
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.*;
import java.net.URL;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.awt.event.MouseEvent;
import game.input.InputHandler;
import game.player.Player;
import game.scenario.Block;

public class ShooterGame extends JFrame{
static int playerX=500;
static int playerY=520;

InputHandler input = new InputHandler(this);
public static Player player = new Player(playerX,playerY,50,50);
Block meteor = new Block(100,100,30,30);

public static void main(String[] args){
    ShooterGame game = new ShooterGame();
    game.run();
    System.exit(0);
}

static int windowWidth = 1300;
static int windowHeight = 600;
static int fps = 30;
static BufferedImage backBuffer = new BufferedImage(windowWidth, windowHeight, BufferedImage.TYPE_INT_RGB);

public void run(){
    boolean running = true;

    initialize();

    while(running){
        long time = System.currentTimeMillis();

        update();
        draw();

        time = (1000 / fps) - (System.currentTimeMillis() - time);

        if (time > 0) { 
            try{ 
                Thread.sleep(time); 
            } 
                catch(Exception e){}; 
        };
    }

}

public void initialize(){
    setTitle("--- Shooter Game ---");
    setSize(windowWidth, windowHeight);
    setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
}

public void update(){
    player.update(input);
    meteor.update(0);
}

public void draw(){

    Graphics g = getGraphics(); 

    Graphics bbg = backBuffer.getGraphics(); 

    bbg.setColor(Color.BLACK); 
    bbg.fillRect(0, 0, windowWidth, windowHeight); 
    player.Draw(bbg);
    meteor.Draw(bbg);

    g.drawImage(backBuffer, 0, 0, this); 
}
}
gabzerbinato
  • 68
  • 1
  • 1
  • 13

1 Answers1

0

Based on your code and comments, I believe you need

java -cp game ShooterGame

The game folder is where your ShooterGame.class is. So you could also

cd game
java -cp . ShooterGame

Now, if ShooterGame was in a game package; then it would be

java -cp . game.ShooterGame

And you would point to the folder that holds game.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Apparently the "cd game; java -cp . ShooterGame" worked, but it shows "Exception in thread 'main' java.lang.NoClassDefFoundError: game\player\Player". The player folder and Player.java are part of my game. – gabzerbinato Jan 07 '15 at 03:09
  • @gabzerbinato Move the `ShooterGame.class` up one folder, or add a `package game;` to `ShooterGame.java` and recompile. Or, create a new folder `game\game` and move `game\player` to `game\game\player`. Why did you put `ShooterGame.class` in `game` if you weren't going to put it in a `game` package? – Elliott Frisch Jan 07 '15 at 03:23
  • I added a "package game;" to ShooterGame.java and recompiled it. My current directory is _game_. What command should I use now? "java -cp . ShootGame"? – gabzerbinato Jan 07 '15 at 03:33
  • Move up one folder, and then "java -cp . game.ShootGame"; the package is part of the class name (and you can take a look at what a java class looks like compiled with `javap -v game\ShootGame.class`). Note that `import` is a convenience for you, internally classes are always fully qualified. – Elliott Frisch Jan 07 '15 at 03:39