943,748 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2885
  • Java RSS
Jun 7th, 2005
0

New to Java, can't get program to compile

Expand Post »
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.
Similar Threads
Reputation Points: 23
Solved Threads: 6
Posting Pro
Dark_Omen is offline Offline
573 posts
since Apr 2004
Jun 8th, 2005
0

Re: New to Java, can't get program to compile

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jun 8th, 2005
0

Re: New to Java, can't get program to compile

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.
Reputation Points: 11
Solved Threads: 0
Newbie Poster
adityabakle is offline Offline
8 posts
since Aug 2004
Jun 8th, 2005
0

Re: New to Java, can't get program to compile

Quote originally posted by adityabakle ...

Note Java is case sensitive. in every terms.
What about hexidecimal literals?
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jun 8th, 2005
0

Re: New to Java, can't get program to compile

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.
Reputation Points: 23
Solved Threads: 6
Posting Pro
Dark_Omen is offline Offline
573 posts
since Apr 2004
Jun 8th, 2005
0

Re: New to Java, can't get program to compile

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
twocentwhoracle is offline Offline
3 posts
since Jun 2005
Jun 8th, 2005
0

Re: New to Java, can't get program to compile

Quote originally posted by twocentwhoracle ...
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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jun 8th, 2005
0

Re: New to Java, can't get program to compile

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
Reputation Points: 23
Solved Threads: 6
Posting Pro
Dark_Omen is offline Offline
573 posts
since Apr 2004
Jun 8th, 2005
0

Re: New to Java, can't get program to compile

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jun 12th, 2005
0

Re: New to Java, can't get program to compile

Ok, setting the environment variables to the locations did the trick. Thanks for the help.
Reputation Points: 23
Solved Threads: 6
Posting Pro
Dark_Omen is offline Offline
573 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: help with calednars and photo gallerys
Next Thread in Java Forum Timeline: A Java Question??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC