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

javahelp7

macrumors newbie
Original poster
Aug 27, 2008
8
0
Mkay so I want to start programming Java, and I have a new macbook pro with the Mac OS X built in, and so where in the world do I start?! I've opened up the terminal, but now what? I want to just do the "Hello, World!", and I have no clue where to begin. I have the code and I just don't know where to type it. If I just start typing in the terminal, it gives me an error since I can't go farther than the next line. I've read things about, oh, just type in 'pico', or download NetBeans. Which leads me to another question. Is NetBeans necessary? I thought everything you needed was already built in. Also, if I do indeed need NetBeans, which one should I download? There's like 5 or 6 options! Help please!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
NetBeans is a pretty good IDE, but you don't need that yet. pico is a commandline text editor, but its capabilities are limited. If you were to aspire to longterm development I would recommend learning vi, but people seem to be opposed to that. You can just enter your code in textedit and save as plain text. There are other good text editors like Smultron (free), TextMate, and TextWrangler (not free) available without having to learn the key combinations needed for vi.

Once you've picked an editor, enter your code and save it with the name of your primary class followed by .java. Then navigate to the directory you saved the file in from the terminal and run:
javac MyHello.java
This will compile your class. If there are errors, you will be told where/what they are.

Once you've compiled without errors, run it with the java command followed by the name of your class:
java MyHello

That's it! Once you start getting complicated you can switch to an IDE like NetBeans or Eclipse, but while you're learning focus on the language fundamentals, not the nuance of a particular IDE.

-Lee
 

javahelp7

macrumors newbie
Original poster
Aug 27, 2008
8
0
NetBeans is a pretty good IDE, but you don't need that yet. pico is a commandline text editor, but its capabilities are limited. If you were to aspire to longterm development I would recommend learning vi, but people seem to be opposed to that. You can just enter your code in textedit and save as plain text. There are other good text editors like Smultron (free), TextMate, and TextWrangler (not free) available without having to learn the key combinations needed for vi.

Once you've picked an editor, enter your code and save it with the name of your primary class followed by .java. Then navigate to the directory you saved the file in from the terminal and run:
javac MyHello.java
This will compile your class. If there are errors, you will be told where/what they are.

Once you've compiled without errors, run it with the java command followed by the name of your class:
java MyHello

That's it! Once you start getting complicated you can switch to an IDE like NetBeans or Eclipse, but while you're learning focus on the language fundamentals, not the nuance of a particular IDE.

-Lee

Thanks so much this was a huge help! Especially since I didn't know that you needed to write it first in textedit, but oh, I downloaded Smultron and at least that lets me save it under .java since under textedit i could only save it if they added on .rtf or something like that. But I also have some more questions. You said after I wrote it, save it with a primary class. What is that? If I'm just trying to do the "Hello, World!" one, do I save it as HelloWorld.java? Or MyHello.java? I'm confused. Because once I get into terminal, do I go under Shell and click on Import? If so, I'm not able to click on the files I saved. But I'm pretty sure I'm doing something wrong. And when you compile it, right afterwards can you run it on the same terminal screen? Confused!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Thanks so much this was a huge help! Especially since I didn't know that you needed to write it first in textedit, but oh, I downloaded Smultron and at least that lets me save it under .java since under textedit i could only save it if they added on .rtf or something like that. But I also have some more questions. You said after I wrote it, save it with a primary class. What is that? If I'm just trying to do the "Hello, World!" one, do I save it as HelloWorld.java? Or MyHello.java? I'm confused. Because once I get into terminal, do I go under Shell and click on Import? If so, I'm not able to click on the files I saved. But I'm pretty sure I'm doing something wrong. And when you compile it, right afterwards can you run it on the same terminal screen? Confused!

Slow down, there! Don't work yourself into a tizzy. =)

