No Hello, World! : (

Discussion in 'Mac Programming' started by larswik, Sep 9, 2006.

  1. macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    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
     
  2. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #2
    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
     
  3. macrumors regular

    Joined:
    May 19, 2006
    #3
    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
     
  4. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #4
    I'll taake another look at the Syntax and my naming of the file and get back to you tomorrow.

    Thanks,

    -Lars
     
  5. macrumors regular

    Joined:
    Jan 28, 2006
    #5
    You should be able to run it from within NetBeans from the menu and the output should print to the output window in NetBeans.
     
  6. macrumors regular

    Joined:
    Apr 6, 2004
    Location:
    canada
    #6
    println for print line. print in doesn't make a lot of sense.
     
  7. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #7
    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
     
  8. macrumors member

    Joined:
    Jun 18, 2005
    #8
    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, and Dr Java. 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.
     
  9. macrumors newbie

    frankie

    Joined:
    Jul 19, 2002
    #9
    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.
     
  10. macrumors 65816

    Joined:
    Apr 10, 2003
    Location:
    mi
    #10
    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:
     
  11. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #11
    TextPad is a Windows-only editor. The advice may not be faulty but that certain choice isn't helpful.
     
  12. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #12
    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.
     
  13. macrumors 65816

    Joined:
    Apr 10, 2003
    Location:
    mi
    #13
    Oh, I agree, just thought calling him a troll for it was a little... out of hand.
     
  14. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #14
    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
     
  15. macrumors 6502

    ggibson913

    Joined:
    Sep 11, 2006
    #15
    BBEdit

    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. :)
     
  16. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #16
    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.
     
  17. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #17
    Yes, TextWrangler is a good choice.

    Edit in GUI, compile and run within the Terminal application.
     
  18. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #18
    Cool, can I just use Text Edit located in the Applications folder on the mac. since I will use Terminal to compile?

    -Lars
     
  19. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #19
    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.
     
  20. macrumors regular

    Joined:
    Feb 14, 2006
    #20
    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.
     
  21. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #21
    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
     
  22. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #22
    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
     
  23. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #23
    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
     
  24. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #24
    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.
     
  25. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #25
    Why didn't you just copy the text from your first post and indent it?

    Good luck with future projects. :)
     

Share This Page