PDA

View Full Version : No Hello, World! : (




larswik
Sep 9, 2006, 10:58 PM
I have spent the last couple of hours wondering why it isn't working. I'm working along with Java for dumbies and came across the first code to have it spit out "Hello World."

Using NetBeans that was recomended I typed in this right out of the book...

public class HelloApp
{
public static void main (String[] args)
{
System.out.println("Hello, World!");
}
}


The book then said to save and open up a command line program and do the CD to find the directory and the JAVAC command to run the app but I get an error message.

I followed the book and looked over what was supposed to be in Cap and small case. O' and it is PRINTLN even though it looks like an eye.

Is there a differance between the Book which he wrote on a Windows machine Vs. my Mac?

-Lars



savar
Sep 9, 2006, 11:05 PM
The book then said to save and open up a command line program and do the CD to find the directory and the JAVAC command to run the app but I get an error message.

I followed the book and looked over what was supposed to be in Cap and small case. O' and it is PRINTLN even though it looks like an eye.

Is there a differance between the Book which he wrote on a Windows machine Vs. my Mac?

Why don't you cut and paste exactly what you typed and exactly what error you got? That would help.

Your code looks fine to me. When you cd to the directory that contains it, you should be typing

javac HelloApp.java

Then once that is done, you type

java HelloApp

If this doesn't work, post exactly what you're doing and what error you're getting.

By the way, congrats for figuring out that its an L in println not an I. I fumbled with that for several hours the first time I tried running a program from a book. Haha, I've come quite a ways since then.

Good luck, cheers

nutman
Sep 9, 2006, 11:37 PM
i think the most common mistake at the beginning is not naming your file with the same name as your: public class _________.

other than that, its all about syntax and CaSEsensitVITY

larswik
Sep 10, 2006, 12:13 AM
I'll taake another look at the Syntax and my naming of the file and get back to you tomorrow.

Thanks,

-Lars

atmenterprises
Sep 10, 2006, 12:22 AM
You should be able to run it from within NetBeans from the menu and the output should print to the output window in NetBeans.

cait-sith
Sep 10, 2006, 01:17 PM
println for print line. print in doesn't make a lot of sense.

larswik
Sep 11, 2006, 12:54 AM
Yes I got the Print Line. When I copy / pasted the L looked like an I.

I think the problem is in Netbean. I got a little creative tonight with the ' Hello World' and changed the name due to some frustration.

Here is the code

public class biteme
{
public static void main(String[] args)
{
System.out.println("Bite ME!");
}
}

Now everything is fine but Netbean is putting a little red 'x' next to the first line which is 'public class biteme' when I hover the mouse over it a box pops up saying "class biteme is public, should be declaired in a file name biteme.Java"

I created a new project and called it 'biteme'. When I typed it in Netbean filled in the 3rd text box with the same name except it put the '.Main' after it. so I typed 'biteme' and it called the file 'biteme.Main'

So,

In the projects window I spin down the coffee looking icon that says 'Biteme' the spindown the other one Source Packages/biteme/Main.java/biteme

When I change the 'public class biteme' to 'public class Main' the little red box goes away and it complies fine and runs. But it is not called Main, I called it biteme. Since the error box said it was looking for something ending with .Java and I saw the file in the tree called Main.Java I filled in biteme with Main and it worked.

Why didn't Netbean name my file what I names it? biteme.java? instead it created the item in the tree above called 'biteme' and a file called Main.Java

I would like to understand this before I go on with my lessons. I hope I didn't updet anyone with the Biteme instead of Hello World. I was just frustrated.

Thanks!

-Lars

jamie329
Sep 11, 2006, 07:35 AM
Just a suggestion, stop using NetBeans. IDE's (Integrated or Interactive Development Environments) such as NetBeans and Eclipse are great for people who are already completely familiar with the language. However, if you are just starting out, they are going to confuse you more than help you. If you are just starting out with Java, stick to learning the language, not the development software.

A couple great editors for Java which I would recommend are called TextPad (http://www.textpad.com/products/textpad/index.html), and Dr Java (http://drjava.sourceforge.net/). Both of these are very easy to use, and they both have built in functionality for running the Java compiler. Alternatively you could just use Notepad to type in your programs and then use the command line to compile them, but I guarantee the using one of those programs I mentioned will be much simpler. Once you have mastered the language and need a more robust development environment, then you can try learning NetBeans or Eclipse.

frankie
Sep 11, 2006, 09:12 AM
A couple great editors for Java which I would recommend are called TextPad (http://www.textpad.com/products/textpad/index.html), and Dr Java (http://drjava.sourceforge.net/). Both of these are
A guy using a Mac posts a question on MacRumors, and your reply is TextPad. Ha ha funny. Now go take your trolling somewhere else.

mkaake
Sep 11, 2006, 10:49 AM
A guy using a Mac posts a question on MacRumors, and your reply is TextPad. Ha ha funny. Now go take your trolling somewhere else.

trolling?

he posted a very good answer to the OP's question, and gave him some very good advice from a person who sounds like he's been doing this for a while...

who's trolling? :rolleyes:

bousozoku
Sep 11, 2006, 11:40 AM
trolling?

he posted a very good answer to the OP's question, and gave him some very good advice from a person who sounds like he's been doing this for a while...

who's trolling? :rolleyes:

TextPad is a Windows-only editor. The advice may not be faulty but that certain choice isn't helpful.

savar
Sep 11, 2006, 12:34 PM
<snip lots of IDE agony>
Thanks!

-Lars

This is exactly why I don't suggest newbies use an IDE. You don't know how the IDE compiles and runs programs, so you dont know how to figure out what's going wrong.

Start over. You can write the source code in an IDE, but compile it at the command line as described in my first post.

Edit: furthermore, once you do get to a point where you want an IDE, don't use NetBeans. That thing is a slow piece of junk. Eclipse is much better but requires recent hardware to perform at acceptable levels, IMO.

mkaake
Sep 11, 2006, 12:42 PM
TextPad is a Windows-only editor. The advice may not be faulty but that certain choice isn't helpful.

Oh, I agree, just thought calling him a troll for it was a little... out of hand.

larswik
Sep 11, 2006, 12:53 PM
Ok, so what I got from those replys is that I should stop using Netbeans for now and start using a simpler program. I did want to start with something simpler but could not find anything like Textpad which was PC only.

Can I use 'Textedit' on the Mac platform just to write the code and use the 'Terminal' to complie it for just the basics? It sounds like I could write it in anything.

You guys are right. I do want to start simple with Java and ease my self into a more complicated IDE. So if I use Texteditor and compile my 'hello world' with terminal then I would have had sucess where Netmeans gave me a problem, is that right?

Thanks,

-Lars

ggibson913
Sep 11, 2006, 01:36 PM
Hey,

I agree that NetBeans is very confusing a simple text editor will do just fine. I recommend BBedit at www.barebones.com . They also offer another text editor called Text Wrangler (I think that is the name). Both are made for the Mac and you can use them for Java and HTML formatting. It does have a cost but you can trial it for 30 days. Hope that helps you out. :)

HiRez
Sep 11, 2006, 01:42 PM
I recommend BBedit at www.barebones.com . They also offer another text editor called Text Wrangler (I think that is the name). Both are made for the Mac and you can use them for Java and HTML formatting. It does have a cost but you can trial it for 30 days. Hope that helps you out. :)They are both based on the same code so they are very similar. TextWrangler has most of the features of BBEdit (including Java syntax highlighting) and it's free, I highly recommend trying that first.

bousozoku
Sep 11, 2006, 02:17 PM
They are both based on the same code so they are very similar. TextWrangler has most of the features of BBEdit (including Java syntax highlighting) and it's free, I highly recommend trying that first.

Yes, TextWrangler is a good choice.

Edit in GUI, compile and run within the Terminal application.

larswik
Sep 11, 2006, 02:19 PM
Cool, can I just use Text Edit located in the Applications folder on the mac. since I will use Terminal to compile?

-Lars

HiRez
Sep 11, 2006, 05:06 PM
Cool, can I just use Text Edit located in the Applications folder on the mac. since I will use Terminal to compile?Yep, you certainly can. But I'd still recommend TextWrangler since it has a lot more features you might really use and will automatically syntax-highlight your code, yet it's still a simple text editor at its core so you won't have to deal with a lot of the funkyness a full-blown IDE app can have. The files are still text files, just like TextEdit. Oh, if you do use TextEdit though, make sure you set the type to Plain Text as opposed to Rich Text, that will mess up the compiler.

mbabauer
Sep 11, 2006, 09:43 PM
Ok, so what I got from those replys is that I should stop using Netbeans for now and start using a simpler program.

Try jEdit. Its free, and written in Java, so it should work on the Mac. It also does syntax highlighting for not only Java but MANY other languages, and has a TON of plugins that do all sorts of magical things. I personally use Eclipse, and find it to be better than Netbeans. Eclipse can do more than just Java (as I am sure Netbeans can), is OpenSource, and has tons of plugins.

I agree with others somewhat...stay away from the IDEs if you are a programming newb. If you are a developer, say in C/C++ or Perl and are rather perficiant in generic development things (IE you know what Linked Lists are, know how to do a bubble sort, can write your own HashMap, etc), then maybe you could jump into an IDE since most languages are pretty much the same anyways. Saying a seasoned programmer can't learn a new language with an IDE is just plain stupid. Thats like saying someone new to Cocoa and Objective-C should start with TextEdit and gcc...thats just nutz. Obviously, you know your skill levels here and can make that determination on your own.

Other than that, here are the biggest mistakes I see newbies make:
1) The file name MUST match the class name EXACTLY. It is case sensitive
2) Compiling out of their classpath. If you don't know what I mean, you will
3) Curly braces dont match up. This can easily be solved using good, standard code formating practices and clean style
4) Missing the ';' on the end of 1 or more lines

Focus on formatting and style. I find that everyone I tought did WAY better when I enforced a manditory style, because it helped them catch stupid mistakes like missing/extra curlies and such.

larswik
Sep 11, 2006, 10:16 PM
Again thanks for the help. I would have never imagened that there are so many programing Apps for writing Java. I'm wondering if it will be harder to just find one App then it is to write the program, haha. I'll take a peek at jEdit.

The other text editors look good but I don;t want to spend laot of cash right now learning it. incase I get frustraded and stop I would rather my only loss be the book I bought. Plus I could use almost anything to write it.

Thanks peoples...

-Lars

larswik
Sep 11, 2006, 10:56 PM
Ok I downloaded jEdit and it seems like a simple free text type program.

I started over with the Hello World and typed this into jEdit

public class HelloApp
{
public static void main(String[] args)
{
system.out.println("Hello World!");
}
}


I then saved the file from jEdit as 'HelloApp.java'

Opend up the Terminal and cd my way to the directory and tyoed in

javac HelloApp.java and here is what the terminal spit back at me.

larsg5s-power-mac-g5:~ larsg5$ javac HelloApp.java
HelloApp.java:5: package system does not exist
system.out.println("Hello World!");
^
1 error
larsg5s-power-mac-g5:~ larsg5$


I looked the code over for Caps and small case and everything looks fine. What gives? This seems so easy and yet it's failing?