It sounds like you had some example code that you saved with smultron. One of the first lines would look like:
Code:
public class HelloWorld{

Whatever appears immediately after 'class' is the name of your class. It needs to have a method:
Code:
public static void main(String[] args)

So if, as my example above shows, your class name is HelloWorld, you should save your file as HelloWorld.java.

If, for example, you saved this in your desktop, you would open the terminal, and by default you would be in your home folder. To change directories to the Desktop folder, type:
cd Desktop<return>

Your working directory will now be the Desktop. You will then type:
javac HelloWorld.java

Assuming there are no errors, there will now be a file called HelloWorld.class. You can verify this by typing:
ls -l HelloWorld.class<return>

To run your program, you will type:
java HelloWorld<return>

You should see the text in your System.out.print(ln) display in the terminal.

If you need more help, just post.

-Lee
 

javahelp7

macrumors newbie
Original poster
Aug 27, 2008
8
0
Oh my gosh hurray!!!!! It finally works! My first ever program! Haha! Thank you so much, but also, just to clarify things, do I always have to type "javac" with the class, and right after javac, type in java with the class, just to get my program running? Plus, what if I save stuff in a folder IN documents. Then what? How would I get inside the folder that's inside documents? Lets say the folder's name is Chapter 1. So would I somehow type in "cd Documents Chapter 1" or something like that? And also, this time I didn't have any mistakes or anything, but if I do get an error, then what? Will it tell me if it's a spelling error, or if I forgot something, or what? Oh, and I also have questions about future IDEs. Right now I have Smultron, and then I go over to the terminal. But if I ever get NetBeans, or Eclipse, or whatever else there is, first, which one is the most user-friendly or common? And also, for example, on NetBeans, there's like 6 different types you choose from to download. Which one? What's the difference between "Java" and "Web and Java EE"? Also, if I do get NetBeans, Eclipse, etc., do I ever have to use the terminal anymore? Is there a place where I can type it in, click a button, have it compiled, click another button, have it run, etc.? Or do I always have to type in "javac" and "java" for it to compile and run? Sorry if I'm asking sooo many questions!
 

iShater

macrumors 604
Aug 13, 2002
7,027
470
Chicagoland
One of my favorite books is O'Reilly's Learning Java. Take a look and see if it fits your current comfort with general programming concepts.

Oh, and we have a few good folks around here who know how to nudge you in the right direction ... *hint hint* *cough* Lee1210 *cough* ;) :D
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Boy, that was a lot of questions. there's nothing wrong with that at all, but maybe break things into paragraphs when you change thoughts. It's sort of hard to read. Let's see:

do I always have to type "javac" with the class, and right after javac, type in java with the class, just to get my program running?
javac is the java compiler. You will use it to build the .class files for any .java file you write. The rule in java is one class per file (you can have classes nested inside of one another, but that's much later on). Right now you will probably only be using one class for each program, but eventually you'll be branching out and writing your own classes and each will need it's own file. The compiler changes your source code, which is just a bunch of meaningless text to the computer (or JVM, the machine that runs java bytecode), and turns it into machine code (bytecode in the case of java, which is the machine code of the JVM).

I've mentioned the JVM a few times so far, so that might bear some explanation. The JVM (Java virtual machine) is a software implementation of a computer that can be run on many different types of actual hardware. This is why you can (in general) run java code on windows, the mac, unix, linux, etc. without changes. There are of course exceptions, but in general by using the JVM Sun allows programmers to target that machine instead of one specific type of hardware and OS.

Back to your question(s)... java is the program which will actually interpret and run your .class files. When you give it the name of the class, it searches for a .class files that contains that class, then searches for the main method with the signature that was detailed earlier. Once it finds this, it starts running the code (that is now compiled to bytecode).

So, in essence, each time you change a .java file, you have to compile that to a .class file with javac so it can be run by java.

Plus, what if I save stuff in a folder IN documents. Then what? How would I get inside the folder that's inside documents? Lets say the folder's name is Chapter 1. So would I somehow type in "cd Documents Chapter 1" or something like that?

http://www.uic.edu/depts/accc/software/unixgeneral/unix101.html
This is a brief UNIX tutorial that should answer your questions about directory navigation and file manipulation in UNIX. When you're using the terminal, this is basically your portal to the UNIX underpinnings of OS X. If you had directories set up like:
/Users/javahelp7/Documents/Chapter 1/

then when you open the terminal, you will be in your home directory (check where you are with the pwd command), so to get into the Chapter 1 directory, you'd type:
cd Documents<return>
cd "Chapter 1"<return>

The quotes are necessary for directories with spaces. Otherwise you have to type a \ before the space. You can also start typing filenames or directory names in the terminal and press <tab> and the shell will autocomplete names for you, which can be helpful with long directory names with a lot of spaces, etc.

And also, this time I didn't have any mistakes or anything, but if I do get an error, then what? Will it tell me if it's a spelling error, or if I forgot something, or what?

The compiler isn't human, so it won't be able to tell you everything, but it is quite good at telling you at least where, if not exactly what, you've done wrong.

https://forums.macrumors.com/members/170727/
This member is just starting out with Java as well, and has run into a lot of compiler errors. From their profile you can access threads they have started and see some examples. Some things are a little trickier, like forgetting a ; at the end of a line. The error will be on the next line because the compiler thinks you are continuing the statement from the line above. Some errors will be cryptic at first, but you will learn what the compiler is complaining about pretty quickly.

Oh, and I also have questions about future IDEs. Right now I have Smultron, and then I go over to the terminal. But if I ever get NetBeans, or Eclipse, or whatever else there is, first, which one is the most user-friendly or common? And also, for example, on NetBeans, there's like 6 different types you choose from to download. Which one? What's the difference between "Java" and "Web and Java EE"?

I like NetBeans the most, but for my purposes I have to use Eclipse (I thought being the architect for this product would mean i got to choose these sorts of things. Web services are sticky.). NetBeans is pretty straight-forward for basic kinds of things. However, once you start with an IDE getting your code/project OUT is pretty tough. The Java SE version of NetBeans should be fine, but I always get "All" just in case. =)

