Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

loserDB

macrumors newbie
Original poster
Oct 11, 2006
3
0
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(System.in);

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

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...
init:
deps-jar:
compile-single:
run-single:​

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(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)​


Any help would be appreciated.
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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 'First.java' (as the class is called First the file MUST be 'First.java' 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 First.java' 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.
 

loserDB

macrumors newbie
Original poster
Oct 11, 2006
3
0
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
 

loserDB

macrumors newbie
Original poster
Oct 11, 2006
3
0
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.
 

lmalave

macrumors 68000
Nov 8, 2002
1,614
0
Chinatown NYC
loserDB said:
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.

That doesn't explain the ClassNotFoundException!!

print() is a valid method. println() is just a print() that adds a carriage return to your string.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.