PDA

View Full Version : Java




TeddyCarey
Apr 6, 2006, 09:06 PM
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
Apr 6, 2006, 10:09 PM
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
Apr 7, 2006, 10:15 AM
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
Apr 7, 2006, 10:32 AM
Ah, you just need to type 'java QuadraticFormula' to run your Application.

You might also find XCode (http://developer.apple.com/) from Apple (membership required) or Eclipse (http://www.eclipse.org/) 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
Apr 7, 2006, 12:43 PM
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
Apr 7, 2006, 02:46 PM
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
Apr 7, 2006, 03:06 PM
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
Apr 7, 2006, 05:55 PM
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
Apr 7, 2006, 07:13 PM
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.

TeddyCarey
Apr 7, 2006, 10:25 PM
Thanks again to all of you for your help...I will almost certainly be back :)

SC68Cal
Apr 7, 2006, 11:34 PM
I prefer Xcode myself.

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

http://java.sun.com/docs/books/tutorial/getStarted/cupojava/mac.html

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.