Run simple java programs

Discussion in 'Mac Programming' started by steveca, Mar 24, 2007.

  1. macrumors 6502

    Joined:
    Mar 9, 2005
    Location:
    Malta
    #1
    Hello everyone...

    I am very new to Java and programming in general. At university we use Windows and write Java code in a program called TextPad, that also caters for compiling and running the program. We still write very very basic code. The files, saved as *.java can be read from Xcode and it gives you the code.

    Now... the problem is that i am not able to run these small programs. In TextPad we compile and then run but in :apple: XCode almost all the options under build etc are greyed out.

    Please help... thanks :)
     
  2. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
  3. macrumors 68020

    Osarkon

    Joined:
    Aug 30, 2006
    Location:
    Wales
    #3
    You could always do it through the terminal.

    1. navigate to the folder where your java file is via the 'cd' command.
    2. type in ' javac *.java' - this will compile all .java files in that directory.
    3. type in 'java' then the name of the class that contains the Main.
     
  4. thread starter macrumors 6502

    Joined:
    Mar 9, 2005
    Location:
    Malta
    #4
    thanks for the help guys... it works from the terminal like wonder :)
     
  5. macrumors 6502

    Joined:
    Jul 16, 2004
    Location:
    Milwaukee, WI
    #5
    hate to hijack this thread but....
    Is there anyway you could do this from within XCode?
    Either by making a full project or from just within the java editor?

    It can be very frustrating to continously switch back to Terminal.
     
  6. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #6
    Simplest way is to do what toddburch suggest, ie create a new Java tool project in Xcode and call it whatever your main class is called. Then you can hit the run button and Xcode will compile and run it. The alternative is to reuse the same project but use a manifest file that specifies which is the main class.

    b e n
     
  7. macrumors 68020

    Osarkon

    Joined:
    Aug 30, 2006
    Location:
    Wales
    #7
    You could install Eclipse.
    http://www.eclipse.org/

    It's cross-platform, free java development software, and you can run your programs from inside it. It also compiles on-the-fly so you know straight away if something is wrong and it'll give a good indication of what it is.

    The interface can be daunting and takes getting used to, but once you get used to it, it does prove invaluable.
     
  8. macrumors 6502

    Joined:
    May 17, 2002
    Location:
    Denver, CO
    #8
    Eclipse, along with IntelliJ, Netbeans, etc., should not be used by someone who is interested in learning Java or else they run the risk of learning the tool, not the language. Assuming the original poster is interested in learning Java, they really should stick to using the command line as much as possible and a simple editor such TextMate, TextWrangler, JEdit, etc.
     
  9. macrumors 68020

    Osarkon

    Joined:
    Aug 30, 2006
    Location:
    Wales
    #9
    Well in it's base form Eclipse does pretty much that. The fact you can install plug-ins to make things easier doesn't deter from the fact it's an extremely useful editor.

    However I do see your point, I've only recently started using it, was using BlueJ beforehand when I started learning.
     
  10. macrumors 6502

    Joined:
    May 17, 2002
    Location:
    Denver, CO
    #10
    As someone who has been using Eclipse for close to 5 years I know how useful it can be. :)

    Code completion/refactoring, dynamic class (re)loading, remote debugging, in-line documentation are all extremely useful features. But, I've seen far too many junior (and higher!) developers and students unable to write code if they don't have access to their favorite crutch of an IDE.


    And I see you point, but Eclipse is far more than an editor. As I said above, if the original poster really wants to learn Java and not a tool they should really avoid using a full blown IDE.
     
  11. macrumors member

    cruzrojas

    Joined:
    Mar 26, 2007
    Location:
    USA
    #11
    I would say try using Emacs with Syntax Highlighting and a Terminal Window, once you feel comfortable in Emacs you will find yourself coding faster.
     
  12. macrumors newbie

    Joined:
    Mar 24, 2007
    #12
    I defenitly would use Eclipse to edit Java code
     
  13. macrumors newbie

    Joined:
    Apr 11, 2007
    #13
    I am also a student learning Java. Eclipse is NOT the answer. I've installed it and it's WAY over my head. I cannot easily figure out how to open something, and once I did I have no idea how to run it. I can't figure out how to do anything with XCode, either, because every option in the menu seems to be mysteriously grayed out. My tutor recommended DrJava, which is exactly the sort of simplicity I need--but it won't run 1.5, and is therefore pretty useless to me. :(

    Now, I have an assignment due tomorrow, and I have no way of compiling it or running it, let alone printing out the results for my professor. I don't want to learn how to use a program; I have enough to learn already.

    Does anyone have a real answer for this?
     
  14. macrumors newbie

    Joined:
    Mar 24, 2007
    #14
    @LisaQ: Well ... when you learn a programming language it comes to a point
    where you HAVE TO learn to use some tools too. An IDE is one of those basic
    tools. Maybe you should buy an Eclipse Book or look for some Tutorials in the
    internet (Google)...
     
  15. macrumors newbie

    Joined:
    Apr 10, 2007
    #15
    Vi

    I guess this shows how far back I go, but I actually, would recommend you look at vi. The best thing is should already be on your machine, just type vi. You'll want to google up some websites for the basics.

    I currently use Eclipse, and it is a great tool, especially for Java. For writing complex java apps, its the best. In my opinion an integrated IDE (including XCode) is not really good for beginning programmers.

    For one thing, it teaches programmers to be lazy, which is REALLY bad in the beginning stages of learning the language. I didn't even notice how lazy I got, until I had to do some coding outside of Eclipse. Using a manual text editor teaches respect for simple stupid mistakes like the missing semi-colon, missing paren, double quote, etc. While an IDE will make sure you never make this mistake, who knows when you have to fix someone else's code which is in some other IDE, or no IDE at all. Who knows if you have to do it over a terminal, meaning no IDE at all.

    Since you're fairly fresh, I would say look into VI. Its a bit higher learning curve, than TextPad, or something of that sort, but its totally worth it. Its actually a good idea to learn it at this stage, since the code is easy, you can put more effort into learning VI. Once the code gets complicated, you won't want to be futzing around trying to learn VI.

    When you get good at it, you'll be able to code much faster using the more sophisticated features, and of course the UNIX geeks would approve.
     
  16. macrumors regular

    kiang

    Joined:
    Apr 8, 2007
    #16
    I learned java using BlueJ, Eclipse and NetBeans, and I AM capable of creating a full java program, only using a text-editor.
    so: just download netbeans...
     

Share This Page