-Lars

larswik
Sep 11, 2006, 11:11 PM
Opps, there was a typo system shold have been System with a capitol S.

Also I guess I don't type in 'javac HelloApp.java' which I did. I just type 'java HelloApp' and it worked.

or do I have to first type in- javac HelloApp.java and then later type in java HelloApp

Sorry for all the posts : )

-Lars

savar
Sep 11, 2006, 11:32 PM
Opps, there was a typo system shold have been System with a capitol S.

Also I guess I don't type in 'javac HelloApp.java' which I did. I just type 'java HelloApp' and it worked.

or do I have to first type in- javac HelloApp.java and then later type in java HelloApp

Sorry for all the posts : )

-Lars

Lars, congrats! Looks like you got it compiled and running.

javac = java compiler. this program turns java source code into java bytecode. in your case, it creates a file called HelloApp.class

java = java launcher. this program sets up a java environment and then invokes the class which you specify at the command line.

You got it right. Pass the name of the file to javac, and the name of the class to java.

javac HelloApp.java

java HelloApp

Notice that you only need to run the compiler when you change the source code. Once you've compiled the code you can run it over and over.

bousozoku
Sep 11, 2006, 11:57 PM
Opps, there was a typo system shold have been System with a capitol S.

Also I guess I don't type in 'javac HelloApp.java' which I did. I just type 'java HelloApp' and it worked.

