Running java.class

Questions about programming languages and debugging
Post Reply
User avatar
isapiens
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 533
Joined: 05 May 2006, 16:00
14
Location: Turn around

Running java.class

Post by isapiens »

Hi, i wrote the 'Hello World' in java (just started programing..). Anyway, i managed to compile it successfully, but now i cant run the program. I am using cmd to do it, but everytime i run it i get this message : ""Exception in thread "main" java.lang.NoClassDefFoundError" I am sure its a noob error. My .class file is in the same directory as java.exe. It said something about set 'CLASSPATH= java HelloWorld' on internet but that didnt help it. What am i doing wrong?

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11566
Joined: 06 Apr 2005, 16:00
16
Location: The zone.
Contact:

Post by bad_brain »

well, can have different reasons....

- PATH variable not set properly (make sure the path to the bin-folder of Java is in the PATH variable of the environment variables)

- start the app with java appname, not with java appname.class, pretty common beginner-error.

- wrong class name (make sure the class-file has the same name as announced in the source, if the class in the source is called "class x" and the file is called "y.class" it will not work)

- a simple mistake in writing, Java is a little annyoing because the error messages can be very confusing, you can post the source here and we will have a look at it... :wink:

User avatar
DNR
Digital Mercenary
Digital Mercenary
Posts: 6114
Joined: 24 Feb 2006, 17:00
15
Location: Michigan USA
Contact:

Goodbye Cruel World

Post by DNR »

I can't believe I just heard "Hello World" programming :lol:

it brings back some memories. I wish you copied the code you wrote and let us review it..

DNR
-
He gives wisdom to the wise and knowledge to the discerning. He reveals deep and hidden things; he knows what lies in Darkness, and Light dwells with him.

User avatar
sternbildchen
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 421
Joined: 26 Apr 2006, 16:00
14
Location: Germany

Post by sternbildchen »

I had the same Problem with the Dos console.^^

Now i downloaded JCreator LE and it works much better for me :D

User avatar
isapiens
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 533
Joined: 05 May 2006, 16:00
14
Location: Turn around

Post by isapiens »

Well this is wierd. I am following the instructions on this web site <url>http://java.sun.com/docs/books/tutorial ... .html</url> they seem pretty straightforward but it still doesnt work. Here is what i do: set CLASSPATH= C:\...\bin after that i type java HelloWorld but it still gives me the error.

I assume the problem is in the set variable. I successully ran the program as an applet in HTML.

btw this HelloWorld programm is slightly different then the classical one. You see i am reading the "JAva for dummies", it pretty usefull for a beginner but i hate how they treat you as a moron. And they always have to be special, here is their HelloWorld code

<pre>
import java.applet.*;
import java.awt.*;

/*
* HelloWorld
* @version 0.1
* @author dkoosis@isc.com
*/

public class HelloWorld extends Applet {
Label helloLabel = new Label ("Yo, you lookin' at me?");

public void init() {
setBackground(Color.yellow);
add (helloLabel);
}
}

</pre>

Anyway if fixing this isnt worth it i can alwas get a java editor or smth...

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11566
Joined: 06 Apr 2005, 16:00
16
Location: The zone.
Contact:

Post by bad_brain »

allright, here´s the classic version:

Code: Select all

class Hello_World {
//you have to call the main method, except in applets
public static void main(String args[]) {
   System.out.println("Hello world!");
 }
}
copy and compile it, it will work....

the code you posted is an applet, and so only can run in appletviewer or in a HTML document. running it in command prompt can not work because it uses graphical elements (that´s why it imports the awt-package), so you can´t start it with java name, you have to use appletviewer name instead.

:wink:
Last edited by bad_brain on 14 May 2006, 14:27, edited 1 time in total.

User avatar
isapiens
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 533
Joined: 05 May 2006, 16:00
14
Location: Turn around

Post by isapiens »

Well that explains it... Stupid book, they used GUI... I should have guessed. Thanks for help. I think i am just gonna continue using HTMl to see if my programms work.

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11566
Joined: 06 Apr 2005, 16:00
16
Location: The zone.
Contact:

Post by bad_brain »

np.... :wink:
as long as you will code applets it´s fine to embed it in html....I find it pretty strange they begin with GUIs...normally the main-method is the first thing you learn because any command line-app needs it (at least you should know it from the start, what it means is already a little more advanced), followed by variables and loops...

....if you need help again simply post... :wink:

Post Reply