Reputation: 31
It's my first time using packages in Java, and I'm not sure where I went wrong. I'm trying to make a Tetris game. Here's part of my code:
package mytetris;
import javax.swing.*;
import java.awt.*;
public class Tetris extends JFrame {
// class constructor
public Tetris() {
super("Tetris");
this.setSize(400, 600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
// just started, haven't really done anything
this.setVisible(true);
}
public static void main(String[] args) {
Tetris myTetris = new Tetris();
}
}
I then compiled it using javac Tetris.java
and then I tried running it using java Tetris
, this is what the terminal gave me:
Exception in thread "main" java.lang.NoClassDefFoundError: Tetris (wrong name: mytetris/Tetris)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:637)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Upvotes: 2
Views: 2166
Reputation: 11
This error is because the class mentioned in your program is not fount by JVM in the class loader. There are various reasons for this error. check the detailed concept here https://te-xplore-ch.blogspot.com/2020/08/java-architecture.html May be your package was not added in the source folder. Click build path for the folder and click use as a source folder
Upvotes: 0
Reputation: 1
If you observe compilation code by the command javap your class name i.e
javap Tetris
then it display it as
class mytetris.Tetris{
}
So when you execute this program ,you have to give command like this.
java mytetris.Tetris
then your program executes smoothly.
Upvotes: 0
Reputation: 357
Suppose you have your file in /home/mytetris/Tetris.java
Then you need to run "java mytetris.Tetris" in the directory home.
Because you specified package mytetris, it will go to find your files in the directory mytetris, where it will find the class Tetris.
Upvotes: 3
Reputation: 420
Assuming that you have the following project structure:
project/src/mytetris/Tetris.java
project/classes
Run from the project dir:
javac src/mytetris/Tetris.java -d classes
It will compile Tetris.java and put it into classes dir.
Upvotes: 0