• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

New to Java

 
Dietrich Lehr
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need some help getting Java set up initially on my Mac OS X computer. I'm new to mac and java but have played around with html before.

I have the book "Head First Java" and in the part about setting up Java, it says that I need to add an entry to my Path environment variable (whatever that is) that points to the /bin directory inside the main Java directory.

I've tried searching my mac for the bin directory, but haven't been able to find it. I haven't downloaded and installed any java, because the mac os x is supposed to come with Java. Java is up to date as well.

My question is, how to I even run a .java file on my mac (i'm guessing through terminal?) and how do I set up the path so that it points to wherever it needs to point so that I can run my app. And how do I compile a .java file? From what little I understand since I just started, the compiler changes the .java to a .class file so that your JVM can run it. I just don't know how to do this : /

I am using Text Edit on the mac to do this. I got it to work in Eclipse by making a MyFirstApp class under the src folder and then just clicking run. Then in that bottom window under the console tab, it showed my text " I rule the world". I guess clicking the run button at the top compiles and runs your work. I'd still like to know how I could do this the more manual way. That way I understand what Eclipse does when it does it automatically.

I also noticed in my Java Preferences application, that under the advanced tab under java console, it was defaulted to "do not start console" as opposed to "show console" or "hide console". Does that matter?

This is the source code i'm practicing with:

Other than being able to save it as a .java file, I am dead in the water. Please help!
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Open a terminal window and try this out.



To run your example, type


This would compile your code.

Last, to run it, type


This should print the text onto the console. Hope it helps.

 
Dietrich Lehr
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java -version worked and my version is 1.6.0_20

When I typed javac MyFirstApp.java I got this


I have the file saved onto my desktop, I'm not good with command prompts so how would I get it to point to where it's at?
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use 'cd' to change the directory and point to the folder where you have the .java files.

 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't set a system classpath, did you? That can make it impossible to find files, if done wrongly.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On windows type dir and on Unix/Linux ls, then you can see whether you actually have that file available.
 
Dietrich Lehr
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't set any sort of path as I have no idea how to do that and am afraid to mess around with it on my own.

I have my java file saved to a folder on my desktop. I managed to navigate to
in the terminal, but can't get it to go to the folder where MyFirstApp.java is.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find about setting the PATH on a Mac here. But it is usually not necessary on a Mac. Macs come with Java preinstalled, so your PATH should already be correct. As well as writing java -version, try javac -version. If you get a sensible result from that (eg 1.6_0_20), then your PATH is probably already correct, and you ought not to change it.
Open a shell and see which directory you are in. Youwill have to learn to use the cd command (list of Mac commands here). You will need to work out the exact name of the file in your desktop; I usually use Linux rather than OS/X, but it would require something like cd /home/campbell/desktop or cd ~/desktop on a Linux box.
 
Dietrich Lehr
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the javac -version command worked and it gave me the version, so I guess my path is correct. I also managed to cd my way to the folder on my desktop. Now I'm searching through those list of commands to see how to actually run my .java file.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To "run" the java file, type "java Filename" (with no .class extension) after you've run "javac Filename.java".

Although I wouldn't really recommend putting this on your desktop; create a directory for your Java work.

I *strongly* recommend reading some basic Unix command-line tutorials, though.
 
Dietrich Lehr
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for your help! I got it to work.


Glad I found this website and that the Head First Java book even recommended it.
 
Dietrich Lehr
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:To "run" the java file, type "java Filename" (with no .class extension) after you've run "javac Filename.java".

Although I wouldn't really recommend putting this on your desktop; create a directory for your Java work.

I *strongly* recommend reading some basic Unix command-line tutorials, though.


I just have it on the desktop so I can get to it quickly and remember where it is while I'm messing around. As I get more serious I'll have places dedicated for it. Thanks for your help!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dietrich Lehr wrote:Thanks everyone . . . Glad I found this website and that the Head First Java book even recommended it.
You're welcome

Look at the authors of Head First Java: Kathy Sierra helped found JavaRanch, and we see her occasionally. Bert Bates is still very active here.

I agree with David Newton: the sooner you create yourself a "java work" folder the better.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic