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

TeddyCarey

macrumors newbie
Original poster
Apr 6, 2006
9
0
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 :)
 

angelneo

macrumors 68000
Jun 13, 2004
1,541
0
afk
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 <filename.java>". If it compiled without any error, then do a "java <filename>" which will run your program.
 

TeddyCarey

macrumors newbie
Original poster
Apr 6, 2006
9
0
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 QuadraticFormuala.java (in the correct location) and it brings me to a new line [indicating that there are no errors?] Then when I just type java QuadraticFormula.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);

}
}
_______________________________________
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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 ;)
 

TeddyCarey

macrumors newbie
Original poster
Apr 6, 2006
9
0
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.
 

savar

macrumors 68000
Jun 6, 2003
1,950
0
District of Columbia
TeddyCarey said:
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.

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.
 

TeddyCarey

macrumors newbie
Original poster
Apr 6, 2006
9
0
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....
 

ryan

macrumors 6502
May 17, 2002
283
0
Denver, CO
TeddyCarey said:
Ok, what do you suggest then other than jedit.

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.
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
TeddyCarey said:
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....

I use jGrasp and it handles everything, including project management and creating the .jar file, plus it handles Objective-C, HTML, etc.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.