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:
|
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 |
Re: Wierd Java Error
Add current directory (ie "." ) to your CLASSPATH environment variable.
|
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