Also, if I do get NetBeans, Eclipse, etc., do I ever have to use the terminal anymore? Is there a place where I can type it in, click a button, have it compiled, click another button, have it run, etc.? Or do I always have to type in "javac" and "java" for it to compile and run?

The commandline builds character, dagnabbit! Yes, with IDEs they are the whole package in one program (hence the (I)ntegrated part of the acronym). They generally include an editor, compiler (and linker for languages that need it...), a means to run programs, and a debugger. Some have more than this, but this is generally what people have come to expect from an IDE. So you will not get to use the commandline anymore (it's not a matter of having to). =)

Sorry if I'm asking sooo many questions!

We all start somewhere. Just take a breathe between them and hit enter a few times.

Oh, and we have a few good folks around here who know how to nudge you in the right direction ... *hint hint* *cough* Lee1210 *cough* ;) :D

I try =).

-Lee
 

javahelp7

macrumors newbie
Original poster
Aug 27, 2008
8
0
Again, thank you so much! And yes, I will definitely space out my messages now, sorry! Okay, so I also have some more questions, surprise surprise....
1. If I get NetBeans, they have versions 6.1 and 6.5 Beta, so...which one? And just get "All" then instead of "Java", or "Web and Java EE", etc.?
2. On the Terminal, as of right now, I'm having to type in "cd Documents", or "cd Documents/"Computer Science" ", etc. before I start compiling and running programs. Is there some way that it will permanently stay there so I won't have to type that in every time? Or is this just another one of the "character building" things that I'm supposed to learn lol!
And...I think that's all the questions I have so far!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Again, thank you so much! And yes, I will definitely space out my messages now, sorry! Okay, so I also have some more questions, surprise surprise....
1. If I get NetBeans, they have versions 6.1 and 6.5 Beta, so...which one? And just get "All" then instead of "Java", or "Web and Java EE", etc.?
2. On the Terminal, as of right now, I'm having to type in "cd Documents", or "cd Documents/"Computer Science" ", etc. before I start compiling and running programs. Is there some way that it will permanently stay there so I won't have to type that in every time? Or is this just another one of the "character building" things that I'm supposed to learn lol!
And...I think that's all the questions I have so far!

Well, you could just leave a terminal window open, and use it as needed. You should only need to switch to that directory once per "session", or less if you just keep it open all of the time and hide it while you're not actively programming. I guess for me navigating directories from a commandline is just second nature now. It certainly isn't going to be the most complicated thing you have to come to terms with when programming. You can always put temporary symlinks using
ln -s ./Documents/Project\ 1 P1
in your home directory, then just
cd P1

to get there in future sessions.

At this point for NetBeans you won't need any new features available in the beta, so i'd just get 6.1. Just get the Java SE version for now and you'll be fine.

I'd resist the siren's song of the IDE for as long as you can. It's good to know what's actually going on, and the IDE abstracts all of that from you. When you know what's going on, and your builds are getting too complicated (and you don't want to learn how to use ant =) ) then you need an IDE. I do think that with Java's large class library having access to code completion is a real boon. On top of that though, this should be your best friend:
http://java.sun.com/j2se/1.5.0/docs/api/

-Lee
 

daflake

macrumors 6502a
Apr 8, 2008
920
4,329
I took Java in college and the book I used was by Savitch and it was pretty good. However, another book that I used that was fun and explained things clearly is Head First Java by O'Reilly. I highly recommend this....

An IDE is not necessary but can be helpful. Start with a text editor to get the basics and then move to the IDE (Netbeans or Eclipse) when you feel more comfortable as you will have to learn the IDE and JAVA at the same time thus making it more difficult.
 

javahelp7

macrumors newbie
Original poster
Aug 27, 2008
8
0
Okay thanks! That was a lot of help! Oh, the reason I asked about the terminal was because whenever I do a new program, I always opened up a new terminal, but I guess that was because it was getting too cluttered and now I can just clear the scrollback!
Anyway okay, so I have another question.


