User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion

Reply
Thread Tools Display Modes
Unread 4 Oct 2006, 23:41   #1
Androme
☆ ♥ 
 
Androme's Avatar
 
Join Date: Jan 2003
Posts: 3,489
Androme can only hope to improve
Wierd Java Error

I've just installed Sun's Java JDK 1.5_08 and I've set PATH Cath to java\bin under Environment Variables.

Compiling a java program I've written works fine as the .class file appears. But when it comes to running it via e.g. "java hello" after I've compiled via "javac hello.java" it doesn't seem to run and instead, I get this error:

C:\ComSci\DCS100>javac hello.java
C:\ComSci\DCS100>java hello
Exception in thread "main" java.lang.NoClassDefFoundError: hello

Code:
	
/* ***************************************
   AUTHOR: Paul Curzon
	A program that prints a welcome message
	Notice how the program called hello is 
	made of 2 methods. One called main
	and one called helloMessage.
   ****************************************/

class hello
{
    public static void main (String[] param)
    {
        // We want to print out the message defined in method helloMessage
		
		helloMessage();
		System.exit(0);
		
    } // END main


/* ***************************************************
	   Define some commands of our own to use above
   *************************************************** */
	

	
	/* ***************************************
	*
	*	Define a method to print a welcome message
	*/
	
    public static void helloMessage ()
    {
 
        System.out.println("Hello Mark");
		
    } // END helloMessage
	
	
	
} // END class hello
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
Androme is offline   Reply With Quote
Unread 4 Oct 2006, 23:48   #2
Phil^
Insomniac
 
Phil^'s Avatar
 
Join Date: May 2003
Posts: 3,583
Phil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus wouldPhil^ spreads love and joy to the forum in the same way Jesus would
Re: Wierd Java Error

public class hello, not class hello - that *should* make it compile and execute fine if its being picky about that - although when i tested your code it worked straight off for me without modification so im not sure why you are getting that error. Best guess is that theres something youve overlooked as me copypasting that code into notepad, saving as hello.java in c:\ and doing javac hello.java , java hello worked straight off

im using
C:\>java -version
java version "1.6.0-beta2"
Java(TM) SE Runtime Environment (build 1.6.0-beta2-b86)
Java HotSpot(TM) Client VM (build 1.6.0-beta2-b86, mixed mode, sharing)
btw. Shouldnt make a difference but still...

Note that the filename (hello.java) has to be the same to the class name ( hello ) , minus the .java bit when you make the class public. Its also case sensitive iirc so be careful that you didnt name the file Hello.java
Youve done it here but its something to bear in mind when you do other programs
public class MyWonderfulClass -> MyWonderfulClass.java
public class MyOtherThing -> MyOtherThing.java
and so on
you can only have one public class in a java file, but if you want more then one class there you can make them private by specifying them as private.

furthermore, main is a static method so you need to create an instance of that class before you use any (nonstatic)functions or variables in it - granted you can get around that by making hellomessage static too but thats going to cause you problems later on when you want to do things with inner variables.

there isnt any need for System.exit(0) since the program terminates itself after running through all the code anyway
its a good idea to get into the habit of making classes start with capital letters, and variables/method names start with lowercase - though its not necessary for it to work
__________________
Phil^

Last edited by Phil^; 5 Oct 2006 at 00:10.
Phil^ is offline   Reply With Quote
Unread 5 Oct 2006, 19:51   #3
Raging.Retard
Street Tramp
 
Raging.Retard's Avatar
 
Join Date: Apr 2000
Location: Street Gutter
Posts: 341
Raging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant futureRaging.Retard has a brilliant future
Re: Wierd Java Error

Add current directory (ie "." ) to your CLASSPATH environment variable.
__________________
Chimney Pots.
Raging.Retard is offline   Reply With Quote
Unread 20 Oct 2006, 09:42   #4
Androme
☆ ♥ 
 
Androme's Avatar
 
Join Date: Jan 2003
Posts: 3,489
Androme can only hope to improve
Re: Wierd Java Error

thanks
__________________
R3: LegioN (came #32) || R4: BlueTuba
R5: WolfPack Order || R6: Wolfpack
R7: Fury
----------retired-------
R52-R55: Apprime
R56-R57: FaceLess
R58-60: Apprime/Ultores
Androme is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Forum Jump


All times are GMT +1. The time now is 19:37.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018