Forums Register Login

Exception in thread "main"

+Pie Number of slices to send: Send
Hi,
can you help me out troubleshooting the following code (origin: HeadFirst Java), the compilation works fine, but runtime errors occur. Here's the code, then follows the runtime output.
Thanks in advance, Patrick

/*
* Created on 17.12.2005
*/

package kapitel_15;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class TippDesTagesServer {

String[] tippListe = {"Nehmen Sie kleinere Bissen zu sich.", "Holen Sie sich die engen Jeans. Nein, Sie sehen darin NICHT dick aus.", "Mit einem Wort: unm�glich!", "Seien Sie ehrlich, nur heute mal. Sagen Sie Ihrem Boss, was Sie *wirklich* denken.", "Vielleicht wollen Sie doch noch mal �ber diesen Haarschnitt nachdenken."};

public void los() {

try {
ServerSocket serverSock = new ServerSocket(4242);



while(true) {
Socket sock = serverSock.accept();
PrintWriter writer = new PrintWriter(sock.getOutputStream());
String tipp = getTipp();
writer.println(tipp);
writer.close();
System.out.println(tipp);
}

} catch(IOException ex) {
ex.printStackTrace();
}
} // los() schlie�en

private String getTipp() {
int random = (int) (Math.random() * tippListe.length);
return tippListe[random];
}

public static void main(String[] args) {
TippDesTagesServer server = new TippDesTagesServer();
server.los();
}
}


And here's the output:

Exception in thread "main" java.lang.NoClassDefFoundError: TippDesTagesServer (wrong name: kapitel_15/TippDesTagesServer)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
+Pie Number of slices to send: Send
Hi,

Depending on your os, open a terminal and go to the build directory (where the class has been compiled). You should see there the 'kapitel_15' directory.
Run the app by calling "java kapitel_15.TippDesTagesServer"
+Pie Number of slices to send: Send
Hi Olivier,
your advice didn't help, thanks anyhow. What finally did it is to take away the ligne "Package kapitel_15" in the java code.
I'll do the theory about packages tomorrow, and then i should understand what's happening there.
Nice day, Patrick
+Pie Number of slices to send: Send
I would be interested in knowing what ultimately fixes this problem. I am having the same issue. I have the two directory structures below:
..\src\com\example\ch6
..\classes\com\example\ch6

and my 3 class files are all in the src folder, and have the "package com.example.ch6" statement.
GameHelper.java
DotCom.java
DotComBust.java

I go to the src folder and type 'javac -d ..\classes com\example\ch6\*.java', and all of the compiled files to to the classes\com\example\ch6 folder.

But then when I go to the classes folder and type 'java com\example\ch6\DotComBust', I get the message:

E:\javadir\HF-Java\MyCode\classes>java com\training\ch6\DotComBust
Exception in thread "main" java.lang.NoClassDefFoundError: com\training\ch6\DotC
omBust (wrong name: com/training/ch6/DotComBust)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

E:\javadir\HF-Java\MyCode\classes>

I am sure we are having the same problem. Any help is appreciated.
+Pie Number of slices to send: Send
Great! I fixed the problem by changing my java statement.

I was typing 'java com\training\ch6\DotComBust', which is incorrect.

The correct command is 'java com.training.ch6.DotComBust'. Apparently, typing the path to DotComBust makes java expect to find the package in a subfolder instead of in the current folder.
Is that a spider in your hair? Here, threaten it with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 930 times.
Similar Threads
Question on thread
main() & package question
Linux vs. Windows jar file - cannot find main class
NoClassDefFoundError: wrong name
Basic Question please answer
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 18:07:52.