import javax.swing.JOptionPane;

public class DialogViewer
{
public static void main(String[] args)
{
String name = JOptionPane.showInputDialog("What is your name?");
System.out.println(name);
System.exit(0);
}
}


So how do I modify the program to print "Hello, Audrey!", displaying my name that I typed in?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Okay thanks! That was a lot of help! Oh, the reason I asked about the terminal was because whenever I do a new program, I always opened up a new terminal, but I guess that was because it was getting too cluttered and now I can just clear the scrollback!
Anyway okay, so I have another question.


import javax.swing.JOptionPane;

public class DialogViewer
{
public static void main(String[] args)
{
String name = JOptionPane.showInputDialog("What is your name?");
System.out.println(name);
System.exit(0);
}
}


So how do I modify the program to print "Hello, Audrey!", displaying my name that I typed in?

Please use code tags. I can't type them literally because they'd get sucked up when I submit it, but they look like [ CODE] [ /CODE] without the spaces in there. Put those around your blocks of code to retain formatting. There is a hash mark # in the editor to insert them for you, too.

You should take a look at JOptionPane:
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JOptionPane.html

It looks like there is a showMessageDialog method. You have to pass null as the first argument, then the string you want to print. The code you posted already prints the entered name at the terminal. This method will show it in a dialog box, instead.

Your code would change to:
Code:
import javax.swing.JOptionPane;

public class DialogViewer
{
  public static void main(String[] args) {
    String name = JOptionPane.showInputDialog("What is your name?");
    //System.out.println(name); //Took this out
    JOptionPane.showMessageDialog(null,"Welcome, " + name +"!"); //Put this in
  }
}

Give it a whirl. As an aside, console I/O might be a better plan to start with than Swing. It won't hurt, but learning with System.in and System.out might be a better plan.

-Lee

Edit: I'm a server-side guy, so I haven't done Java on the client other than school projects. Swing might be perfectly fine for a beginner, but I'm totally unfamiliar with it so I'm not sure if it gets too complicated too fast.
 

NT1440

macrumors Pentium
May 18, 2008
15,092
22,158
if you want to start reeeeaaaaly basice the kareltherobot program i find is a good foundation (im currently taking it in school)
 

javahelp7

macrumors newbie
Original poster
Aug 27, 2008
8
0
Please use code tags. I can't type them literally because they'd get sucked up when I submit it, but they look like [ CODE] [ /CODE] without the spaces in there. Put those around your blocks of code to retain formatting. There is a hash mark # in the editor to insert them for you, too.

You should take a look at JOptionPane:
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JOptionPane.html

It looks like there is a showMessageDialog method. You have to pass null as the first argument, then the string you want to print. The code you posted already prints the entered name at the terminal. This method will show it in a dialog box, instead.

Your code would change to:
Code:
import javax.swing.JOptionPane;

public class DialogViewer
{
  public static void main(String[] args) {
    String name = JOptionPane.showInputDialog("What is your name?");
    //System.out.println(name); //Took this out
    JOptionPane.showMessageDialog(null,"Welcome, " + name +"!"); //Put this in
  }
}

Give it a whirl. As an aside, console I/O might be a better plan to start with than Swing. It won't hurt, but learning with System.in and System.out might be a better plan.

-Lee

Edit: I'm a server-side guy, so I haven't done Java on the client other than school projects. Swing might be perfectly fine for a beginner, but I'm totally unfamiliar with it so I'm not sure if it gets too complicated too fast.

Thank you so much, it helped! I think those are all the questions I have....for today anyway, lol! And thanks for the CODE heads up, I will definitely do that from now on.
 

daflake

macrumors 6502a
Apr 8, 2008
920
4,329
Glad to see that you are diving right in but I do recommend what Lee said. Start with command line first before you start jumping into GUI. This will give you the foundation that you need so that you don't get frustrated trying to fight GUI and simple code issues. Once you get comfortable then jump into Swing.

Also there is another good web site called JAVA Ranch

http://www.javaranch.com
 

javahelp7

macrumors newbie
Original poster
Aug 27, 2008
8
0
Glad to see that you are diving right in but I do recommend what Lee said. Start with command line first before you start jumping into GUI. This will give you the foundation that you need so that you don't get frustrated trying to fight GUI and simple code issues. Once you get comfortable then jump into Swing.

Also there is another good web site called JAVA Ranch

http://www.javaranch.com

"A friendly place for Java greenhorns"?! Lol, that reminds me of deadliest catch and their greenhorns. Anyway thanks, this looks like a good site, and yes, I'm definitely sticking with command line right now since I got NetBeans and....it's totally different, so yes, I'll just wait!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.