user2719875 user2719875 - 1 year ago 50
Java Question

Getting a java.lang.NoClassDefFoundError error even when .class file exists

This is my file structure:

T1 -> T1sample -> src -> t1sample ->
T1 -> T1sample -> src -> t1sample -> T1.class

I created these folders and files myself (except for T1.class, I got that by running 'javac' from the shell).

This is

package t1sample;

public class T1 {
public static void main(String []args) {

Now when I try to run
java T1
it returns an error saying:

java.lang.NoClassDefFoundError: T1 (wrong name: t1sample/T1)

Any idea why?

Answer Source

First create the directory structure... (Using gvim as an editor)

~/Desktop $ mkdir -p T1sample/src/t1sample && cd T1sample/src/t1sample && gvim

Create the source...

package t1sample;

public class T1 {

    public static void main(String... args) {

Compile ...

~/Desktop $  cd T1sample/src/t1sample/
~/Desktop/T1sample/src/t1sample $  javac


~/Desktop/T1sample/src/t1sample $  ls -la
total 2
-rw-r--r-- 1 Edward None 411 Sep 28 21:46 T1.class
-rw-r--r-- 1 Edward None 137 Sep 28 21:45

Run it...

~/Desktop/T1sample/src/t1sample $  cd ..
~/Desktop/T1sample/src $  java t1sample.T1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download