Planetarion Forums

Planetarion Forums (https://pirate.planetarion.com/index.php)
-   Programming and Discussion (https://pirate.planetarion.com/forumdisplay.php?f=57)
-   -   Wierd Java Error (https://pirate.planetarion.com/showthread.php?t=192567)

Androme 4 Oct 2006 23:41

Wierd Java Error
 
I've just installed Sun's Java JDK 1.5_08 and I've set PATH C:path 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


Phil^ 4 Oct 2006 23:48

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

Raging.Retard 5 Oct 2006 19:51

Re: Wierd Java Error
 
Add current directory (ie "." ) to your CLASSPATH environment variable.

Androme 20 Oct 2006 09:42

Re: Wierd Java Error
 
thanks :)


All times are GMT +1. The time now is 11:54.

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