(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?
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/davidor/Users/davidif your user name wasdavid.In UNIX, you have the concept of a working directory, your current location within the filesystem hierarchy, and
cdis the command you use to change it. Typically, this is the place programs will look for their files if you use a relative filename, sorm xyzzywill attempt to remove thexyzzyfile in your working directory whereasrm /xyzzywill 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.
javacis indeed the Java compiler, which will turn your source code into class files, andjavais 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/javasrcor/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
cdcommand would be more like one of: