Running Programs
OK, now that you've mastered the command line and are ready to teach your own class on it, let's move on to running "normal" programs. Linux is capable of running lots of familiar applications like Firefox and word processors. Many of these can be launched by clicking through the "Applications" menu at the top of the screen, but we're going to show you how to launch programs from a shell, since that will be more convenient.
Open up a Terminal, and type:
firefox &
This is how you can launch the iceweasel browser (a version of firefox) through the terminal.
A Note about Foreground / Background Processes
By default, when you run a program from the command line — either a simple UNIX utility like ls or a full application like Firefox — the program runs in the foreground, meaning it takes up the Terminal window and occupies it until you're finished with the program. For a simple utility that's fine, since the command is usually finished before you even realize, but when you open an application like a web browser, you're probably going to want to use it for more than a nanosecond. To avoid the problem of an "occupied" terminal, you can instruct the shell to open the program in the background by adding a & symbol to the end of the command, like you did above.
To see the difference, try opening Iceweasel again, but without the &. You'll see that you don't get a command prompt in that terminal until you close the application.
In general, if you're opening an application, it might be convenient to run it in the background with &.
A Note about Applications opened from the Terminal
Be aware that when you close a terminal window all the applications you opened from that terminal window will close without prompting you to save your work. Closing the terminal kills the terminal and everything inside it. Poor applications :-(
Kate: Text Editor of Dreams
In CS15 and most other computer science courses, code will be written as plain text. There are many text editors available to work with computer code, and the one we'll be using in CS15 is called "Kate."
To open up Kate, type into a shell:
kate &
If you get a "Session Chooser" screen pop up, select "Always use this choice" and then click on "New Session".
If you have a particular document that you want to open, you can specify that as well:
kate myFile.txt &
and Kate will open the specified document. (The path to the file can either be relative or absolute.)
A Quick Sidenote about File Extensions
In Linux, file extensions (the part of the filename after the .) are completely optional, and serve no purpose other than helpfulness for the user. For example, by convention all text files that contain Java code end in .java (e.g. MyJavaFile.java), which can make it easier to identify which files are which, but it makes no difference to the computer. One nice thing about using extensions, though, is that if you want to open all the Java files in a particular directory, you can refer to them all using the command *.java. The astrix * is another Linux shorthand called a "wildcard" and stands for any number of characters.
So if you typed:
kate *.txt &
Kate would open all the files in the current directory that ended in ".txt". Likewise,
kate My* &
would open all files in the current directory that started with "My".
Using Kate
When you open up Kate, it looks something like this:
A basic Kate window showing a Java file
(obviously, you're not expected to understand any of it).
Along the left side of the window is a sidebar, with tabs running down the side showing the function. You can use the sidebar to view a list of files that are open — when you start writing programs, you'll have several files open at once, and this is a convenient way of keeping track of them. In this window, there are two files open. You can also use the sidebar as a "file browser" to navigate the file system.
The buttons and menus across the top of the window have basic commands like "open," "save," "new file," etc. These commands should be familiar to anyone who has used a word processor before.
Kate has a lot of features that make it nice for working with code, and you can look up tutorials online for some of the cool features. We're going to highlight 3 of them right now, though:
- Attached to each Kate window is a Terminal section, that you can access by clicking on the "Terminal" button at the bottom of the screen. This will be particularly handy when you're working with code.
- Kate supports a feature called syntax highlighting, which means that different words are automatically displayed in different colors, depending on their meaning. This makes it easier to read code. Once we start digging into Java more, this will become clearer, but you can see in the window above that Kate has highlighted special keywords.
- Kate also supports code folding, which is a way of showing and hiding parts of code temporarily, so that it isn't distracting, sort of like opening and collapsing disclosure triangles in a file browser. You can see the little "minus" icons in the left margin of the above screen showing where you could click to fold the code.
Unfortunately, you can't really play with some of these features until you have some code to write, which will have to wait another week or so. In the meantime, rest assured that Kate is tons o' fun.
Open up a terminal and follow along:
-
Type
cdto return to your home directory, if you're not already there. -
Type
touch NewFile.txtto create a new file. -
Open your new file by typing:
kate NewFile.txt &. If a window pops up asking you about sessions, check "don't ask again" and pick the "new session" option. -
Type something into the open file and click the "Save" icon. Close Kate.
-
Type
mv NewFile.txt FileA.txtThis will move the file, essentially renaming it to "FileA.txt". Typelsto confirm. -
Type
cp FileA.txt FileB.txtThis will copy the file. Typelsto confirm. -
Type
kate *.txt &to open all files ending in.txt. Notice that both of the files you created open in a Kate window. Close Kate and remove those files using the
rmcommand.
Writing, Compiling and Running Java Programs
Whenever you write a Java application, you start by editing .java text files. You compile your code by running those files through the javac compiler. Then, your program is ready to run.
You'll learn more about the structure of a Java application as the semester progresses. For now, let's dive in to an introductory example:
-
Type
cs015_install lab0in a terminal to get the stencil code for lab 0. This will create anApp.javafile in/home/<yourlogin>/course/cs015/lab0/. -
cdinto thelab0folder and typekate App.java &to take a look the installed file. -
Check out the basic structure of a
.javafile. You'll see it's just a simple text file. The code inside of it probably makes very little sense at this point, and that's perfectly okay. The important part is where we callnew cs015.labs.TetrisSupport.Tetris(). This simply creates a game of Tetris for the user to play. -
Compile your code by typing
javac *.javain your shell, and then run it by typingjava lab0.App.
Congratulations! You just compiled and ran your first Java application!