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

pnz999

macrumors member
Original poster
Nov 6, 2002
70
0
java error in mac os x.2.3 terminal.

I get this error "Exception in thread "main" java.lang.NoClassDefFoundError" when I try to run my *.class file under Terminal? What should I do?

I think I compile it correctly cause it pop up a *.class file with no error comment

import javax.swing.JOptionPane;

public class circle
{
public static void main(String[] args)
{
String input = JOptionPane.showInputDialog ("Please enter the radius");
double rad = Double.parseDouble(input);

double area = Math.PI*2*rad;
double cir = Math.PI*Math.pow(rad,2);

System.out.println("The area is " + area + ".");
System.out.println("The circumference is " + cir + ".");
}
}
 
Here are the correct commands to use. Note that on the "java" command, which runs the .class file, you do NOT specify "circle.class":

javac circle.java
java circle

The program works. However, this is not really a good way to do things, partially using Swing and partially using text input-output. After it displays its output, you have to break (with ctrl-C) to get back to the system prompt.

What I usually do is have main just construct the outer class:

public static void main( String args[] ) {
circle c = new circle();
}

Then have the constructor for class circle do all the work:
public circle() {
etc.
}

But Swing requires a little more complexity in application design. Keep learning!
 
I ran your program just fine. After you've compiled it, type the following (without the quotes): "java circle". Don't put ".class" after "circle."

It should work--let me know if it doesn't.
 
It work! Thanks

I just need to be in the same directory as my *.java and *.class files.


BTW, is it possible to do C++ programming in a mac? can i still use Terminal?

and also How can I tell what version of Java in on my machine(10.2.3 / G4 MDD)? and Java on a Mac is the same as Sun's Java?

cause current Sun's Java version is 1.4.1_01
 
You can definitely do C++ programming within the terminal.

type cc -o programmefile programsource for C, C++, and Objective-C.

Then, once it compiles successfully, you may run your programme by typing ./programmefile
 
Originally posted by pnz999
BTW, is it possible to do C++ programming in a mac? can i still use Terminal?

Yes you can. Check out this thread for more info:

C++ Programming on OS X


and also How can I tell what version of Java in on my machine(10.2.3 / G4 MDD)? and Java on a Mac is the same as Sun's Java?

cause current Sun's Java version is 1.4.1_01


Type (again, without the quotes) "java -version" in Terminal. OS 10.2.3 currently comes with Java 1.3.1. Apple currently has a beta version of Java 1.4 for OS X, and it should hopefully be released soon.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.