views:

131

answers:

3

(I'm a mac user)

I'm new at this whole programing thing. its been explained to me that i have to compile my program in terminal with:

cd ~
javac filename.java

and then run it with

java filename

Why do i have to do it this way? (i'm just curious) also what does cd ~ mean/do? What does javac mean? (is that javaCompile?) also i've had to save all the files i've done this with to my user. Why won't this work if i save the .java file in my documents or some other folder? or can i do this?

+8  A: 
cd ~

means change to your home directory (the place designated as yours in UNIX-land, including Mac OSX which is based on UNIX). It's likely to be something along the lines of /home/david or /Users/david if your user name was david.

In UNIX, you have the concept of a working directory, your current location within the filesystem hierarchy, and cd is the command you use to change it. Typically, this is the place programs will look for their files if you use a relative filename, so rm xyzzy will attempt to remove the xyzzy file in your working directory whereas rm /xyzzy will attempt to remove a file of the same name in the top level (root) directory.

And you don't have to compile and run Java that way. It's just one way of doing it. If you have an IDE like Eclipse, you probably never need use the command line at all.

javac is indeed the Java compiler, which will turn your source code into class files, and java is the Java runtime which will actually run those class files.

You can put your Java source code anywhere where you have the privileges to create files, you don't have to put them in your home directory. Of course, if you put them somewhere else, like /home/david/javasrc or /Users/david/javasrc, you'll need to ensure that's the directory you're in when you compile and run them from the command line.

To do that, the cd command would be more like one of:

cd /home/david/javasrc
cd /Users/david/javasrc
cd ~/javasrc
paxdiablo
what does that mean?
David
@David, see the update. Hopefully that explains it better.
paxdiablo
@paxdiablo, yes that does help allot. just one little bit of ambiguity though: when you give the 3 diferent cd commands at the end of your post is one suposed to do these in sequence or are they all examples of diferent possible commands that acomplish the same task?
David
David, the ~ is shorthand for your home directory regardless of what your home directory may be. The other two are explicit locations depending on what your actual home directory is. @RobKennedy did an edit to this answer to put in /Users so it's likely that Apple uses that location rather than /home or something else. That's why I explicitly state "like one of" since, technically, your home directory could be anywhere.
paxdiablo
+8  A: 

You don't. You can very well use an IDE like Eclipse or Netbeans.

However doing it initially using a command prompt and simple text editor is better as it helps in understanding the basics of Java. Especially the Syntax, Classpath etc.

Padmarag
+3  A: 

David, remember my answer to your previous question How do I compile and run a program in Java on my Mac?

I'm sure everybody here would agree with me that getting an IDE like Eclipse is a good idea. It hides a lot of the low-level tediousness involved, and automates most of the basic routines that you do as a developer.

polygenelubricants
that's the down side of using ides and similar tools, they provide too much level of abstraction to new users... and i personally think thats bad. i think shifting to the use of ide and other similar tools which makes our work easier and faster should be done only when you already have a thorough understanding of the underlying concepts... now i hope someone would tell me my mindset is wrong... just a thought!
ultrajohn
I can see your point, but there is no one right level of abstraction for everyone. It helps to know the low level hardware/operating system stuff, for example, but certainly it's acceptable to start learning how to program without first mastering these fundamentals. People have to start somewhere, and I think starting with an IDE is fine. He already picked a high level language like Java. We shouldn't be telling him to learn assembly first.
polygenelubricants
yeah, that's true, but for sure sooner or later, this guy will step back to the basics... they're essential...
ultrajohn
hold on i have to learn assembly?!?!?
David
If you're a CS student you'll eventually have to.
Santa