or do I have to first type in- javac HelloApp.java and then later type in java HelloApp

Sorry for all the posts : )

-Lars

Why didn't you just copy the text from your first post and indent it?

Good luck with future projects. :)

jamie329
Sep 12, 2006, 12:31 AM
A guy using a Mac posts a question on MacRumors, and your reply is TextPad. Ha ha funny. Now go take your trolling somewhere else.

You're right frankie. I did refer to Windows software in my post. It's pretty easy to get the two confused when you have to work with Windows all day. But instead of correcting my post (which was only incorrect when it came to TextPad. DrJava is available for the Mac), you chose to call me a troll. This just goes to show who the real troll is. You. So why don't you go run along back to your highschool programming class where you are the "l33t35t" kid around. By frankie, and have a nice day!

larswik
Sep 12, 2006, 01:33 AM
Thanks!!! after I got that down the world finally said hello to me! I understand that I need to create the class first and then run it with java.

It's amazing anything ever gets written with everything being so case sensitive.

As for why I didnít copy and paste the above post I wanted to write it to get use to it and understand it more.

The book is now starting on Objects.

Thanks again for all your help everyone!!!

-Lars

bousozoku
Sep 12, 2006, 01:46 AM
...
It's amazing anything ever gets written with everything being so case sensitive.
...

In the old days, when I started, most languages weren't case sensitive. Punched paper cards were used before I started, and the keyboards had no lower case characters. I entered all my source code through CRT terminals using single line editors, which were only slightly better than punched cards. It was really nice when we finally had intelligent editors that would check basic syntax.

I'm thankful for case sensitivity but I've found that a lot of people type the same variable name different ways and get errors that a certain variable doesn't exist. Intelligent editors help there too, since many keep a list of variables, check your parentheses, and other everyday things that go wrong.

I rarely use a plain text editor for anything these days but there are times when you just have to turn it off in order to learn.

larswik
Sep 12, 2006, 12:02 PM
Punched paper cards, that was a while ago.

Thanks for the insight.

-Lars

jeremy.king
Sep 12, 2006, 02:59 PM
It's amazing anything ever gets written with everything being so case sensitive.


Not to worry my fellow Java friend. Once you get the hang of the language and understand whats going on underneath the covers, move on to an IDE (several suggested above) and you will never have to worry again since a) code completion will take care of case for you and b) IDEs will display compile errors before you even build so you will have visual clues to where you have syntax problems.

Understand - I do not advocate IDEs for beginners - they just get in the way and prevent a total understanding of a programming language.

Edit: I think I just said the same thing as bousozoku

larswik
Sep 12, 2006, 05:15 PM
I agree. I wanted something simple and it looks like I got it with jEdit. and now that I understand that I need to run everything in the Terminal starting with the JAVAC command to first create the CLASS file and then run the JAVA command that helped alot.

Getting my head around the fact that a CLASS contains the OBJECTS, or how was it I read it, the CLASS is the blue print for creating the OBJECT and so on. understanding that will more then likely be the hardest.

I wish it were more like lingo ' if i = 1 then goto frame 10 else frame 1' or something like that. Where objects are called sprites and I can see them or hear them 'Onmouse Down play sprite 14'

But you have to start some place.

thanks.

-Lars

atmenterprises
Sep 12, 2006, 06:21 PM
A class is a basic blueprint - correct.

Think about it this way (and, believe me, I had the hardest time grasping this concept):

An automobile is a class. It has tires, a steering wheel and a horn. Those are all properties (or variables) of the class. It can go forward and reverse. Those are behaviors (or methods) of the class.

Now a Mustang is an instance of the class. It inherits all of the properties and behaviors of the parent class (the automobile), but has unique characteristics not found in the parent class. Like, for instance, a red paint job. Or a suped-up engine. Or a rear spoiler. Those are all properties. Let's say the car can fly instead of just go forward or reverse. That's a new method the Mustang class that the parent (automobile) class doesn't.

larswik
Sep 12, 2006, 06:59 PM
ya, that is kind of what i am reading about now. a Mustang I can see and touch. I guess what I am expecting is that an bobkect in the Java language is something that I can also see. Like a blank window or an icon. something that I can see. But I guess objects can also be things that I can't see that still do things.

I need to wrap my head around that one.

Thanks for the support!

-Lars