Why is it that i have to use terminal to compile and run a program in java?
(I'm a mac user)
I'm new at this whole programing thing. its been e开发者_运维知识库xplained 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?
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.
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
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.
精彩评论