Problems with Java

Discussion in 'Mac Programming' started by loserDB, Oct 11, 2006.

  1. loserDB macrumors newbie

    Oct 11, 2006
    I am running OSX 10.4.8

    I have just downloaded J2SE 5.0 Release 9 and installed it, and I have also installed Netbeans,

    I have a program with the following code..

    import java.util.Scanner;

    /* First program for Programming2
    ** Revisiting basic variables and
    ** reading input from the keyboard

    public class First
    public static void main (String args[])
    int number1;
    int number2;
    double number3;
    double result;
    String name;

    Scanner scan = new Scanner(;

    //ask user's name
    System.out.print("Who are you? ");
    name =;

    System.out.println("Give me an int ");
    number1 = scan.nextInt();

    System.out.println("Give me another int ");
    number2 = scan.nextInt();

    System.out.println("Give me a double ");
    number3 = scan.nextDouble();

    System.out.println("Hello "+name);

    result = number1 + number2 + number3;
    System.out.println("three numbers added is "+result);

    result = number1 * number2 * number3;
    System.out.println("three numbers multiplied is "+result);

    result = number3 - number2;
    System.out.println("third number minus the second number is "+result);

    result = number3 / number2 ;
    System.out.println("third number divided by second number is "+result);

    The output is the following...

    Why does java not work on here?

    Here is the contents of the console...

    Exception in thread "main" java.lang.NoClassDefFoundError: First (wrong name: week1/First)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(
    at Method)
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    at java.lang.ClassLoader.loadClassInternal(​

    Any help would be appreciated.
  2. Eraserhead macrumors G4


    Nov 3, 2005
    I got the code to work fine on my Macbook, you are clearly making a mistake in the compilation/running.

    The error itself says it cannot find the class. For a single file and a beginner you don't want to be worrying about .jar's yet, just .class files.

    If you save your code in a text editor (such as TextEdit, or XCode which has syntax highlighting) and save the first as '' (as the class is called First the file MUST be '' and it is case sensitive.

    Then load up terminal (in /Applications/Utilities/) and navigate to the directory you have put the file (if you don't yet know how save the file in your home directory.)

    then type 'javac' and press enter.
    Once that has finish and come up with the input line (it may take a while)
    then type 'java First' and press enter

    Your program should run. You shouldn't type any single quotes mentioned around commands in the above text, they are used to clarify what you are typing.

    PS Can you use ['CODE'] and ['/CODE'] around your code next time as it makes it easier to read (without the single quotes.) :)

    PPS I probably wouldn't use Netbeans just yet, a text editor with syntax highlighting is the most useful, I used Xcode but other programs are probably better. Netbeans is an advanced programs designed for doing much more complicated stuff than you are currently doing.
  3. ChrisBrightwell macrumors 68020


    Apr 5, 2004
    Huntsville, AL
    java -cp . First

    (set the classpath explicitly.)
  4. loserDB thread starter macrumors newbie

    Oct 11, 2006
    Problems with installing java

    The problems are more with installing java on my machine,

    that bit of code was just something given by our lecturers to revise java from last year,

    how do i set the class path?

    I think that maybe netbeans isnt finding java on my machine,

    I had the same problem with xcode when I tried to use that.

    Please respond asap as I am really stuck with this
  5. loserDB thread starter macrumors newbie

    Oct 11, 2006
    Problem Solved

    The problem is now solved

    The first line should have been Println not just print, so the output was not correct, it is now working ok.
  6. lmalave macrumors 68000


    Nov 8, 2002
    Chinatown NYC
    That doesn't explain the ClassNotFoundException!!

    print() is a valid method. println() is just a print() that adds a carriage return to your string.

Share This Page