Hello everyone,

I just started learning Java, I like it so far because it is very similar to c#, which I am familar with. But I can't get programs to compile for me. It gives me an error saying 'Exception in thread "main" ' I am using NetBeans to compile the program.
Any suggestions would be great.
Thanks in advance.

A public class needs to have the same name as the java file it's contained in.
A class must also be public in order to be able to run as a program AND have a main method with the correct signature.

copy this following code sample


class Test
{
public static void main(String arg[])
{
System.out.println("Hello From the Java World");
}
}

//////// Save this file as Test.java
now start cmd prompt and ur current dir must be the bin of jdk
use this command
javac Test.java

run the application using
java Test

Note Java is case sensitive. in every terms.

Ok, I got it to compile. I had to upgrade netbeans. Once I did that, I compiled it and it ran fine.

Thanks for the help.

If you are new to java, netbeans will only hold you back. Try using a less advanced, lessdecorated, and less stiff IDE like JCreator or eclipse, so you have control over the language instead of the IDE. Just my opinion but it is what i was told when i first started programming and i'm sure many experienced programmers agree that the bells and whistles make learning java alot more complex than it needs to be.

If you are new to java, netbeans will only hold you back. Try using a less advanced, lessdecorated, and less stiff IDE like JCreator or eclipse, so you have control over the language instead of the IDE. Just my opinion but it is what i was told when i first started programming and i'm sure many experienced programmers agree that the bells and whistles make learning java alot more complex than it needs to be.

I agree, but a new java programmer should start out with the command line and text editor. I feel that is an important learning experience, altough in the beginning it really sucks.

Well, I was using sciTE editor, which is pretty nice to have. Java looks pretty much like c#, and I already know that pretty well. Netbeans really doesn't help me at all, it's auto-complete thing is crap (it takes forever to load). I tried to compile my program using the command prompt but I dont think I did it right, so it didn't work. Is there any special command prompt thing I need to do in order to use the command line compiler?
Thanks

You have to set your environment variables:

Considering you have windows:

Goto start--->right click on my computer--->select properties
Goto the advanced tab, and you should see a button at the bottom that says environment variables...click on that.

The only thing you need to worry about is the PATH variable, and the CLASSPATH variable, nothing else.

SKIP THIS STEP AND GO ON TO THE CLASSPATH, IF IT DOESN'T WORK AFTER SETTING THE CLASSPATH, THEN COME BACK AND SET THIS.
The path variable must point to the bin folder in the sun app server, and the bin folder in the jdk directory. Now, those are two different directories, so you seperate them with a semicolon when creating it. Here is what mine looks like:

C:\Sun\AppServer\bin;C:\Program Files\Java\jdk1.5.0\bin


Classpath....This points to the tools.jar file in the jdk directory. It's pretty easy to find. Here is what mine looks like:

.;%classpath%.;C\Program Files\Java\jdk1.5.0\lib\tools.jar

Your's will look similar, although you do not need the .;%classpath%.; statement.


Next, you need to know how to access your files at the command line.

To do so, you simply type cd/folderName at the command line. The cd stands for current directory. My folder was named javawork, so to access it, I did this at the command prompt:

cd/javawork // brings me to the right directory
javac Hello.java //compiles my program
java Hello //runs my program


Let me know if you don't undestand something or need more help.

Ok, setting the environment variables to the locations did the trick. Thanks for the help.

This article has been dead for over six months. Start a new discussion instead.