Discussion in 'Mac Programming' started by TeddyCarey, Apr 6, 2006.

  1. macrumors newbie

    Apr 6, 2006
    Ok, this is going to sound pretty stupid:

    I have begun to teach myself Java. I have a textbook that is great, except for one thing: It is primarily oriented for PCs. I have realized that Java is cross platform compatable, but I am having trouble figuring out how to 'run' programs. Basically my question is this: If I've created a program in Java (using jedit) and I can't figure out how to simply run the program. If I could just get a little bit of guidance on this, that would be great :)

    EDIT: Ok thanks, I figured it out I think :)
  2. macrumors 68000

    Jun 13, 2004
    If you are still doing basic java, then I would suggest using the command line to run your programs. Basically, fire up your terminal, navigate to your source java file and compile it first by typing "javac <>". If it compiled without any error, then do a "java <filename>" which will run your program.
  3. thread starter macrumors newbie

    Apr 6, 2006
    ok, now for some reason I am getting an error each time that is the same, no matter which program I try to run. For example, I tried to run a simple program that utilizes the Quadratic Formula to calculate the roots of an equation. (At the end of this post) I go to terminal, type javac (in the correct location) and it brings me to a new line [indicating that there are no errors?] Then when I just type java, I get "Exception in thread "main" java.lang.NoClassDefFoundError: QuadraticFormula/java"

    Heres the program I was trying to run, not sure if it really matters though:
    public class QuadraticFormula

    public static void main (String[] args)
    int a,b,c; //ax^2 + bx + c

    //Input your coefficients here.

    a = 1; //Value of a
    b = 2; //Value of b
    c=3; //Value of c

    //Using the quadratic formula to compute the roots

    double discriminant = Math.pow(b,2) - (4 * a * c);
    double root1 = ((-1 * b) + Math.sqrt(discriminant)) / (2 * a);
    double root2 = ((-1 * b) - Math.sqrt(discriminant)) / (2*a);

    System.out.println ("Root #1: " + root1);
    System.out.println ("Root #2: " + root2);

  4. macrumors G4


    Nov 3, 2005
    Ah, you just need to type 'java QuadraticFormula' to run your Application.

    You might also find XCode from Apple (membership required) or Eclipse are useful to program JAVA in.

    If javac takes you to a new line it means the program has compiled with no errors, but it is still possible to make the program crash ;)
  5. thread starter macrumors newbie

    Apr 6, 2006
    Ah thanks. now I have to figure out text input and a few others...i will probably be back here soon =P

    I am starting the download of XCode, but it is slow, and usually it doesn't get very far (I have tried downloading it a few times already) Hopefully tonight I can download it overnight :) I'll also try Eclipse. Thanks for your help, both of you.
  6. macrumors 68000


    Jun 6, 2003
    District of Columbia
    I suggest you stick with compiling by hand for now. It's much better for small projects and if you understand what an IDE is doing behind the scenes it will increase your ability to use one.

    Edit in jedit or whatever applications can provide syntax highlighting and/or auto-completion.
  7. thread starter macrumors newbie

    Apr 6, 2006
    Ok, what do you suggest then other than jedit. Its kind of tedious to use. =/ I'm also still trying to figure out text input....
  8. macrumors 6502

    May 17, 2002
    Denver, CO
    My text editor of choice is TextMate but it's a commerical app. If you want a decent free edit check out Text Wrangler.

    Also, I'd echo the other posters suggestion of staying away from an IDE while you're still learning any language. I love using Eclipse, but I wrote Java apps for about 5 years using SlickEdit before switching to Eclipse and while the learning curve was much steeper I learned the language, not a tool.
  9. Moderator emeritus

    Jun 25, 2002
    Gone but not forgotten.
    I use jGrasp and it handles everything, including project management and creating the .jar file, plus it handles Objective-C, HTML, etc.
  10. thread starter macrumors newbie

    Apr 6, 2006
    Thanks again to all of you for your help...I will almost certainly be back :)
  11. macrumors 68000

    Feb 23, 2006
    I prefer Xcode myself.

    Here's a good document from Sun to help get your started with Xcode for programming Java:

    Frankly, I wish I had Xcode when I was learning Java in my AP Computer Science class, it is much faster than typing into a DOS prompt all the commands to compile the program and run it.

Share This Page