2

I'm sort of a beginner in java and i just started working on a project using an applet and the applet & stuff all works.. but every time i try running it i get this error:

--------------------Configuration: AlexVega - JDK version 1.8.0_20 <Default>     - <Default>--------------------
load: class Game.class not found.
java.lang.ClassNotFoundException: Game.class
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:219)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:152)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:634)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:799)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:728)
at sun.applet.AppletPanel.run(AppletPanel.java:378)
at java.lang.Thread.run(Thread.java:745)

Process completed.

and here is my program:

package com.alexvega;
import java.applet.*;
import java.awt.*;

public class Game extends Applet{

private boolean running = false;
private Thread thread;

public synchronized void start(){
    if(running)
        return;

    running = true;
    thread = new Thread(thread);
    thread.start();
 }

 public void run(){
    long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int updates = 0;
int frames = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1){
    int tick;
    updates++;
    delta--;
}
int render;
frames++;

if(System.currentTimeMillis() - timer > 1000){
    timer += 1000;
    System.out.println("FPS: " + frames + " TICKS: " + updates);
    frames = 0;
    updates = 0;
}
}
}

public static void main(String args[]){

}

public void paint(Graphics g){
    setBackground(Color.BLACK);
}
}

and here is the second part of the program:

package com.alexvega;

public class Window {

public Window(Game game){
    game.start();

}

}

What am i doing wrong???

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Intrinza
  • 35
  • 2
  • 8
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson May 14 '15 at 05:18
  • The HTML is part of the problem. It apparently has something like `code='Game.class'` in the applet attribute when it should have `code='com.alexvega.Game'`. It also needs to be in the correct path relative to the code base or code base/Jar. – Andrew Thompson May 14 '15 at 05:21

1 Answers1

0

There is nothing wrong with your code. It just means that your Java class could not be found/loaded at runtime, a very common issue for Java developers. From the error, it says class Game.class not found which is self explanatory.

You will need to set your classpath to tell the compiler where your classes are.

Check out this link for instructions on how to set your classpath (for your Game class). There are also a lot more sites with instructions for this as well.

Happy coding.

codingbash
  • 1,102
  • 1
  • 9
  • 21